(点击此处查看原题)

题意说明

有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 ;
}

最新文章

  1. Boost.Python简介
  2. Android中图像变换Matrix的原理、代码验证和应用(一)
  3. paramiko模块使用
  4. 【亲述】Uber容错设计与多机房容灾方案 - 高可用架构系列
  5. 线段树或树状数组---Flowers
  6. ruby开发过程中的小总结
  7. OD hit跟踪 run跟踪使用问题
  8. os模块 关于路径问题使用
  9. cursor详解
  10. List小结
  11. HDOJ2002计算球体积
  12. hdu 5305Friends
  13. 大数据Lambda架构
  14. VMware: linux起步提示 memory for crashkernel(0*0 to 0*0)not within permissible
  15. Immutable(不可变)集合
  16. NOIP2015题解
  17. 动车上的书摘-java网络 连接服务器
  18. Web Penetration Testing w3af fierce
  19. 新安装和已安装nginx如何添加未编译安装模块/补丁
  20. Struts2基本使用(二)--配置文件简述

热门文章

  1. vscode设置VUE eslint开发环境
  2. dubbo——高可用性
  3. 注解之 @RestController 和 @RequestMapping
  4. CF1200C
  5. elasticsearch _mapping api
  6. strom部署问题
  7. java并发编程--第一章并发编程的挑战
  8. php+mysql模糊查询功能
  9. [Scikit-learn] 1.4 Support Vector Classification
  10. JAVA 基础编程练习题48 【程序 48 加密】