CodeForces-1249D2-Too Many Segments (hard version) -STL+贪心
The only difference between easy and hard versions is constraints.
You are given nn segments on the coordinate axis OXOX. Segments can intersect, lie inside each other and even coincide. The ii-th segment is [li;ri][li;ri] (li≤rili≤ri) and it covers all integer points jj such that li≤j≤rili≤j≤ri.
The integer point is called bad if it is covered by strictly more than kk segments.
Your task is to remove the minimum number of segments so that there are no bad points at all.
Input
The first line of the input contains two integers nn and kk (1≤k≤n≤2⋅1051≤k≤n≤2⋅105) — the number of segments and the maximum number of segments by which each integer point can be covered.
The next nn lines contain segments. The ii-th line contains two integers lili and riri (1≤li≤ri≤2⋅1051≤li≤ri≤2⋅105) — the endpoints of the ii-th segment.
Output
In the first line print one integer mm (0≤m≤n0≤m≤n) — the minimum number of segments you need to remove so that there are no bad points.
In the second line print mm distinct integers p1,p2,…,pmp1,p2,…,pm (1≤pi≤n1≤pi≤n) — indices of segments you remove in any order. If there are multiple answers, you can print any of them.
Examples
7 2
11 11
9 11
7 8
8 9
7 8
9 11
7 9
3
4 6 7
5 1
29 30
30 30
29 29
28 30
30 30
3
1 4 5
6 1
2 3
3 3
2 3
2 2
2 3
2 3
4
1 3 5 6
题意:
给定n个线段的覆盖区间
求最少删除多少个线段可以让覆盖每个点的线段数量<=k
思路:
从前往后找如果大于k
就删掉该点所有线段上 右端点最靠右 的线段
vector<int>v;//相当于数组的作用了
学习到了这个新的用法和容器:
set<pair<int,int> >s;//两个> >中间要加空格隔开
因为set会自动升序排列
把里面每一个元素都看作是pair
则排序是先排pair里的first,再排pair里面的second
比如pair<2,3> pair<2,1> pair<0,6>
排列之后是pair<0,6>,pair<2,1>,pair<2,3>
pair<int,int>
这是泛型
pair是一个键值对
键是int类型,值是int类型
然后这种类型的变量组成一个set,也就是集合
这个集合的变量叫s
codeforces上好像是不太注意格式的。。
#include<bits/stdc++.h>
using namespace std;
const int N=2e5+;
#define inf 0x3f3f3f3f struct node
{
int l;
int r;
int num;
} a[N]; int cmp1(node a,node b)
{
// if(a.r!=b.r)
// return a.r>b.r;
// else
return a.l<b.l;//按左端点从小到大排序
}
//题意:
//给定n个线段的覆盖区间
//求最少删除多少个线段可以让覆盖每个点的线段数量<=k //思路:
//从前往后找如果大于k
//就删掉该点所有线段上 右端点最靠右 的线段 vector<int>v;//相当于数组的作用了 set<pair<int,int> >s;//两个> >中间要加空格隔开
//因为set会自动升序排列
//把里面每一个元素都看作是pair
//则排序是先排pair里的first,再排pair里面的second
//比如pair<2,3> pair<2,1> pair<0,6>
//排列之后是pair<0,6>,pair<2,1>,pair<2,3> //这是泛型
//pair是一个键值对
//键是int类型,值是int类型
//然后这种类型的变量组成一个set,也就是集合
//这个集合的变量叫s //7 2
//11 11
//9 11
//7 8
//8 9
//7 8
//9 11
//7 9 //3
//4 6 7
pair<int,int>p; int main()
{
int n,k;
while(~scanf("%d %d",&n,&k))
{
v.clear();
s.clear();
//p.clear();//不可以
int maxx=-,minn=inf;
for(int i=; i<=n; i++)
{
scanf("%d %d",&a[i].l,&a[i].r);
a[i].num=i;
maxx=max(maxx,a[i].r);//找到最大的右端点
minn=min(minn,a[i].l);//找到最小的左端点
}
sort(a+,a++n,cmp1);////按左端点从小到大排序
int q=;
for(int i=minn; i<=maxx; i++)
{
while(q<=n&&a[q].l<=i)
{
s.insert(make_pair(a[q].r,a[q].num));
q++;
}
while(s.size()&&s.begin()->first<i)
s.erase(s.begin()); //默认排序从小到大
while(s.size()>k)//s里面存的是每一个点被线段覆盖的次数
{
p=*(--s.end());
v.push_back(p.second);
s.erase(p);
}
}
sort(v.begin(),v.end());
cout<<v.size()<<endl;
for(int i=; i<v.size(); i++)
cout<<v[i]<<' ';
// cout<<v[v.size()-1]<<endl;//RT 不知道为啥???
cout<<endl;
}
return ;
}
CodeForces-1249D2-Too Many Segments (hard version) -STL+贪心的更多相关文章
- Codeforces 1108E2 Array and Segments (Hard version) 差分, 暴力
Codeforces 1108E2 E2. Array and Segments (Hard version) Description: The only difference between eas ...
- Codeforces 1108E2 Array and Segments (Hard version)(差分+思维)
题目链接:Array and Segments (Hard version) 题意:给定一个长度为n的序列,m个区间,从m个区间内选择一些区间内的数都减一,使得整个序列的最大值减最小值最大. 题解:利 ...
- Codeforces Round #535 (Div. 3) E2. Array and Segments (Hard version) 【区间更新 线段树】
传送门:http://codeforces.com/contest/1108/problem/E2 E2. Array and Segments (Hard version) time limit p ...
- Codeforces Round #535 E2-Array and Segments (Hard version)
Codeforces Round #535 E2-Array and Segments (Hard version) 题意: 给你一个数列和一些区间,让你选择一些区间(选择的区间中的数都减一), 求最 ...
- Array and Segments (Easy version) CodeForces - 1108E1 (暴力枚举)
The only difference between easy and hard versions is a number of elements in the array. You are giv ...
- 【Codeforces 1108E1】Array and Segments (Easy version)
[链接] 我是链接,点我呀:) [题意] 题意 [题解] 枚举最大值和最小值在什么地方. 显然,只要包含最小值的区间,都让他减少. 因为就算那个区间包含最大值,也无所谓,因为不会让答案变小. 但是那些 ...
- Codeforces 1249 D2. Too Many Segments (hard version)
传送门 贪心 对于第一个不合法的位置,我们显然要通过删除几个覆盖了它的区间来使这个位置合法 显然删右端点更靠右的区间是更优的,所以就考虑优先删右端点靠右的,然后再考虑下一个不合法位置 用一个 $set ...
- codeforces 1249 D2 Too Many Segments (hard version) 贪心+树状数组
题意 给定n个线段,线段可以相交,第\(i\)个线段覆盖的区间为\([l_i,r_i]\),问最少删除多少个线段让覆盖每个点的线段数量小于等于k. 分析 从左往右扫每个点\(x\),若覆盖点\(x\) ...
- Codeforces Round #540 (Div. 3) D1. Coffee and Coursework (Easy version) 【贪心】
任意门:http://codeforces.com/contest/1118/problem/D1 D1. Coffee and Coursework (Easy version) time limi ...
随机推荐
- PIL库,图像处理第三方库
PIL ---> python imaging library 安装需要安装pillow库,包含了21种类,其中Image类是PIL最重要的一个类,可以通过它来处理图像. Python最常用的 ...
- 将日志(Microsoft.Extensions.Logging)添加到.NET Core控制台应用程序
在.NET Core项目中,日志记录是通过依赖项注入进行管理的. 尽管这对于ASP.NET项目效果很好,但在启动Startup.cs中的新项目时,所有这些都会自动创建,而在控制台应用程序中则需要一些配 ...
- J. The Volcano Eruption(圆相交+并查集)
题目链接:https://codeforces.com/gym/101915/problem/J 思路:将所有相交的圆用并查集维护看做一个整体,然后枚举每个整体的左边界和右边界,判断能不能同时覆盖整个 ...
- PHP 图片裁切
PHP CLASS TO CREATE IMAGE THUMBANILS Some years ago, I wrote a PHP class to create image thumbnails ...
- 基于MFC的Media Player播放器的制作(2---导入第三方库和介绍第三方库)
| 版权声明:本文为博主原创文章,未经博主允许不得转载. 这一节我们介绍如何导入类库,和介绍类库的一功能和介绍MFC的一些主要的模块部分.下面是如何导入类库.第一步我们选中 Media Playe ...
- Linux下设置Core文件生成路径及文件名
修改core dump文件路径: 方法1:临时修改: 修改/proc/sys/kernel/core_pattern文件/proc目录本身动态加载每次系统重启都会重新加载因此种方法只能作临时修改/p ...
- Java菜鸟笔记
System.out.println( ); 会在输出完毕后自动换行 System.out.print( ); 在输出完毕后不会自动换行 MyEclipse/Eclipse快捷键: 定位到某一行, ...
- io.lettuce.core.RedisCommandExecutionException: ERR unknown command 'GEOADD'
io.lettuce.core.RedisCommandExecutionException: ERR unknown command 'GEOADD' at io.lettuce.core.Exce ...
- 隐式激活Activity
- ORACLE PL、SQL编程
PL(Procedural Language)过程化的编程语言,是在SQL的基础上增加的部分,如:变量的使用.流程控制等, 重点学习Oracle和MySQL创建存储过程及流程控制的异同. 一.存储过程 ...