URAL1303——贪心——Minimal Coverage
2015-05-11 16:56
441 查看
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
大意:找最少的覆盖区间,现在输入里面处理没用的数据,然后对于begin开始排序,找到在起点(end前一个点)前t[i].end最大的,不断更新end点如果nend没有被更新,说明没有点能够到达
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 |
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; struct edge{ int b,e; }t[100010]; bool vis[100010]; bool cmp(edge i,edge j){ if(i.b == j.b) return i.e < j.e; return i.b < j.b; } int main() { int M; int x,y; while(~scanf("%d",&M)){ int count = 1; memset(vis,false,sizeof(vis)); while(~scanf("%d%d",&x,&y)){ if(x == 0 && y == 0) break; if(y <= 0 || x >= M || x == y) continue; t[count].b = x; t[count++].e = y; } sort(t+1, t+count,cmp); int end = 0,pos = 1; int flag = 1,res = 0;; while(end < M){ int end1 = 0,npos = 1; while(pos < count && t[pos].b <= end){ if(t[pos].e > end1){ end1 = t[pos].e; npos = pos; } pos++; } if(end1 == 0) {flag = 0 ;break;} end = end1; vis[npos] = 1; res++; } if(flag == 0) printf("No solution\n"); else { printf("%d\n",res); for(int i = 1; i < count ;i++){ if(vis[i]) printf("%d %d\n",t[i].b,t[i].e); } } } return 0; }
相关文章推荐
- Ural 1303 Minimal Coverage(贪心)
- Ural 1303 Minimal Coverage(贪心)
- Minimal Coverage(ural1303,区间贪心)
- Ural 1303 Minimal Coverage(贪心)
- ural 1303 Minimal Coverage【贪心】
- ural 1303 Minimal Coverage【贪心】
- 贪心 URAL 1303 Minimal Coverage
- 【贪心】URAL - 1303 Minimal Coverage
- ural 1303 Minimal Coverage(贪心)
- URAL 1303 Minimal Coverage (贪心)
- URAL - 2021 Scarily interesting!(贪心)
- URAL1901贪心
- URAL 2000 Grand Theft Array V 贪心
- URAL 1876. Centipede's Morning (贪心)
- (贪心5.2.6)URAL 1014 Product of Digits(利用数据有序化进行贪心选择)
- URAL 1303
- URAL 2021 Scarily interesting! (贪心+题意)
- URAL 1389 Roadworks 贪心
- URAL 1787 Turn for MEGA (贪心 + 模拟)
- URAL 1303