Minimal coverage

The Problem

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].

The 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.

The 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

题意:用最少的区间覆盖(0,m)这个区间。

分析:尽量选覆盖目的区间大的区间。我们能够依照每一个给的区间的最左端端点排序,从左端点小于st的区间中选取右端点最大的区间赋给en。这时候再比較st=en与m的大小,不满足k>=m,继续上面的循环,直到满足,或者找不到能够覆盖的区间。 详情看代码。

代码:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#define M 100005
using namespace std;
struct node{
int st, en;
}s[M];
int ans[M]; int cmp(node a, node b){
if(a.st == b.st) return a.en>b.en;
return a.st < b.st;
}
int main(){
int t, m;
scanf("%d", &t);
while(t --){
scanf("%d", &m);
int tot = 0, a, b;
while(scanf("%d%d", &a, &b), a||b){
if(a>b) swap(a, b);
s[tot].st = a; s[tot].en = b;
++tot;
}
sort(s, s+tot, cmp);
int st, en, num;
st = en = num = 0;
while(st<m){
en = st;
for(int i = 0; i < tot; i ++){
if(s[i].st<=st&&s[i].en>en){ //从左端点小于st的区间中找出右端点最大的赋给en,而且用ans【num】储存右端点最大的区间的下标
en = s[i].en; ans[num] = i;
}
}
if(en == st){ //假设没有找到,能够继续覆盖的区间,直接跳出来。
num = 0; break;
}
st = en;
++num;
}
cout<<num<<endl;
for(int i = 0; i < num; i++)
cout<<s[ans[i]].st<<" "<<s[ans[i]].en<<endl;
}
exit(0);
}

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(区间覆盖并贪心)

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

  3. uva.10020 Minimal coverage(贪心)

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

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

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

  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 贪心+区间覆盖问题

    n sprinklers are installed in a horizontal strip of grass l meters long and w meters wide. Each spri ...

  7. ural 1303 Minimal Coverage(贪心)

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

  8. UVA - 1615 Highway(贪心-区间选点问题)

    题目: 给定平面上n(n≤105)个点和一个值D,要求在x轴上选出尽量少的点,使得对于给定的每个点,都有一个选出的点离它的欧几里得距离不超过D. 思路: 先自己造区间,然后贪心选点就可以了.之前做过一 ...

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

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

随机推荐

  1. Visibility属性实现自动隐藏功能

    //使用一个Button,鼠标移入listView显示,移出隐藏 private void button2_MouseEnter(object sender, System.Windows.Input ...

  2. 跨平台移动框架iMAG开发入门

    iMAG是一个非常简洁高效的移动跨平台开发框架,开发一次能够同一时候兼容Android和iOS平台,有点儿Web开发基础就能非常快上手.当前移动端跨平台开发的框架有非常多,但用iMAG另一个优点,就是 ...

  3. SQL集合运算 差集 并集 交

    SQL-3标准中提供了三种对检索结果进行集合运算的命令:并集UNION:交集INTERSECT:差集EXCEPT(在Oracle中叫做 MINUS).在有些数据库中对此的支持不够充分,如MySql中只 ...

  4. unrecognized selector sent to instance 0x10b34e810

    一个错误: *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSURLEr ...

  5. 在小发现SQL字符串比较是不是他们的大写和小写敏感

    声明:select  petName from dbo.T_pet order by petName desc 成绩:    petName    An admin A的ascii码小于a,按理说应该 ...

  6. Eclipse工具的问题

    1 Eclipse采用ctrl抄 粘贴很卡 Eclipse -- Windows->Preferences->General->Editors->Text Editors-&g ...

  7. windows中间vmware的Linux系统安装jdk步骤

    1.设置文件的享受,对于本地阅读windows档 于vmware虚拟机设置共享文件夹,那么共享文件中,你可以 2.然后打开虚拟机上,使用root输入账户,然后,在夹/mnt/hgfs/   共享文件了 ...

  8. 为什么tap事件绑定在document上,而不是对象本身上

    1.在移动端前端开发,click事件有300ms的延时,为了提升用户体验,快速响应.zepto添加了tap事件.tap是在手指触屏横纵向移动距离小于30px,触发tap事件.移动距离的判断是通过tou ...

  9. 微软最牛MS08-067漏洞各系统补丁下载地址

    本次MS08-067严重漏洞各系统补丁地址例如以下: 中文操作系统KB958644补丁下载地址: Windows Vista 安全更新程序 (KB958644) http://download.mic ...

  10. git bash 出现vim的时候怎么退出

    如果是输出状态,首先Esc退出输入状态,然后Shift+;,再输入q!或wq!(不保存改动,wq!是保存文件的写入修改)退出