Codeforces 1249 D2. Too Many Segments (hard version)

传送门

贪心

对于第一个不合法的位置,我们显然要通过删除几个覆盖了它的区间来使这个位置合法

显然删右端点更靠右的区间是更优的,所以就考虑优先删右端点靠右的,然后再考虑下一个不合法位置

用一个 $set$ 维护一下右端点和区间编号即可

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<vector>
#include<set>
using namespace std;
typedef long long ll;
inline int read()
{
    int x=0,f=1; char ch=getchar();
    while(ch<‘0‘||ch>‘9‘) { if(ch==‘-‘) f=-1; ch=getchar(); }
    while(ch>=‘0‘&&ch<=‘9‘) { x=(x<<1)+(x<<3)+(ch^48); ch=getchar(); }
    return x*f;
}
const int N=2e5+7;
int n,m;
struct dat {
    int r,id;
    dat (int _r=0,int _id=0) { r=_r,id=_id; }
    inline bool operator < (const dat &tmp) const {
        return r!=tmp.r ? r<tmp.r : id<tmp.id;
    }
};
vector <dat> V[N];
vector <int> mov;
set <dat> S;
int main()
{
    n=read(),m=read(); int a,b,mx=0;
    for(int i=1;i<=n;i++)
        a=read(),b=read(),
        mx=max(mx,b),
        V[a].push_back(dat(b,i));
    int ans=0;
    for(int i=1;i<=mx;i++)
    {
        while(S.size()&&(*S.begin()).r<i) S.erase(S.begin());
        for(auto x: V[i]) S.insert(dat(x.r,x.id));
        while(S.size()>m)
        {
            auto p=S.rbegin();
            ans++,mov.push_back((*p).id);
            S.erase(*p);
        }
    }
    printf("%d\n",ans);
    for(auto x: mov) printf("%d ",x); puts("");
    return 0;
}

原文地址:https://www.cnblogs.com/LLTYYC/p/11756608.html

时间: 2024-08-30 17:38:55

Codeforces 1249 D2. Too Many Segments (hard version)的相关文章

Codeforces Round #535 E2-Array and Segments (Hard version)

题意: 给你一个数列和一些区间,让你选择一些区间(选择的区间中的数都减一), 求最后最大值与最小值的差值最大,并输出选择的区间 思路: 在n=300的时候,我们是枚举每个数作为最小值,应用所有覆盖它的区间,并且没 次都更行差值的最大值. 但是这里的n=1e5,所以我们不能用O(n*n*m),但是我们看到这里的m=300 所以可以从m入手,枚举区间,就是记录每个区间的两个端点,利用差分的思想, 来枚举更新最大值 这里说一下为什么枚举最小值,因为如果最大值也在这个区间则抵消,如果没在则 更好 #in

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

D2 Equalizing by Division (hard version) &amp;&amp;D1 Equalizing by Division (easy version) (easy version)(Codeforces Round #582 (Div. 3))

The only difference between easy and hard versions is the number of elements in the array. You are given an array aa consisting of nn integers. In one move you can choose any aiai and divide it by 22 rounding down (in other words, in one move you can

Educational Codeforces Round 10 D. Nested Segments (树状数组)

题目链接:http://codeforces.com/problemset/problem/652/D 给你n个不同的区间,L或者R不会出现相同的数字,问你每一个区间包含多少个区间. 我是先把每个区间看作整体,按照R从小到大排序.然后从最小的R开始枚举每个区间,要是枚举到这个区间L的时候,计算之前枚举的区间有多少个Li在L之后,那么这些Li大于L的区间的数量就是答案.那我每次枚举的时候用树状数组add(L , 1) 说明在L这个位置上出现了区间,之后枚举的时候计算L之前的和,然后i - 1 -

CF Educational Codeforces Round 10 D. Nested Segments 离散化+树状数组

题目链接:http://codeforces.com/problemset/problem/652/D 大意:给若干个线段,保证线段端点不重合,问每个线段内部包含了多少个线段. 方法是对所有线段的端点值离散化,按照左端点从大到小排序,顺着这个顺序处理所有线段,那么满足在它内部的线段一定是之前已经扫到过的.用树状数组判断有多少是在右端点范围内. 1 #include <iostream> 2 #include <vector> 3 #include <algorithm>

【CodeForces】901 C. Bipartite Segments

[题目]C. Bipartite Segments [题意]给定n个点m条边的无向连通图,保证不存在偶数长度的简单环.每次询问区间[l,r]中包含多少子区间[x,y]满足只保留[x,y]之间的点和边构成的图是一个二分图. [算法]Tarjan缩点(找环) [题解]如果两个奇数长度的环相交,会得到一个偶数长度的简单环.所以原图是不存在偶数长度环的仙人掌(每条边只属于一个简单环). 二分图的定义:一个图是二分图当且仅当不存在奇数长度的环.在当前仙人掌上,二分图实际上要求选择的点不存在环. 也就是对于

Codeforces #496 E1. Median on Segments (Permutations Edition)

http://codeforces.com/contest/1005/problem/E1 题目 https://blog.csdn.net/haipai1998/article/details/80985281  原博客 对样例1: 5 42 4 5 3 1 m=4,所以下标pos=2: 从pos往右遇到比m大的就cnt++,遇到小的就cnt--: 刚开始cnt=0;  mp[cnt]++ 于是从pos开始到 n:   mp[0]=1,   mp[1]=1,   mp[0]=2 ,   mp[

CodeForces 1152F1 Neko Rules the Catniverse (Small Version)

题目链接:http://codeforces.com/problemset/problem/1152/F1 题目大意 有 n 个星球,给定限制 m,从 x 星球走到 y 星球的条件是,$1 \leq y \leq x + m$,且 y 不能被访问过. 求游玩其中 k 个星球有多少种不同的方案? 分析 一开始我的想法是二维 dp,dp[i][j] 表示前 i 个星球,访问其中 j 个,一共的方案种数,然后在遍历到第 i + 1 个星球的时候,很明显有访问和不访问两种操作,不访问好算,直接赋值即可,

CodeForces 1152F2 Neko Rules the Catniverse (Large Version)

题目链接:http://codeforces.com/problemset/problem/1152/F2 题目大意 见http://codeforces.com/problemset/problem/1152/F1,此题 n 最大能到 109. 分析 在 F1 的基础上, 代码如下 时间复杂度:$O(\log n*(k*2^m)^3)$ 原文地址:https://www.cnblogs.com/zaq19970105/p/10886953.html