uva.10020 Minimal coverage(贪心)
10020
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
贪心是要讲规则的,这道题的规则就是:首先要覆盖x=0这个点,那么选谁呢?自然选L<=0 && R 尽可能大的区间[l0,r0];然后下一个要覆盖的是x=ro,采取同样的策略……直到某一轮的选出的区间覆盖了x=m。
#include<stdio.h>
#include<string.h>
#include<algorithm>
const int inf = 0x3f3f3f3f ;
int T ;
int m ;
int path[ + ] ;
struct node
{
int l , r ;
}e[ + ]; bool cmp (node a , node b)
{
return a.l < b.l ;
} int main ()
{
//freopen ("a.txt" , "r" , stdin ) ;
scanf ("%d" , &T) ;
int cas = ;
while (T --) {
if (cas != ) puts ("") ;
cas ++ ;
scanf ("%d" , &m ) ;
int n = ;
while () {
scanf ("%d%d" , &e[n].l , &e[n].r ) ;
if (e[n].l == && e[n].r == ) break ;
n ++ ;
}
//printf ("n = %d\n" , n );
std::sort (e , e + n , cmp) ;
bool flag = ;
int cur = , id = - , maxn = -inf ;
int tot = ;
for (int i = ; i < n ; i ++) {
for (int j = ; j < n ; j ++) {
// printf ("l = %d , r = %d , cur = %d\n" , e[j].l , e[j].r , cur ) ;
if (e[j].r > cur && e[j].l <= cur ) {
if (e[j].r > maxn) {
id = j ;
maxn = e[j].r ;
}
}
}
if (id == -) {
flag = ;
break ;
}
path[tot ++] = id ;
cur = e[id].r ;
id = - ; maxn = -inf ;
if (cur >= m) break ;
}
if (flag) puts ("") ;
else {
printf ("%d\n" , tot ) ;
for (int i = ; i < tot ; i ++) {
printf ("%d %d\n" , e[path[i]].l , e[path[i]].r ) ;
}
}
}
return ;
}
uva.10020 Minimal coverage(贪心)的更多相关文章
- UVA 10020 Minimal coverage(贪心 + 区间覆盖问题)
Minimal coverage The Problem Given several segments of line (int the X axis) with coordinates [Li, ...
- uva 10020 Minimal coverage 【贪心】+【区间全然覆盖】
Minimal coverage The Problem Given several segments of line (int the X axis) with coordinates [Li,Ri ...
- 【区间覆盖问题】uva 10020 - Minimal coverage
可以说是区间覆盖问题的例题... Note: 区间包含+排序扫描: 要求覆盖区间[s, t]; 1.把各区间按照Left从小到大排序,如果区间1的起点大于s,则无解(因为其他区间的左起点更大):否则选 ...
- UVa 10020 - Minimal coverage(区间覆盖并贪心)
Given several segments of line (int the X axis) with coordinates [Li, Ri]. You are to choose the min ...
- uva 10020 Minimal coverage
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...
- ural 1303 Minimal Coverage(贪心)
链接: http://acm.timus.ru/problem.aspx?space=1&num=1303 按照贪心的思想,每次找到覆盖要求区间左端点时,右端点最大的线段,然后把要求覆盖的区间 ...
- Minimal coverage (贪心,最小覆盖)
题目大意:先确定一个M, 然后输入多组线段的左端和右端的端点坐标,然后让你求出来在所给的线段中能够 把[0, M] 区域完全覆盖完的最少需要的线段数,并输出这些线段的左右端点坐标. 思路分析: 线段区 ...
- 贪心 URAL 1303 Minimal Coverage
题目传送门 /* 题意:最少需要多少条线段能覆盖[0, m]的长度 贪心:首先忽略被其他线段完全覆盖的线段,因为选取更长的更优 接着就是从p=0开始,以p点为标志,选取 (node[i].l < ...
- ural 1303 Minimal Coverage【贪心】
链接: http://acm.timus.ru/problem.aspx?space=1&num=1303 http://acm.hust.edu.cn/vjudge/contest/view ...
随机推荐
- lbs(查看附近的人),看看社交软件如何实现查看附近的人
最近在做一款移动端棋牌游戏,为了进一步提高用户体验.拉近玩家的距离,我们决定在游戏中加入好友功能,而对于体验好友功能的玩家来说,要是玩牌的时候可以看看附近都有谁在玩牌,跟他们交流交流玩牌心得什么的无疑 ...
- Maven中配置maven-compiler-plugin插件
这个插件就如同名字所显示的这样,用来编译源代码的. 加载第三方包 <dependency> <groupId>cn.eshore.bnet</groupId> &l ...
- 提高SQL的查询效率
1.对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 order by 涉及的列上建立索引. 2.应尽量避免在 where 子句中对字段进行 null 值判断,否则将导致引擎放弃使 ...
- django redirect的几种方式
You can use the redirect() function in a number of ways. By passing some object; that object’s get_a ...
- iOS自动处理键盘事件的第三方库:IQKeyboardManager
我们写界面要考虑很多用户体验问题,键盘事件的响应就是比较麻烦的一种.我们需要监听键盘事件,考虑点击背景收起键盘.考虑键盘遮挡输入框问题等等,而且每个界面都要做这么一套.这个库帮我们解决了这个事情. 这 ...
- 如何判断ios设备中是否安装了某款应用
URL Schemes关键字研究一下即可 常见得URL Schemes见http://www.cnblogs.com/huangzs/p/4491286.html if ([[UIApplicatio ...
- VS2012旗舰版接选择调试 出现了这样一个错误
问题: 解决: 项目-右键-属性,勾选如下配置试试
- iOS - 装饰对象
1.设计模式原则 多组合,少继承 类对拓展开放,对修改关闭 派生的子类接口是在编译时就静态决定的,而所有子类都会继承到相同的接口.然而,利用组合或者说装饰模式来拓展抽象类的接口,就可以在运行时动态的进 ...
- iOS - 如何自动播放H5中的音频
场景:iOS端设备,App页面跳转到H5产品介绍,背景音乐无法播放.(为什么不能自动播放,因该是iPhone人性化设定吧~) 加载H5用UIWebView空间: 代码: CGRect rect = s ...
- 网络存储(三)之ISCSI搭建的入门
搭建iscsi 我们就拿两台linux服务器来做吧, 服务器系统均为CentOs6.6 64位的,信息如下 IP 安装的软件 192.168.22.142 iscsi target端:scsi-tar ...