uvalive 4328(贪心)

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

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

#include <stdio.h>#include <algorithm>using namespace std;const int N = 100005;struct Couple {int s, t, d;}cou[N];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;elsereturn 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");elseprintf("NO\n");}return 0;}

我诚恳地希望能够获得你的原谅。只是你懂得的,对于有一些委屈,

uvalive 4328(贪心)

相关文章:

你感兴趣的文章:

标签云: