UVA-10020 Minimal coverage
题目:
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=961
代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
|
#include <iostream> #include <limits> #include <algorithm> #include <numeric> #include <string> #include <utility> #include <cstdlib> #include <vector> #include <sstream>
using namespace std;
typedef pair<int, int> P; P L[100010]; int have[100010];
int main() { ios::sync_with_stdio(false); cin.tie(NULL);
int T; cin >> T; while (T--) { int endi = 0, m; cin >> m; while (cin >> L[endi].first >> L[endi].second && (L[endi].first || L[endi].second)) ++endi;
sort(L, L + endi);
int haveend = 0, last = 0; for (int i=0;i < endi;++i) { if (L[i].first > last) { haveend = 0; break; }
int maxleft = i; for (int j = i+1;j < endi&&L[j].first<=last;++j) { if (L[j].second >= L[maxleft].second) maxleft = j; }
i = maxleft;
if (L[i].second < 0) { haveend = 0; break; }
have[haveend++] = i;
last = L[i].second;
if (last >= m) break; }
if (last < m) { cout << 0 << endl; } else { cout << haveend << endl; for (int i = 0;i < haveend;++i) cout << L[have[i]].first << ' ' << L[have[i]].second << endl; } if (T) cout << endl; } }
|
解析&吐槽:
标准的区间覆盖问题,这道题还把情况变得容易了一些,左端点是固定的,只有右端点是变量 M。区间覆盖的教程参见:http://blog.sina.com.cn/s/blog_892cc2ff01013cn4.html
本作品采用
署名-相同方式共享 4.0 国际
进行许可。欢迎转载、使用、重新发布,但务必保留文章署名
“不科学的科学君” (Liu233w) 与博客链接:
https://liu233w.github.io
,基于本文修改后的作品务必以相同的许可发布。如有任何疑问,请
与我联系
。