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. hdu 4322 最大费用流

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4322 #include <cstdio> #include <cstring> ...

  2. Jenkins 九: 小技巧

    1.  问题: jenkins的项目默认存放在 JENKINS_HOME下面的 workspace路径下,导致每次找项目都很不方便. 解决思路:更改jenkins的项目存放地址. 解决方法: 1) 将 ...

  3. MAVEN 工程打包resources目录外的更多资源文件

    首先,来看下MAVENx项目标准的目录结构: 一般情况下,我们用到的资源文件(各种xml,properites,xsd文件等)都放在src/main/resources下面,利用maven打包时,ma ...

  4. XQuery FLWOR 表达式

    FLWOR 是 "For, Let, Where, Order by, Return" 的只取首字母缩写.for 语句把 bookstore 元素下的所有 book 元素提取到名为 ...

  5. Android Fragment生命周期及静态加载

    一个Fragment必须总是被嵌入到一个Activity中,它的生命周期直接被其所属的宿主Activity生命周期影响,它的状态会随宿主的状态变化而变化. 要创建一个Fragment 必须创建一个Fr ...

  6. 数据结构 - 堆排序(heap sort) 具体解释 及 代码(C++)

    堆排序(heap sort) 具体解释 及 代码(C++) 本文地址: http://blog.csdn.net/caroline_wendy 堆排序包括两个步骤: 第一步: 是建立大顶堆(从大到小排 ...

  7. x64位windows上程序开发的注意事项

    在Windows上面32位与64位的区别有: 1.指针大小的区别,sizeof(int *)在32bit下面是4个字节,在64bit下面是8个字节 2.size_t的区别,size_t在32bit下面 ...

  8. [转] tomcat结合nginx使用小结

    相信很多人都听过nginx,这个小巧的东西慢慢地在吞食apache和IIS的份额.那究竟它有什么作用呢?可能很多人未必了解. 说到反向代理,可能很多人都听说,但具体什么是反向代理,很多人估计就不清楚了 ...

  9. [转] C++11带来的move语义

    PS: 通过引入接收右值的函数形参,可以通过接收右值来实现高效 PS在C++11中,标准库在<utility>中提供了一个有用的函数std::move,这个函数的名字具有迷惑性,因为实际上 ...

  10. Bernese安装及使用

    一.安装: 伯尔尼软件的安装很简单,但是在64位下,可能perl解释器安装不成功,我找了一个,并且可用,下载地址: 链接:http://pan.baidu.com/s/1hr8fgEC 密码:fj8b ...