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=,f=; char ch=getchar();
while(ch<''||ch>'') { if(ch=='-') f=-; ch=getchar(); }
while(ch>=''&&ch<='') { x=(x<<)+(x<<)+(ch^); ch=getchar(); }
return x*f;
}
const int N=2e5+;
int n,m;
struct dat {
int r,id;
dat (int _r=,int _id=) { 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=;
for(int i=;i<=n;i++)
a=read(),b=read(),
mx=max(mx,b),
V[a].push_back(dat(b,i));
int ans=;
for(int i=;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 ;
}
Codeforces 1249 D2. Too Many Segments (hard version)的更多相关文章
- codeforces 1249 D2 Too Many Segments (hard version) 贪心+树状数组
题意 给定n个线段,线段可以相交,第\(i\)个线段覆盖的区间为\([l_i,r_i]\),问最少删除多少个线段让覆盖每个点的线段数量小于等于k. 分析 从左往右扫每个点\(x\),若覆盖点\(x\) ...
- Codeforces Round #535 E2-Array and Segments (Hard version)
Codeforces Round #535 E2-Array and Segments (Hard version) 题意: 给你一个数列和一些区间,让你选择一些区间(选择的区间中的数都减一), 求最 ...
- 【Codeforces 1108E1】Array and Segments (Easy version)
[链接] 我是链接,点我呀:) [题意] 题意 [题解] 枚举最大值和最小值在什么地方. 显然,只要包含最小值的区间,都让他减少. 因为就算那个区间包含最大值,也无所谓,因为不会让答案变小. 但是那些 ...
- codeforces 1249D1/D2 Too Many Segments (贪心)
(点击此处查看原题) 题意说明 有n个区间,第i个区间覆盖范围[li,ri]内所有点,问删除最少哪些区间,使得所有点被区间覆盖的次数少于等于k次 解题思路 看到这个题的时候,觉得和开关(反转)问题很像 ...
- Codeforces 1108E2 Array and Segments (Hard version) 差分, 暴力
Codeforces 1108E2 E2. Array and Segments (Hard version) Description: The only difference between eas ...
- 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 1108E2 Array and Segments (Hard version)(差分+思维)
题目链接:Array and Segments (Hard version) 题意:给定一个长度为n的序列,m个区间,从m个区间内选择一些区间内的数都减一,使得整个序列的最大值减最小值最大. 题解:利 ...
- D2. Remove the Substring (hard version)(思维 )
D2. Remove the Substring (hard version) time limit per test 2 seconds memory limit per test 256 mega ...
- D2. Equalizing by Division (hard version)
D2. Equalizing by Division (hard version) 涉及下标运算一定要注意下标是否越界!!! 思路,暴力判断以每个数字为到达态最小花费 #include<bits ...
随机推荐
- 20175313 张黎仙《获奖感想与Java阶段性学习总结》
一.获奖感想 很荣幸能够成为为数不多的小黄衫获得者之一,这是对我近一学期以来学习成果的肯定,也激励着我更加努力学习. 首先我要感谢的人就是娄嘉鹏老师.我曾在师生关系中提到,我认为的好老师的特点之一是: ...
- 对请求数据的格式化 方案 Spring Cloud Gateway features:
对请求数据的格式化 例如 {body:{}}--->{data:{}} 执行阶段概念 · OpenResty最佳实践 · 看云 https://www.kancloud.cn/kancloud/ ...
- mysql中的sql-mode导致的datetime类型字段不能为0000
问题描述: 在执行建表语句的时候,出现invalid default datetime value '0000-00-00 00:00:00',从字面意思看,就是不合法的默认值'0000-00-00 ...
- iOS12 中的后台下载与上传
严格意义上来说,iOS并不能像Android一样,真的在后台开启一个下载Service,一直下载.但是它可以进行在系统允许范围内的后台上传和下载. 当使用 NSURLSessionConfigurat ...
- JavaScript Functional Programming:声明式与命令式
函数式编程属于声明式编程(declarative programming)的范畴,经常跟声明式编程一块儿讨论的是命令式编程(imperative programming),因为它们是两种不太一样的风格 ...
- HBase里配置SNAPPY压缩以后regionserver启动不了的问题
配置了HBase的SNAPPY压缩以后,出现regionserver启动不了的问题.分析应该是属性配置错了! 官网上的是:<name>hbase.regionserver.codecs&l ...
- logging日志管理
日志:记录系统运行时的信息的(调试信息和异常信息) 基本用法: import logging logging.debug("这是一条调试信息") logging.info(&quo ...
- PAT 甲级 1048 Find Coins (25 分)(较简单,开个数组记录一下即可)
1048 Find Coins (25 分) Eva loves to collect coins from all over the universe, including some other ...
- keepalived两台机器同时出现vip问题
配置文件: 主:192.168.1.14 ! Configuration File for keepalived global_defs { script_user root enable_scrip ...
- Release报错Debug无错
代码在Release模式下会crash,Debug模式下可以运行,最后定位到原因 for (size_t j = 0; j < ids.size()-1; ++j) { } 发现问题是Relea ...