uva 10020 Minimal coverage (greedy)
uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=961
Classical greedy problem!
The Code:
View Code
1 typedef pair<int, int> PII; 2 typedef vector<PII> VPII; 3 #define REP(i, n) for (int i = 0; i < (n); i++) 4 5 VPII rec, out; 6 7 void input() { 8 rec.clear(); 9 int x, y; 10 while (~scanf("%d%d", &x, &y) && (x || y)) rec.PB(MPR(x, y)); 11 } 12 13 bool work(int n) { 14 if (!SZ(rec)) return false; 15 sort(ALL(rec)); 16 int mk = -1, L = 0; 17 out.clear(); 18 REP(i, SZ(rec)) { 19 if (rec[i].FI > L) { 20 if (mk == -1 || (rec[mk].SE < rec[i].FI) && (rec[mk].SE < n)) return false; 21 out.PB(rec[mk]); 22 if (rec[mk].SE >= n) return true; 23 L = rec[mk].SE; 24 mk = i; 25 } 26 if (~mk) { 27 if (rec[i].SE > rec[mk].SE) mk = i; 28 } else mk = i; 29 } 30 if (rec[mk].SE >= n) { 31 out.PB(rec[mk]); 32 return true; 33 } 34 return false; 35 } 36 37 int main() { 38 int n, T; 39 scanf("%d", &T); 40 while (T-- && ~scanf("%d", &n)) { 41 input(); 42 if (work(n)) { 43 printf("%d\n", SZ(out)); 44 REP(i, SZ(out)) printf("%d %d\n", out[i].FI, out[i].SE); 45 } else { 46 puts("0"); 47 } 48 if (T) puts(""); 49 } 50 return 0; 51 }
——written by Lyon