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 ...
随机推荐
- 【消息中间件】kafka
一.kafka整体架构 kafka是一个发布订阅模式的消息队列,生产者和消费者是多对多的关系,将发送者与接收者真正解耦: 生产者将消息发送到broker: 消费者采用拉(pull)模式订阅并消费消息: ...
- Ajax,ajax封装
/** * Created by liyinghao on 2016/8/23. */ /*仿jQuery中的ajax方法,简单版实现;封装ajax的工具函数*/ /* * 1 请求方式 type g ...
- 设置 Quartus II 的仿真时间大于 1us
Quartus II 仿真的默认时长是 1us. 设置时钟时看到 End time 想修改时长,把默认的 1us 改成 10us. 然后提示 End time 不合法.(只能设置为 0 到 1us) ...
- CSRF如何防御
总结网上所说,细细的归纳下 CSRF利用的时网站对用户网页浏览器的信任.在受害人不知情的情况下以 受害人的名义伪造请求发送给攻击者的站点. 1.首先XSS漏洞先防护好(一般是通过过滤器更改特殊字符) ...
- 专业解决 MySQL 查询速度慢与性能差!
Java技术栈 ,一般把连接数设置得大一些). 并发量:同一时刻数据库服务器处理的请求数量 3.超高的 CPU使用率:CPU资源耗尽出现宕机. 4.磁盘 IO:磁盘 IO性能突然下降.大量消耗磁盘性能 ...
- MySQL数据库安装与启动(Linux)
1.用yum安装 用root权限打开命令行界面,执行以下yum指令: yum安装MySQL yum install mysql mysql-server mysql-devel -y 在最终提示Com ...
- Python-装饰器的进阶 小知识点
⼀. 通⽤装饰器的回顾 开闭原则: 对增加功能开放. 对修改代码封闭 装饰器的作⽤: 在不改变原有代码的基础上给⼀个函数增加功能 通⽤装饰器的写法: def wrapper(fn): def inne ...
- freemarker在web应用项目的使用
FreeMarker是一个用Java语言编写的模板引擎,它基于模板来生成文本输出.FreeMarker与Web容器无关,即在Web运行时,它并不知道Servlet或HTTP.它不仅可以用作表现层的实现 ...
- HTTP、HTTPS等常用服务的默认端口号
口号标识了一个主机上进行通信的不同的应用程序. 1.HTTP协议代理服务器常用端口号:80/8080/3128/8081/9098 2.SOCKS代理协议服务器常用端口号:1080 3.FTP(文件传 ...
- vue 父子组件数据双向绑定
父组件 <find-search v-model="searchshow"></find-search> 子组件 props: { value: { typ ...