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. AngularJs ngChange、ngChecked、ngClick、ngDblclick

    ngChange 当用户更改输入时,执行给定的表达式.表达式是立即进行执行的,这个和javascript的onChange事件的只有在触发事件的变化结束的时候执行不同. 格式:ng-change=”v ...

  2. PHP之:多图上传

    撰写日期:2016-6-30 15:17:35 Thursday 参考 http://a3147972.blog.51cto.com/2366547/1381136 (08-05ThinkPHP+sw ...

  3. PHP之:析构函数

    如何正确理解PHP析构函数 参考文献:http://developer.51cto.com/art/200912/167023.htm 初次学习PHP语言的朋友们可能对PHP析构函数有些不太了解.我们 ...

  4. 【Alpha版本】冲刺-Day8

    队伍:606notconnected 会议时间:11月16日 会议总结 张斯巍(433) 今天安排:回收站界面设计 完成度:90% 明天计划:关注界面设计 遇到的问题:无 感想:有时候自己设计的队友说 ...

  5. 9.25 DOM作业

    一<style type="text/css">*{margin:0px auto; padding:0px; font-family:微软雅黑; font-size: ...

  6. windows系统下安装MySQL

    可以运行在本地windows版本的MySQL数据库程 序自从3.21版以后已经可以从MySQL AB公司获得,而且 MYSQL每日的下载百分比非常大.这部分描述在windows上安装MySQL的过程. ...

  7. context.Request.Files为NULL问题 在实现图片上传功能的时候出现在ashx等处理页面出现context.Request.Files为NULL异常,有几点需要注意:

    .在客户端可以将form用submit提交,如下: <%@ Page Language="C#" AutoEventWireup="true" CodeF ...

  8. JQuery------判断拥有某个class或id的div是否存在

    if ($(".Btn,#Show").length > 0) { alert("存在"); } else { alert("不存在" ...

  9. phpspidercookie

    <?php /** * Created by PhpStorm. * User: brady * Date: 2016/12/9 * Time: 17:32 */ ini_set("m ...

  10. ntp服务器搭建与客户端设置

    服务器对时间要求非常严格,linux服务器文件的生成,日志,以及数据库的连接都是根据时间的逻辑 进行任务处理,如果时间不同步,那么,处理的任务,以及出问题的分析日志,时间不对,很难分析 直接更改lin ...