uva 10020 Minimal coverage 【贪心】+【区间全然覆盖】
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 【贪心】+【区间全然覆盖】的更多相关文章
- UVA 10020 Minimal coverage(贪心 + 区间覆盖问题)
		
Minimal coverage The Problem Given several segments of line (int the X axis) with coordinates [Li, ...
 - UVa 10020 - Minimal coverage(区间覆盖并贪心)
		
Given several segments of line (int the X axis) with coordinates [Li, Ri]. You are to choose the min ...
 - uva.10020 Minimal coverage(贪心)
		
10020 Given several segments of line (int the X axis) with coordinates [Li, Ri]. You are to choose t ...
 - 【区间覆盖问题】uva 10020 - Minimal coverage
		
可以说是区间覆盖问题的例题... Note: 区间包含+排序扫描: 要求覆盖区间[s, t]; 1.把各区间按照Left从小到大排序,如果区间1的起点大于s,则无解(因为其他区间的左起点更大):否则选 ...
 - uva 10020 Minimal coverage
		
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...
 - UVA 10382 Watering Grass 贪心+区间覆盖问题
		
n sprinklers are installed in a horizontal strip of grass l meters long and w meters wide. Each spri ...
 - ural 1303 Minimal Coverage(贪心)
		
链接: http://acm.timus.ru/problem.aspx?space=1&num=1303 按照贪心的思想,每次找到覆盖要求区间左端点时,右端点最大的线段,然后把要求覆盖的区间 ...
 - UVA - 1615  Highway(贪心-区间选点问题)
		
题目: 给定平面上n(n≤105)个点和一个值D,要求在x轴上选出尽量少的点,使得对于给定的每个点,都有一个选出的点离它的欧几里得距离不超过D. 思路: 先自己造区间,然后贪心选点就可以了.之前做过一 ...
 - Minimal coverage (贪心,最小覆盖)
		
题目大意:先确定一个M, 然后输入多组线段的左端和右端的端点坐标,然后让你求出来在所给的线段中能够 把[0, M] 区域完全覆盖完的最少需要的线段数,并输出这些线段的左右端点坐标. 思路分析: 线段区 ...
 
随机推荐
- Coder的利器
			
Coder的利器记载 工作近三年,使用PC快六年,拥抱Mac整一年,投具器石榴裙三年.14年第一次被同事推荐Everything,开启了JeffJade对工具的折腾之旅,并乐此不疲.时去两年,这必然是 ...
 - 项目架构mvc+webapi
			
mvc+webapi 项目架构 首先项目是mvc5+webapi2.0+orm-dapper+ef codefirst. 1.项目框架层次结构: 这个mvc项目根据不同的业务和功能进行不同的区域划分, ...
 - bestcoder44#1002
			
这题采用分治的思想 首先,根据最后一位是否为1,将数分为两个集合, 集合与集合之间的lowbit为1, 然后将每个集合内的元素,倒数第二位是否为1,将数分为两个集合,集合与集合之间的lowbit为2 ...
 - C语言中main函数的參数具体解释
			
main函数的定义形式 main函数能够不带參数,也能够带參数,这个參数能够觉得是 main函数的形式參数.C语言规定main函数的參数仅仅能有两个,习惯上这两个參数写为argc和ar ...
 - HDU 1394 Minimum Inversion Number (数据结构-段树)
			
Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java ...
 - Eclipse+Maven命令创建webapp项目<三>
			
1.使用maven命令:mvn archetype:create -DgroupId=xxxxx -DartifactId=web-sample -DarchetypeArtifactId=maven ...
 - JavaBean编程的基本思路-逻辑业务层
			
JavaBean是Java类别.为了实现业务逻辑层. 你是什么意思?我有一个很长的故事短:我们知道,JSP编程是Java编写的代码html文件.和JavaBean编程是Java写在另一个代码JAVA类 ...
 - 快速排序java
			
快速排序(Quicksort)是对冒泡排序的一种改进.它是先在数组中找到一个关键数,第一趟排序将比关键数小的放在它的左边,比关键数大的放在它的右边.当第一趟排序结束后,再依次递归将左边和右边的进行排序 ...
 - WPF 3D:使用变换中的TranslateTransform3D
			
原文:WPF 3D:使用变换中的TranslateTransform3D 程序效果: WPF 3D中的TranslateTransform3D应该是所有3D变换中最简单的变换,使用起来非常简单,先定义 ...
 - mySQL中删除unique key的语法 (删除某个字段的唯一性)
			
mySQL中删除unique key的语法 CREATE TABLE `good_booked` ( `auto_id` int(10) NOT NULL auto_increment, `goo ...