您的位置:首页 > 其它

uvalive 4328(贪心)

2015-03-25 19:00 405 查看
题意:有n对夫妻要举行婚礼,然后一个牧师需要在所有婚礼上举行一个仪式,每个仪式必须占用婚礼一半以上的时间且不能被打断,给出了n个婚礼的开始和结束时间,问是否可以让牧师在所有婚礼都举行仪式。

题解:先按婚礼起始时间排序,然后遍历所有婚礼看是否有足够时间举行仪式。

#include <stdio.h>
#include <algorithm>
using namespace std;
const int N = 100005;
struct Couple {
int s, t, d;
}cou
;
int n;

bool cmp(Couple a, Couple b) {
return a.s + a.d < b.s + b.d;
}

bool judge() {
int ss = cou[0].s;
for (int i = 0; i < n - 1; i++) {
int temp = ss + cou[i].d;
if (temp >= cou[i + 1].s && temp + cou[i + 1].d <= cou[i + 1].t)
ss = temp;
else if (temp < cou[i + 1].s)
ss = cou[i + 1].s;
else
return false;
}
return true;
}

int main() {
while (scanf("%d", &n) && n) {
for (int i = 0; i < n; i++) {
scanf("%d%d", &cou[i].s, &cou[i].t);
cou[i].d = (cou[i].t - cou[i].s) / 2 + 1;
}
sort(cou, cou + n, cmp);
if (judge())
printf("YES\n");
else
printf("NO\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  uvalive