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 ...
随机推荐
- 关于I2C和SPI总线协议【转】
关于I2C和SPI总线协议 IICvs SPI 现今,在低端数字通信应用领域,我们随处可见IIC (Inter-Integrated Circuit) 和 SPI (Serial Peripheral ...
- 个人笔记 - MATLAB
1.教程 2.基本知识 2.1 帮助文档设置成中文:链接1 2.2 多行注释: 链接1 2.3 MATLAB基本数据类型: 链接1 链接2 2.4 matlab中的 ndims(a).length( ...
- python运行时禁止生成pyc文件
方法 在环境变量文件~/.bashrc中添加 export PYTHONDONTWRITEBYTECODE=False source ~/.bashrc加载即可 如何从项目中删除所有.pyc文件 fi ...
- 02 java语言基础
常量:字面值常量(字符串,字符,整数,小数,布尔,null),自定义常量,''这个不是字符常量,""这个是字符串常量 进制: 02.01_Java语言基础(常量的概述和使用) A: ...
- [已解决]报错: Python Scrapy - service_identity(opentype) not working and cannot install
解决:更新安装service_identity pip3 install service_identity --force --upgrade
- Spring 使用@Async出现循环依赖Bean报错的解决方案
初现端倪 Caused by:org.springframework.beans.factory.BeanCurrentlyInCreationException: Errorcreating bea ...
- 解决Redhat yum出现This system is not registered with RHN的方案
最近博主在学习Linux,菜鸟级别的的选手连装个Chrome都觉得难,悲了个催的……百度了很多教程,大多是类似的.博主的配置是在VM8下搭建的RHEL5.3 (Tikanga)版本,不知道什么原因,每 ...
- 2018-8-9-win-消息
title author date CreateTime categories win 消息 lindexi 2018-8-9 15:35:4 +0800 2018-2-13 17:23:3 +080 ...
- Linux入门命令
####常用的目录 /主目录 ① /root:存放启动Linux时启动的核心文件,包括一些连接文件以及镜像 ② /bin:binary的缩写,存放经常使用的命令 ③ /dev:Device(设备)的缩 ...
- js 实现弹出层效果
代码: <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <tit ...