uva 10020(贪心)
2014-09-06 00:51
393 查看
题意:给出了一些数轴上的范围[L,R]和m,问这些范围在数轴上是否能完全覆盖[0,m]这个范围,如果可以输出最小需要几个范围可以覆盖,把他们按左范围排序输出。
题解:先从小到大排序会省时,然后把左范围从0开始向右扩大,直到大于m,每次都找能到达最右边的那个范围,更新到输出序列中,如果不能再扩大且小于m说明无法完全覆盖。
题解:先从小到大排序会省时,然后把左范围从0开始向右扩大,直到大于m,每次都找能到达最右边的那个范围,更新到输出序列中,如果不能再扩大且小于m说明无法完全覆盖。
#include <stdio.h> #include <algorithm> using namespace std; const int N = 100005; struct Point { int l, r; }coo ; int cmp(Point a, Point b) { return a.l < b.l; } int main() { int t, n, m, ans , num, k; scanf("%d", &t); while (t--) { scanf("%d", &m); n = num = k = 0; while (scanf("%d%d", &coo .l, &coo .r) && (coo .l || coo .r)) n++; sort(coo, coo + n, cmp); while (k < m) { int temp = k; for (int i = 0; i < n; i++) if (coo[i].l <= k && coo[i].r > temp) { temp = coo[i].r; ans[num] = i; } if (temp == k) { num = 0; break; } k = temp; num++; } printf("%d\n", num); for (int i = 0; i < num; i++) printf("%d %d\n", coo[ans[i]].l, coo[ans[i]].r); if (t) printf("\n"); } return 0; }
相关文章推荐
- [贪心]uva10020 Minimal coverage
- UVa 10020 - Minimal coverage(区间覆盖并贪心)
- uva 10020 Minimal coverage(贪心,区间覆盖)
- UVA 10020 简单贪心区间覆盖,各种WA,记录一下
- uva---10020+贪心
- uva 10020 Minimal coverage(贪心-最小覆盖问题)
- UVA-10020 Minimal coverage(贪心)
- UVA 10020——Minimal coverage(最小覆盖,贪心问题)
- UVA 10020 Minimal coverage 区间覆盖问题 贪心
- uva10020 贪心
- UVa 10020 Minimal coverage (贪心&区间覆盖)
- UVA 10020 Minimal Coverage 排序+贪心
- uva 10020 Minimal coverage(贪心-最小覆盖问题)
- UVA 10020 Minimal coverage(贪心 + 区间覆盖问题)
- uva 10020 Minimal coverage 【贪心】+【区间完全覆盖】
- uva 10020 Minimal coverage 【贪心】+【区间全然覆盖】
- UVA10020 Minimal coverage 贪心
- UVA10020->贪心
- UVA 10020 - Minimal coverage (贪心) 区间覆盖问题
- Minimal coverage - UVa 10020 贪心