传送门UVa:UVa 10020 & POJ 2620 - Minimal coverage
→→→POJ:UVa 10020 & POJ 2620 - Minimal coverage
题意:找出最少的区间,覆盖【0,M】。
虽然是小白上的例题,但是小白上的有点看不懂。
参考了帆帆帆帆帆帆帆帆帆帆的解题报告,非常好的思路。
把全部区间【x,y】按照y从大到小排序,每次都从头开始扫描,遇到符合条件的就更新start,这样就能确保每次碰到的区间都是最大的。
详情见代码
UVa:
#include <cstdio> #include <vector> #include <algorithm> using namespace std; struct POINT { int x, y; }; bool cmp(const POINT &a, const POINT &b) { return a.y > b.y; } int main() { //freopen("input.txt", "r", stdin); int T, i, j, target; vector<POINT> num, ans; POINT temp; scanf("%d", &T); while (T--) { ans.clear(); num.clear(); int start = 0; scanf("%d", &target); while (scanf("%d%d", &temp.x, &temp.y)) { if (temp.x == 0 && temp.y == 0) break; num.push_back(temp); } sort(num.begin(), num.end(), cmp); while(start < target) { for (i = 0; i < num.size(); i++) if (num[i].x <= start && num[i].y > start) { start = num[i].y; ans.push_back(num[i]); break; } if (i == num.size()) break; } if (start < target) printf("0\n"); else printf("%d\n", ans.size()); for (i = 0; i < ans.size(); i++) printf("%d %d\n", ans[i].x, ans[i].y); if (T) printf("\n"); } return 0; }
#include <cstdio> #include <vector> #include <algorithm> using namespace std; struct POINT { int x, y; }; bool cmp(const POINT &a, const POINT &b) { return a.y > b.y; } int main() { //freopen("input.txt", "r", stdin); int T, i, j, target; vector<POINT> num, ans; POINT temp; ans.clear(); num.clear(); int start = 0; scanf("%d", &target); while (scanf("%d%d", &temp.x, &temp.y)) { if (temp.x == 0 && temp.y == 0) break; num.push_back(temp); } sort(num.begin(), num.end(), cmp); while(start < target) { for (i = 0; i < num.size(); i++) if (num[i].x <= start && num[i].y > start) { start = num[i].y; ans.push_back(num[i]); break; } if (i == num.size()) break; } if (start < target) printf("No solution\n"); else printf("%d\n", ans.size()); for (i = 0; i < ans.size(); i++) printf("%d %d\n", ans[i].x, ans[i].y); return 0; }