您的位置:首页 > 其它

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


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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: