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(贪心)的更多相关文章

  1. UVA 10020 Minimal coverage(贪心 + 区间覆盖问题)

     Minimal coverage  The Problem Given several segments of line (int the X axis) with coordinates [Li, ...

  2. uva 10020 Minimal coverage 【贪心】+【区间全然覆盖】

    Minimal coverage The Problem Given several segments of line (int the X axis) with coordinates [Li,Ri ...

  3. 【区间覆盖问题】uva 10020 - Minimal coverage

    可以说是区间覆盖问题的例题... Note: 区间包含+排序扫描: 要求覆盖区间[s, t]; 1.把各区间按照Left从小到大排序,如果区间1的起点大于s,则无解(因为其他区间的左起点更大):否则选 ...

  4. UVa 10020 - Minimal coverage(区间覆盖并贪心)

    Given several segments of line (int the X axis) with coordinates [Li, Ri]. You are to choose the min ...

  5. uva 10020 Minimal coverage

    http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...

  6. ural 1303 Minimal Coverage(贪心)

    链接: http://acm.timus.ru/problem.aspx?space=1&num=1303 按照贪心的思想,每次找到覆盖要求区间左端点时,右端点最大的线段,然后把要求覆盖的区间 ...

  7. Minimal coverage (贪心,最小覆盖)

    题目大意:先确定一个M, 然后输入多组线段的左端和右端的端点坐标,然后让你求出来在所给的线段中能够 把[0, M] 区域完全覆盖完的最少需要的线段数,并输出这些线段的左右端点坐标. 思路分析: 线段区 ...

  8. 贪心 URAL 1303 Minimal Coverage

    题目传送门 /* 题意:最少需要多少条线段能覆盖[0, m]的长度 贪心:首先忽略被其他线段完全覆盖的线段,因为选取更长的更优 接着就是从p=0开始,以p点为标志,选取 (node[i].l < ...

  9. ural 1303 Minimal Coverage【贪心】

    链接: http://acm.timus.ru/problem.aspx?space=1&num=1303 http://acm.hust.edu.cn/vjudge/contest/view ...

随机推荐

  1. 如何保持自己 fork 的项目和原始项目同步

    首先先通过 github 的 web 页面 fork 目标的项目 前提是自己已经设置好了git,并且配置了相应的权限 然后使用git clone命令在本地克隆自己 fork 的项目: git clon ...

  2. runtime笔记一

    一.iOS中_cmd The _cmd variable is a hidden argument passed to every method that is the current selecto ...

  3. 修改/etc/profile导致常用命令不可用的解决办法

    原因:/etc/profile文件修改有误 解决办法: 用/usr/bin/vim /etc/profile进入,进去后修改正确/etc/profile,然后重启机器让该文件生效即可.

  4. 9月19日上午JavaScript数组

    数组 一.定义 1.数组的文字定义 广义上说,数组是相同类型数据的集合.但是对于强类型语言和弱类型语言来说其特点是不一样的.强类型语言数组和集合有以下特点. 数组强类型语言:1.数组里面只能存放相同数 ...

  5. Spring MVC学习笔记——Welcome

    参考: http://blog.csdn.net/hehexiaoyou/article/details/23747617 http://www.codingyun.com/article/47.ht ...

  6. Tomcat web.xml中定义了文件扩展名到MIME类型的对应关系

    Tomcat在返回静态资源时,会根据资源文件的扩展名产生对应的content-type头(也即MIME类型)添加到response header中. 在Tomcat的web.xml规定了扩展名及相应M ...

  7. Python标准库:内置函数hasattr(object, name)

    Python标准库:内置函数hasattr(object, name) 本函数是用来判断对象object的属性(name表示)是否存在.如果属性(name表示)存在,则返回True,否则返回False ...

  8. background属性

    background: url(images/01.jpg) 0 10px; 效果: background: url(images/01.jpg) 0 -10px; 效果: *注释:10px 是网上去 ...

  9. Autofac.Integration.Mvc.Owin分析

    using System; using System.ComponentModel; using System.Diagnostics.CodeAnalysis; using System.Secur ...

  10. wampserver 2.5安装pear win8.1

    集成环境的悲伤啊~ 本来看到pear想试试 结果发现根本没有go-pear.bat 自己的环境 都是 系统win 8.1 php 5.5.12 mysql  5.6.17 apache 2.4.9   ...