10020-minimal coverage
Time limit:3.000 seconds
Http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_ problem&problem=961
The Problem
Given several segments of line (int, X axis) with coordinates [LI,RI]. You are are to choose the minimal amount of them, such they would completely the cover the segment [0,m].
The Input
The the number of test cases, followed by a blank line.
Each test case in the input should contains a integer M (1<=m<=5000), followed by pairs "Li Ri" (| li|, | ri|<=50000, i<=100000), each on a separate line. Each test case of the input is terminated by pair "0 0".
Each test case is separated by a.
The Output
For each test case, in the ' the ' of output your programm should print the minimal number of line segments which can C Over segment [0,m]. In the following lines, the coordinates of segments, sorted by their left-end (Li), should is printed in the same format a s in the input. Pair "0 0" should not to be printed. If [0,m] can is covered by the 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
Classic greed on the P154 of entry classics.
Note that the title of the data is defective even if you do not add if (Nowl < M) cnt = 0; This sentence can also be AC.
See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/
Complete code:
/*0.065s*/#include <cstdio> #include <cstring> #include <algorithm> using namespace std;
const int N = 100005;
struct SEG {int L, R;
BOOL operator < (const SEG a) const {return L < A.L;
}} P[n], ans[n];
int main () {int T, n, M, I, Nowl, MAXR, CNT, Templ, TEMPR;
BOOL found;
scanf ("%d", &t);
while (t--) {scanf ("%d", &m);
n = 0; while (scanf ("%d%d", &templ, &TEMPR), Templ | | tempr) if (Templ < M && tempr) p[n++] = (s
eg) {Templ, the tempr};///can not be used in the interval does not exist in the sort (p, p + N);
for (i = Nowl = CNT = 0; Nowl < M; ++cnt) {found = false;
MAXR = 0;
for (; i < n && p[i].l <= nowl; ++i) if (P[i].r > MaxR) {
Found = true; MAXR = p[i].r;///in interval left endpoint characterThe most right ans[cnt] = P[i] that can be covered under the required conditions;
} if (!found) break;
Nowl = maxr;///Update left endpoint} if (Nowl < M) cnt = 0;///task failed ~ printf ("%d\n", CNT);
for (i = 0; i < cnt; i++) printf ("%d%d\n", ANS[I].L, ANS[I].R);
if (T) Putchar (10);
return 0; }