您的位置:首页 > 其它

uva 10020(贪心)

2014-09-06 00:51 393 查看
题意:给出了一些数轴上的范围[L,R]和m,问这些范围在数轴上是否能完全覆盖[0,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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  uva