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. 加州大学伯克利分校Stat2.3x Inference 统计推断学习笔记: Section 3 One-sample and two-sample tests

    Stat2.3x Inference(统计推断)课程由加州大学伯克利分校(University of California, Berkeley)于2014年在edX平台讲授. PDF笔记下载(Acad ...

  2. MOOCULUS微积分-2: 数列与级数学习笔记 2. Series

    此课程(MOOCULUS-2 "Sequences and Series")由Ohio State University于2014年在Coursera平台讲授. PDF格式教材下载 ...

  3. python中%和format

    两者都是格式化字符串用的,前者是比较老的版本,现在已经不推荐,后者更强大一些 % In [22]: print '%s' % 'hello world' hello world In [23]: pr ...

  4. centos搭建https协议的tomcat和apache服务器以及nginx服务器,mysql php

    Apache HTTP Server(简称 Apache)是 Apache 软件基金会的一个开放源码的网页服务器,可以在大多数计算机操作系统中运行,由于其多平台和安全性被广泛使用,是最流行的 Web ...

  5. Fix failed to start session in Ubuntu

    When you are at login, press Ctrl+Alt+F1. It will take you to command line interface from the GUI. I ...

  6. BZOJ3226: [Sdoi2008]校门外的区间

    感觉很有趣的题呢. 每个点拆成两个,线段树维护. 不过这题难点其实在输入输出. #include<bits/stdc++.h> #define N (1<<17) #defin ...

  7. 【原】react+redux实战

    摘要:因为最近搞懂了redux的异步操作,所以觉得可以用react+redux来做一个小小的项目了,以此来加深一下印象.切记,是小小的项目,所以项目肯定是比较简单的啦,哈哈. 项目效果图如图所示:(因 ...

  8. wpf arcglobe +c# 三维缩放到图层

    /// <summary>        /// 地图缩放到图层        /// </summary>        /// <param name="s ...

  9. 以sysdba身份登录oracle报ORA-1031权限不足错误之完美分析

    在linux 操作系统的数据库服务器上,使用”sqlplus / as sysdba” 登录Oracle 10.2 数据库实例时,登录失败,显示ORA-01031:  权限不足. 在数据库所在服务器上 ...

  10. oracle自定义判断数据是否为数值函数

    CREATE OR REPLACE FUNCTION isnumeric (str IN VARCHAR2) RETURN NUMBER IS v_str ); BEGIN IF str IS NUL ...