UVaOJ10020 - Minimal coverage
2013-05-23 19:22
169 查看
[title3]
10020 - Minimal coverage[/title3]
Time limit: 3.000 seconds
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 theirleft 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
Alex Gevak
September 10, 2000 (Revised 2-10-00, Antonio Sanchez)
#include <iostream> #include <algorithm> #include <cstdio> using namespace std; struct Point { int x, y; }point[100010]; bool cmp(const Point &a, const Point &b) { if (a.x != b.x) { return a.x < b.x; } else { return a.y > b.y; } } int ans[100010]; int main() { int T, M; scanf("%d", &T); while (T --) { scanf("%d", &M); int nIndex = 0; while (scanf("%d%d", &point[nIndex].x, &point[nIndex].y) != EOF) { if (point[nIndex].x == 0 && point[nIndex].y == 0) break; if (point[nIndex].y > 0) nIndex ++; } sort(point, point + nIndex, cmp); int cur = 0; int N = -1; if (point[0].x > 0) { printf("0\n"); if (T) { printf("\n"); } continue; } bool flag = false; for (int i = 0; i < nIndex; i ++) { if (point[i].x <= cur) { if (N == -1) ans[++ N] = i; else if (point[i].y > point[ans ].y) ans = i; } else { cur = point[ans ].y; ans[++ N] = i; } if (point[ans ].y >= M) { flag = true; break; } } if(flag){ printf("%d\n", N+1); for(int i = 0; i <= N; ++ i) printf("%d %d\n", point[ans[i]].x, point[ans[i]].y); } else{ printf("0\n"); } if(T) printf("\n"); } return 0; }
相关文章推荐
- UVAOJ 414 - Machined Surfaces
- 02_UVaOJ_ AOAPC I_Beginning Algorithm Contests (Rujia Liu)_Volume0_Getting Started
- UvaOJ 550 - Multiplying by Rotation
- UvaOJ 10082 - WERTYU 代码以及题解
- BFS 更深一步的认识(UVaOJ 10047)
- DP专题4 UVAOJ 108 Maximum Sum
- UVaOJ 10071 - Back to High School Physics
- UVaOJ 465 - Overflow
- UVaOJ 537 - Artificial Intelligence?
- UVaOJ 755 - 487--3279
- UVaOJ442---Matrix Chain Multiplication
- UVaOJ539 - The Settlers of Catan
- UVaOJ11054 - Wine trading in Gergovia
- UVA 10020 Minimal coverage(贪心 + 区间覆盖问题)
- UVaOj How many pieces of land?
- UvaOJ 133 - The Dole Queue
- UVAOJ 12304 - 2D Geometry 110 in 1!
- UVaOJ 490 - Rotating Sentences
- UVaOJ 445 Marvelous Mazes
- UvaOJ 424 整数查询