Minimal Coverage(ural1303,区间贪心)
来源:互联网 发布:社交网络的坏处 编辑:程序博客网 时间:2024/05/18 03:31
http://acm.timus.ru/problem.aspx?space=1&num=1303
http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=13756
Minimal Coverage
Time Limit: 1000MS Memory Limit: 65536KB 64bit IO Format: %I64d & %I64u
[Submit] [Go Back] [Status]
Description
Given set of line segments [L i, R i] with integer coordinates of their end points. Your task is to find the minimal subset of the given set which covers segment [0, M] completely (M is a positive integer).
Input
First line of the input contains an integer M (1 ≤ M ≤ 5000). Subsequent lines of input contain pairs of integers L i and R i (−50000 ≤ L i < R i ≤ 50000). Each pair of coordinates is placed on separate line. Numbers in the pair are separated with space. Last line of input data contains a pair of zeroes. The set contains at least one and at most 99999 segments.
Output
Your program should print in the first line of output the power of minimal subset of segments which covers segment [0, M]. The list of segments of covering subset must follow. Format of the list must be the same as described in input with exception that ending pair of zeroes should not be printed. Segments should be printed in increasing order of their left end point coordinate.
If there is no covering subset then print “No solution” to output.
Sample Input
input output
1
-1 0
-5 -3
2 5
0 0
No solution
1
-1 0
0 1
0 0
1
0 1
Source
Problem Source: II Collegiate Students Urals Programming Contest. Yekaterinburg, April 3-4, 1998
解析:
题意:给出若干段区间,问是否可以找到最少段区间将[0,m]覆盖,输出区间个数以及按照
递增的顺序输出这些区间
思路:
区间的贪心,为保证得到最优解,先对区间进行排序处理,删掉被覆盖区间;
然后从左到右找到可拼接的区间,直至边界跨过或在m为止
思路参考自:http://hi.baidu.com/chingfantsou/item/5e1d530e66cd04803d42e26e
#include<string.h>#include<math.h>#include<stdio.h>#include<algorithm>#include <iostream>using namespace std;const int maxn=100000+10;struct Line{int s;int t;}line[maxn],temp[maxn];bool cmp(Line a,Line b){return a.s<b.s||(a.s==b.s&&a.t>b.t);}int main(){ int m,k,i,j,ans; int n,cnt,l,r; scanf("%d",&m); cnt=0; for(;;) { scanf("%d%d",&l,&r); if(l==0&&r==0) break; line[++cnt].s=l; line[cnt].t=r;} sort(line+1,line+cnt+1,cmp); j=1; for(i=2;i<=cnt;i++)//将互不覆盖的线段存起来 if(line[i].s>line[j].s&&line[i].t>line[j].t) line[++j]=line[i]; n=j; ans=0; line[n+1].s=m+1; line[n+1].t=m+1; k=0;//第一个临界 for(i=1;i<=n;i++) if(line[i+1].s>k&&line[i].s<=k) { k=line[i].t; temp[++ans]=line[i]; if(line[i].t>=m) { printf("%d\n",ans); for(j=1;j<=ans;j++) printf("%d %d\n",temp[j].s,temp[j].t); return 0; } } printf("No solution\n"); return 0;}
- Minimal Coverage(ural1303,区间贪心)
- 13 URAL1303 Minimal Coverage
- [Virtual Judge]URAL1303:Minimal Coverage
- UVA 10020 - Minimal coverage (贪心) 区间覆盖问题
- UVA 10020 Minimal coverage(贪心 + 区间覆盖问题)
- UVa 10020 - Minimal coverage(区间覆盖、贪心)
- uva 10020- Minimal coverage (贪心思想 简单区间覆盖)
- UVA 10200 Minimal coverage (区间覆盖贪心)
- UVa 10020 Minimal coverage (贪心&区间覆盖)
- uva 10020 Minimal coverage(贪心,区间覆盖)
- UVA Minimal coverage (贪心)
- UVa 10020 - Minimal coverage(区间覆盖)
- uva10020 - Minimal coverage(区间覆盖)
- uva10020 - Minimal coverage(区间覆盖)
- UVA 10020 Minimal coverage 区间覆盖问题 贪心
- uva 10020 Minimal coverage 【贪心】+【区间完全覆盖】
- Ural 1303 Minimal Coverage(贪心)
- Ural 1303 Minimal Coverage(贪心)
- CodeForces 333A Secrets
- 内存管理单元mmu
- 批处理创建和修改环境变量
- java5.0中增强的for/in循环
- 小Q系列故事——大笨钟
- Minimal Coverage(ural1303,区间贪心)
- HTTP消息头
- New Year Bonus Grant(SGU195,贪心or区间DP)
- 手工打造可执行程序
- Discrete Function(timus1010,暴力)
- 【rmzt】黄昏少女&&失忆win7主题_8.18
- 文件拖拽效果----C++实现
- hdu4515 小Q系列故事——世界上最遥远的距离
- Best Time to Buy and Sell Stock II