CF 1249D1 - Too Many Segments (easy version) 贪心+排序+set的使用

https://codeforces.com/blog/entry/70779

分析:想到在要删去一条线段时贪心的选取右坐标最长的那一个肯定正确。

就可以利用排序,即set的自动排序再重定义运算符来处理(按左坐标的顺序插入,按右坐标大小排序),用size()表示覆盖的边数,坐标从左到右一个个该删删该增增,维护一遍。

一个技巧:vector<Node> segs[maxn]  的Node 是右坐标 和 线段的序号,而用数组下标用左坐标来表示,就能够实现上述的坐标上扫一遍的处理。

#include <bits/stdc++.h>

using namespace std;
const int maxn = 2e5+10;

struct Node{
    int r;
    int idx;
};
bool operator < (Node a,Node b){
        if(a.r != b.r)
            return a.r<b.r;
        return a.idx<b.idx;
}

vector<Node> segs[maxn];
set<int> ans;
int n,k,x,y;

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cin>>n>>k;
    for(int i=0; i<n; i++){
        cin>>x>>y;
        Node temp;
        temp.r = y;
        temp.idx = i;
        segs[x].push_back(temp);
    }
    set<Node> s;
    for(int i=0; i<maxn; i++){
        while(s.size() && (*s.begin()).r<i ){
            s.erase(s.begin());
        }
        for(Node it: segs[i]){
            s.insert(it);
        }
        while(s.size() > k){
            ans.insert( (*s.rbegin()).idx );
            s.erase( *(s.rbegin()) );
        }
    }
    cout<<ans.size()<<endl;
    for(int it : ans){
        cout<<it+1<<" ";
    }
    cout<<endl;
}

//c.begin() 返回一个迭代器,它指向容器c的第一个元素

// c.end() 返回一个迭代器,它指向容器c的最后一个元素的下一个位置

// c.rbegin() 返回一个逆序迭代器,它指向容器c的最后一个元素

// c.rend() 返回一个逆序迭代器,它指向容器c的第一个元素前面的位置

原文地址:https://www.cnblogs.com/-Zzz-/p/11787495.html

时间: 2024-11-09 12:02:11

CF 1249D1 - Too Many Segments (easy version) 贪心+排序+set的使用的相关文章

CF1108E1 Array and Segments (Easy version)(待更新)

题目地址:CF1108E1 加强版题解:https://www.cnblogs.com/xht37/p/10322344.html 原文地址:https://www.cnblogs.com/xht37/p/10322340.html

Codeforces 1108E2 Array and Segments (Hard version) 差分, 暴力

Codeforces 1108E2 E2. Array and Segments (Hard version) Description: The only difference between easy and hard versions is a number of elements in the array. You are given an array \(a\) consisting of \(n\) integers. The value of the \(i\)-th element

2016 省热身赛 Earthstone: Easy Version

Earthstone: Easy Version Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu Description Earthstone is a famous online card game created by Lizard Entertainment. It is a collectible card game that revolves around turn-based mat

05-图2. Saving James Bond - Easy Version (25)

05-图2. Saving James Bond - Easy Version (25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue This time let us consider the situation in the movie "Live and Let Die" in which James Bond, the world's most famous spy, was capture

Codeforces Round #245 (Div. 2) A - Points and Segments (easy)

水到家了 #include <iostream> #include <vector> #include <algorithm> using namespace std; struct Point{ int index, pos; Point(int index_ = 0, int pos_ = 0){ index = index_; pos = pos_; } bool operator < (const Point& a) const{ return p

05-2. Saving James Bond - Easy Version (25)

05-2. Saving James Bond - Easy Version (25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue This time let us consider the situation in the movie "Live and Let Die" in which James Bond, the world's most famous spy, was captured

Codeforces 430A Points and Segments (easy)

题意:让你染色点,要求在给出的区间内|红色个数-蓝色个数|<=1 思路:排序后依次交替染色就能达到效果 #include <iostream> #include <cstring> #include <algorithm> #include <cstdio> #include <vector> using namespace std; const int MAXN = 110; int arr[MAXN]; int n,m,x,y; int

pat05-图2. Saving James Bond - Easy Version (25)

05-图2. Saving James Bond - Easy Version (25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue This time let us consider the situation in the movie "Live and Let Die" in which James Bond, the world's most famous spy, was capture

PAT Saving James Bond - Easy Version

Saving James Bond - Easy Version Assume that the lake is a 100 by 100 square one. Assume that the center of the lake is at (0,0) and the northeast corner at (50,50). The central island is a disk centered at (0,0) with the diameter of 15. A number of