博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Minimal coverage(贪心 区间覆盖)
阅读量:4136 次
发布时间:2019-05-25

本文共 1756 字,大约阅读时间需要 5 分钟。

Given several segments of line (int the X axis) with coordinates [Li

, Ri
]. You are to choose the minimal
amount of them, such they would completely cover the segment [0, M].
Input
The first line is the number of test cases, followed by a blank line.
Each test case in the input should contains an integer M (1 ≤ M ≤ 5000), followed by pairs “Li Ri”
(|Li
|, |Ri
| ≤ 50000, i ≤ 100000), each on a separate line. Each test case of input is terminated by pair
‘0 0’.
Each test case will be separated by a single line.
Output
For each test case, in the first line of output your programm should print the minimal number of line
segments which can cover segment [0, M]. In the following lines, the coordinates of segments, sorted
by their left end (Li), should be printed in the same format as in the input. Pair ‘0 0’ should not be
printed. If [0, M] can not be covered by given line segments, your programm should print ‘0’ (without
quotes).
Print a blank line between the outputs for two consecutive test cases.
Sample Input
2
1
-1 0
-5 -3
2 5
0 0
1
-1 0
0 1
0 0
Sample Output
0
1
0 1

给你一个数m,然后给你几个区间,让你求这几个区间中能够覆盖[0,m]的最小个数.

明显的贪心.我们按着结尾的大小,由大到小去将区间排序…然后一个一个的去查询,并且随时更新区间.
代码如下:

#include
#include
#include
#include
#include
using namespace std;struct node{ int start; int end;}p[100005],q[100010];int cmp(const node &a,const node &b){ return a.end>b.end;}int s=0;int e;int main(){ int t; scanf("%d",&t); while(t--) { s=0; scanf("%d",&e); int cnt=0; while(scanf("%d%d",&p[cnt].start,&p[cnt].end),p[cnt].end+p[cnt].start) { cnt++; } sort(p,p+cnt,cmp); int ant=0; int i; while(s
s) { s=p[i].end; q[ant++]=p[i]; break; } } if(i==cnt) break; } if(s

努力加油a啊,(o)/~

转载地址:http://vkxvi.baihongyu.com/

你可能感兴趣的文章
HDU 1547 Bubble Shooter(BFS蔓延模拟)
查看>>
HDU 1548 A strange lift(递归模拟标记)
查看>>
(转)HDU 1584 蜘蛛牌(搜索)
查看>>
HDU 1583 DNA Assembly(暴力模拟)
查看>>
HDU 1579 Function Run Fun(记忆化搜索)
查看>>
HDU 1574 RP问题(01背包变形)
查看>>
HDU 5246 超级赛亚ACMer(贪心模拟)
查看>>
HDU 5247 找连续数(暴力)
查看>>
HDU 5256 序列变换(最长上升子序列)
查看>>
HDU 4841 圆桌问题(约瑟夫环队列模拟)
查看>>
HDU 1172 猜数字(暴力)
查看>>
HDU 1495 非常可乐(bfs+标记)
查看>>
HDU 2569 彼岸(递推)
查看>>
HDU 4503 湫湫系列故事——植树节(组合概率)
查看>>
HDU 4500 小Q系列故事——屌丝的逆袭(模拟枚举排序)
查看>>
HDU 4501 小明系列故事——买年货(三重背包)
查看>>
Php连接mysql实现注册信息和文件上传
查看>>
HDU 5253 连接的管道(最小生成树-Kruskal+并查集)
查看>>
HDU 5249 KPI(set+queue+二分查找)(转载)
查看>>
HDU 4502 吉哥系列故事——临时工计划(dp)
查看>>