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,和一些区间[L,R]。。要选出几个区间能完全覆盖住[0,M]区间。要求数量最少。。如果不能覆盖输出0.

思路:贪心的思想。。把区间按R从大到小排序。 然后遇到一个满足的[Li,Ri],就更新缩小区间。。直到完全覆盖。

注意[L,R]只有满足L小于等于且R大于当前覆盖区间左端这个条件。才能选中。

贪心,把各区间按照R从大到小排序。如果区间1的起点不是s,无解,否则选择起点在s的最长区间。选择此区间[Li,Ri]后,新的起点设置为Ri,然后经过依次扫描之后就可以得出最小的线段数。并用另一个结构体储存路径。

#include <stdio.h>
#include <algorithm>
using namespace std;
int t;
int start,end,n,f;
struct qujian
{
int start;
int end;
};
qujian q[],p[];
int cmp (qujian a,qujian b)
{
return a.end > b.end;
}
int main()
{
scanf("%d", &t);
while (t --)
{
n = ;
f = ;
start = ;
scanf("%d", &end);
while (scanf("%d%d", &q[n].start, &q[n].end) && (q[n].start||q[n].end))
{
n++;
}
sort(q,q+n,cmp);
while(start<end)
{
int i;
for (i=;i<n;i++)
{
if (q[i].start <= start && q[i].end > start)
{
start = q[i].end; //更新区间
p[f] = q[i];
f++;
break;
}
}
if (i==n) break; //如果没有一个满足条件的区间,直接结束。
}
if(start<end) printf("0\n");
else
{
printf("%d\n",f);
for (int i=;i<f;i++)
printf("%d %d\n", p[i].start,p[i].end);
}
if (t) printf("\n");
}
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(贪心)

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

  5. uva 10020 Minimal coverage

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

  6. UVA 10382 Watering Grass(区间覆盖,贪心)题解

    题意:有一块草坪,这块草坪长l 米,宽 w 米,草坪有一些喷头,每个喷头在横坐标为 p 处,每个喷头的纵坐标都是(w/2) ,并且喷头的洒水范围是一个以喷头为圆心,半径为 r 米的圆.每次最少需要打开 ...

  7. UVA 10382 Watering Grass (区间覆盖,贪心)

    问题可以转化为草坪的边界被完全覆盖.这样一个圆形就换成一条线段. 贪心,从中选尽量少的线段把区间覆盖,按照把线段按左端点排序,记录一个当前已经覆盖区间的位置cur, 从左端点小于等于cur选一个右端点 ...

  8. 外星人的供给站 (区间覆盖 t贪心)

    /** 区间覆盖问题 分析: 每个点可以确定两个圆心 圆心的范围形成 一个区间 在这个区间上以任意一点画圆便可将此点 包含在内 如果有两个点所确定的区间相交了 说明这两个点可以用一个圆包含在内 即用一 ...

  9. nyoj--12--喷水装置(二)(区间覆盖问题+贪心)

    喷水装置(二) 时间限制:3000 ms  |  内存限制:65535 KB 难度:4 描述 有一块草坪,横向长w,纵向长为h,在它的橫向中心线上不同位置处装有n(n<=10000)个点状的喷水 ...

随机推荐

  1. 动态规划——树形dp

    动态规划作为一种求解最优方案的思想,和递归.二分.贪心等基础的思想一样,其实都融入到了很多数论.图论.数据结构等具体的算法当中,那么这篇文章,我们就讨论将图论中的树结构和动态规划的结合——树形dp. ...

  2. 【BZOJ4327】JSOI2012 玄武密码 AC自动机

    [BZOJ4327]JSOI2012 玄武密码 Description 在美丽的玄武湖畔,鸡鸣寺边,鸡笼山前,有一块富饶而秀美的土地,人们唤作进香河.相传一日,一缕紫气从天而至,只一瞬间便消失在了进香 ...

  3. Linux网络编程echo多线程服务器

    echo_server服务器多线程版本 #include <unistd.h> #include <stdlib.h> #include <stdio.h> #in ...

  4. 输入一个整形数组,数组里有正数也有负数。数组中连续的一个或多个整数组成一个子数组,每个子数组都有一个和。求所有子数组的和的最大值。要求时间复杂度为O(n)

    群里看到这道题,用python做了做, def find(array): v_sum = greatest = 0 for a in array: v_sum += a v_sum = 0 if v_ ...

  5. CSS3伪类选择器 图示

         

  6. 如何判断MSSQL数据库磁盘出现了瓶颈

    问大神石沫:如何判断MSSQL数据库磁盘出现了瓶颈? 石沫(A1):您好,您的问题非常好,SQL SERVER提供了很多关于I/O压力的性能计数器,请选择性能计算器PhysicalDisk(Logic ...

  7. common-httpclient 用户名密码认证示例

    import java.io.IOException; import java.util.ArrayList; import java.util.List; import org.apache.com ...

  8. [转] Python list、tuple、dict区别

    from: http://www.cnblogs.com/Michael-Kong/archive/2012/07/11/2585840.html Dictionary 是 Python 的内置数据类 ...

  9. [转] C++虚函数与虚函数表

    http://www.cnblogs.com/Ripper-Y/archive/2012/05/15/2501930.html http://blog.csdn.net/haoel/article/d ...

  10. oracle 添加自增索引

    1.添加一个Sequence,此处为ID_SEQUENCE. 2.添加对应表,并设置主键 3.设置触发器 create or replace trigger sys.id_add before ins ...