(点击此处查看原题)

题意说明

有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)输出删除的区间个数和区间编号

代码区

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstdlib>
  4. #include<algorithm>
  5. #include<cmath>
  6. #include<cstring>
  7. #include<queue>
  8. #include<string>
  9. #include<fstream>
  10. #include<vector>
  11. #include<stack>
  12. #include <map>
  13. #include<set>
  14. #include <iomanip>
  15.  
  16. #define bug cout << "**********" << endl
  17. #define show(x, y) cout<<"["<<x<<","<<y<<"] "
  18. #define LOCAL = 1;
  19. using namespace std;
  20. typedef long long ll;
  21. const ll inf = 1e18 + ;
  22. const int Max = 2e5 + ;
  23.  
  24. struct Node
  25. {
  26. int r;
  27. int id;
  28. bool operator<(const Node &node) const
  29. {
  30. if (r != node.r)
  31. return r < node.r;
  32. return id < node.id;
  33. }
  34. };
  35.  
  36. int n, k;
  37. int sum = ;
  38. vector<Node> node[Max];
  39. vector<int> num;
  40. set<Node> s;
  41.  
  42. int main()
  43. {
  44. #ifdef LOCAL
  45. // freopen("input.txt", "r", stdin);
  46. // freopen("output.txt", "w", stdout);
  47. #endif
  48. sum = ;
  49. scanf("%d%d", &n, &k);
  50. for (int i = , l, r; i <= n; i++)
  51. {
  52. scanf("%d%d", &l, &r);
  53. Node now;
  54. now.r = r;
  55. now.id = i;
  56. sum = max(sum, r);
  57. node[l].push_back(now);
  58. }
  59. for (int i = ; i <= sum; i++)
  60. {
  61. while (!s.empty() && (*s.begin()).r < i)
  62. s.erase(s.begin());
  63. for (int j = ; j < node[i].size(); j++)
  64. s.insert(node[i][j]);
  65. while (s.size() > k)
  66. {
  67. num.push_back((*s.rbegin()).id);
  68. s.erase(*s.rbegin());
  69. }
  70. }
  71. sort(num.begin(), num.end());
  72. printf("%d\n",num.size());
  73. for (int i = ; i < num.size(); i++)
  74. printf("%d%c", num[i], i == num.size() - ? '\n' : ' ');
  75. return ;
  76. }

codeforces 1249D1/D2 Too Many Segments (贪心)的更多相关文章

  1. codeforces 1249 D2 Too Many Segments (hard version) 贪心+树状数组

    题意 给定n个线段,线段可以相交,第\(i\)个线段覆盖的区间为\([l_i,r_i]\),问最少删除多少个线段让覆盖每个点的线段数量小于等于k. 分析 从左往右扫每个点\(x\),若覆盖点\(x\) ...

  2. Codeforces 1249 D2. Too Many Segments (hard version)

    传送门 贪心 对于第一个不合法的位置,我们显然要通过删除几个覆盖了它的区间来使这个位置合法 显然删右端点更靠右的区间是更优的,所以就考虑优先删右端点靠右的,然后再考虑下一个不合法位置 用一个 $set ...

  3. Codeforces 437C The Child and Toy(贪心)

    题目连接:Codeforces 437C  The Child and Toy 贪心,每条绳子都是须要割断的,那就先割断最大值相应的那部分周围的绳子. #include <iostream> ...

  4. Codeforces Round #546 (Div. 2) D 贪心 + 思维

    https://codeforces.com/contest/1136/problem/D 贪心 + 思维 题意 你面前有一个队列,加上你有n个人(n<=3e5),有m(m<=个交换法则, ...

  5. Codeforces Round #535 E2-Array and Segments (Hard version)

    Codeforces Round #535 E2-Array and Segments (Hard version) 题意: 给你一个数列和一些区间,让你选择一些区间(选择的区间中的数都减一), 求最 ...

  6. Codeforces Round #595 (Div. 3)D1D2 贪心 STL

    一道用STL的贪心,正好可以用来学习使用STL库 题目大意:给出n条可以内含,相交,分离的线段,如果重叠条数超过k次则为坏点,n,k<2e5 所以我们贪心的想我们从左往右遍历,如果重合部分条数超 ...

  7. 【Codeforces 738D】Sea Battle(贪心)

    http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...

  8. Codeforces Educational Codeforces Round 3 C. Load Balancing 贪心

    C. Load Balancing 题目连接: http://www.codeforces.com/contest/609/problem/C Description In the school co ...

  9. Codeforces Testing Round #12 B. Restaurant 贪心

    B. Restaurant Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/597/problem ...

随机推荐

  1. python爬虫demo01

    python爬虫demo01 1 import requests, json, time, sys 2 from bs4 import BeautifulSoup 3 from contextlib ...

  2. redis数据结构有哪些

    1.String 可以是字符串,整数或者浮点数,对整个字符串或者字符串中的一部分执行操作,对整个整数或者浮点执行自增(increment)或者自减(decrement)操作. 2.list 一个链表, ...

  3. Java ExecutorService四种线程池及自定义ThreadPoolExecutor机制

    一.Java 线程池 Java通过Executors提供四种线程池,分别为:1.newCachedThreadPool:创建一个可缓存线程池,如果线程池长度超过处理需要,可灵活回收空闲线程,若无可回收 ...

  4. Windows系统配置Redis密码

    1.首先停止Redis服务,直接关掉Redis或者打开任务管理器查看有没有redis-server进程,有的话选中并结束任务. 2.打开配置文件redis.windows.conf和redis.win ...

  5. fastadmin 后台管理中,权限设置,不同管理员,显示不同的数据

    1.https://doc.fastadmin.net/docs/controller.html

  6. vue实现购物清单列表添加删除

    vue实现购物清单列表添加删除 一.总结 一句话总结: 基础的v-model操作,以及数组的添加(push)删除(splice)操作 1.checkbox可以绑定数组,也可以直接绑定值? 绑定数组就是 ...

  7. Linux 之Shell for循环

    @代表所有参数所以如果后面跟上echo $v你会发现他会一次显示user userdebug eng $poo -le ${#prodlist[@]} 这句话是说 $poo小于等于prodlist中的 ...

  8. 《你不知道的JavaScript(上)》笔记——函数作用域和块作用域

    关于函数声明:如果 function 是声明中的第一个词, 那么就是一个函数声明, 否则就是一个函数表达式.例如匿名函数这种形式,函数会被当作函数表达式而不是一个标准的函数声明来处理. (functi ...

  9. windows 10中的ubuntu子系统安装桌面环境的方法

    windows 10中的ubuntu子系统安装桌面环境的方法 (How to install Ubuntu-desktop in windows 10 Subsystem for Linux) 转载 ...

  10. MySql数据库的下载和安装卸载

    下载:到mysql官网下载 卸载 1.找到mysql安装目录下的my.ini文件 2.找到配置项datadir datadir="D:/develop /MySQL/MySQL Server ...