codeforces 1249D1/D2 Too Many Segments (贪心)
题意说明
有n个区间,第i个区间覆盖范围[li,ri]内所有点,问删除最少哪些区间,使得所有点被区间覆盖的次数少于等于k次
解题思路
看到这个题的时候,觉得和开关(反转)问题很像,从左到右,每次尽量满足当前点需要满足的条件,二者的区别在于这个题目对同一个点的操作次数可能不止一次
这时候,我们就需要考虑这样的问题:如何让当前点x满足条件的同时,让右边的点x+1,x+2,...删除最少的区间以满足条件,答案很显然,我们每次删除覆盖x的区间中ri最大者,直到覆盖当前点x的区间数小于等于k,这样就可以保证删除最少的区间满足条件了
具体实现:
1)统计出以每个点x为左端点的区间,用vector记录
2)因为同一个区间可以覆盖多个点,即覆盖x的区间可能同时覆盖了x+1,x+2...等等,因此用set记录下覆盖当前点的所有区间,这样可以省去大量时间
3)若set中存储的区间个数大于k个,每次删除这些区间中右端点最大者(利用set自动排序的特点,我们对所有区间按右端点排序的话,可以很快地找到set中右端点最大者),并记录下来,直到set中的存储的区间个数小于等于k
4)输出删除的区间个数和区间编号
代码区
- #include<iostream>
- #include<cstdio>
- #include<cstdlib>
- #include<algorithm>
- #include<cmath>
- #include<cstring>
- #include<queue>
- #include<string>
- #include<fstream>
- #include<vector>
- #include<stack>
- #include <map>
- #include<set>
- #include <iomanip>
- #define bug cout << "**********" << endl
- #define show(x, y) cout<<"["<<x<<","<<y<<"] "
- #define LOCAL = 1;
- using namespace std;
- typedef long long ll;
- const ll inf = 1e18 + ;
- const int Max = 2e5 + ;
- struct Node
- {
- int r;
- int id;
- bool operator<(const Node &node) const
- {
- if (r != node.r)
- return r < node.r;
- return id < node.id;
- }
- };
- int n, k;
- int sum = ;
- vector<Node> node[Max];
- vector<int> num;
- set<Node> s;
- int main()
- {
- #ifdef LOCAL
- // freopen("input.txt", "r", stdin);
- // freopen("output.txt", "w", stdout);
- #endif
- sum = ;
- scanf("%d%d", &n, &k);
- for (int i = , l, r; i <= n; i++)
- {
- scanf("%d%d", &l, &r);
- Node now;
- now.r = r;
- now.id = i;
- sum = max(sum, r);
- node[l].push_back(now);
- }
- for (int i = ; i <= sum; i++)
- {
- while (!s.empty() && (*s.begin()).r < i)
- s.erase(s.begin());
- for (int j = ; j < node[i].size(); j++)
- s.insert(node[i][j]);
- while (s.size() > k)
- {
- num.push_back((*s.rbegin()).id);
- s.erase(*s.rbegin());
- }
- }
- sort(num.begin(), num.end());
- printf("%d\n",num.size());
- for (int i = ; i < num.size(); i++)
- printf("%d%c", num[i], i == num.size() - ? '\n' : ' ');
- return ;
- }
codeforces 1249D1/D2 Too Many Segments (贪心)的更多相关文章
- codeforces 1249 D2 Too Many Segments (hard version) 贪心+树状数组
题意 给定n个线段,线段可以相交,第\(i\)个线段覆盖的区间为\([l_i,r_i]\),问最少删除多少个线段让覆盖每个点的线段数量小于等于k. 分析 从左往右扫每个点\(x\),若覆盖点\(x\) ...
- Codeforces 1249 D2. Too Many Segments (hard version)
传送门 贪心 对于第一个不合法的位置,我们显然要通过删除几个覆盖了它的区间来使这个位置合法 显然删右端点更靠右的区间是更优的,所以就考虑优先删右端点靠右的,然后再考虑下一个不合法位置 用一个 $set ...
- Codeforces 437C The Child and Toy(贪心)
题目连接:Codeforces 437C The Child and Toy 贪心,每条绳子都是须要割断的,那就先割断最大值相应的那部分周围的绳子. #include <iostream> ...
- Codeforces Round #546 (Div. 2) D 贪心 + 思维
https://codeforces.com/contest/1136/problem/D 贪心 + 思维 题意 你面前有一个队列,加上你有n个人(n<=3e5),有m(m<=个交换法则, ...
- Codeforces Round #535 E2-Array and Segments (Hard version)
Codeforces Round #535 E2-Array and Segments (Hard version) 题意: 给你一个数列和一些区间,让你选择一些区间(选择的区间中的数都减一), 求最 ...
- Codeforces Round #595 (Div. 3)D1D2 贪心 STL
一道用STL的贪心,正好可以用来学习使用STL库 题目大意:给出n条可以内含,相交,分离的线段,如果重叠条数超过k次则为坏点,n,k<2e5 所以我们贪心的想我们从左往右遍历,如果重合部分条数超 ...
- 【Codeforces 738D】Sea Battle(贪心)
http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...
- Codeforces Educational Codeforces Round 3 C. Load Balancing 贪心
C. Load Balancing 题目连接: http://www.codeforces.com/contest/609/problem/C Description In the school co ...
- Codeforces Testing Round #12 B. Restaurant 贪心
B. Restaurant Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/597/problem ...
随机推荐
- python爬虫demo01
python爬虫demo01 1 import requests, json, time, sys 2 from bs4 import BeautifulSoup 3 from contextlib ...
- redis数据结构有哪些
1.String 可以是字符串,整数或者浮点数,对整个字符串或者字符串中的一部分执行操作,对整个整数或者浮点执行自增(increment)或者自减(decrement)操作. 2.list 一个链表, ...
- Java ExecutorService四种线程池及自定义ThreadPoolExecutor机制
一.Java 线程池 Java通过Executors提供四种线程池,分别为:1.newCachedThreadPool:创建一个可缓存线程池,如果线程池长度超过处理需要,可灵活回收空闲线程,若无可回收 ...
- Windows系统配置Redis密码
1.首先停止Redis服务,直接关掉Redis或者打开任务管理器查看有没有redis-server进程,有的话选中并结束任务. 2.打开配置文件redis.windows.conf和redis.win ...
- fastadmin 后台管理中,权限设置,不同管理员,显示不同的数据
1.https://doc.fastadmin.net/docs/controller.html
- vue实现购物清单列表添加删除
vue实现购物清单列表添加删除 一.总结 一句话总结: 基础的v-model操作,以及数组的添加(push)删除(splice)操作 1.checkbox可以绑定数组,也可以直接绑定值? 绑定数组就是 ...
- Linux 之Shell for循环
@代表所有参数所以如果后面跟上echo $v你会发现他会一次显示user userdebug eng $poo -le ${#prodlist[@]} 这句话是说 $poo小于等于prodlist中的 ...
- 《你不知道的JavaScript(上)》笔记——函数作用域和块作用域
关于函数声明:如果 function 是声明中的第一个词, 那么就是一个函数声明, 否则就是一个函数表达式.例如匿名函数这种形式,函数会被当作函数表达式而不是一个标准的函数声明来处理. (functi ...
- windows 10中的ubuntu子系统安装桌面环境的方法
windows 10中的ubuntu子系统安装桌面环境的方法 (How to install Ubuntu-desktop in windows 10 Subsystem for Linux) 转载 ...
- MySql数据库的下载和安装卸载
下载:到mysql官网下载 卸载 1.找到mysql安装目录下的my.ini文件 2.找到配置项datadir datadir="D:/develop /MySQL/MySQL Server ...