您的位置:首页 > 其它

1420 - Priest John's Busiest Day (贪心)

2014-01-01 00:41 288 查看
John is the only priest in his town. October 26th is the John's busiest day in a year because there is an old legend in the town that the couple who get married on that day will be forever blessed by the God of
Love. This year N couples plan to get married on the blessed day. The i -th couple plan to hold their wedding from time Si to time Ti .
According to the traditions in the town, there must be a special ceremony on which the couple stand before the priest and accept blessings. Moreover, this ceremony must be longer than half of the wedding time and can't be interrupted. Could you tell John how
to arrange his schedule so that he can hold all special ceremonies of all weddings?

Please note that:
John can not hold two ceremonies at the same time.
John can only join or leave the weddings at integral time.
John can show up at another ceremony immediately after he finishes the previous one.

Input 

The input consists of several test cases and ends with a line containing a zero. In each test case, the first line contains a integer N (1

N

100,
000) indicating the total number of the weddings. In the next Nlines, each line contains two integers Si and Ti ( 0

Si < Ti

2147483647 ).

Output 

For each test, if John can hold all special ceremonies, print ``YES"; otherwise, print ``NO".

Sample Input 

3
1 5
2 4
3 6
2
1 5
4 6
0


Sample Output 

NO
YES

题意:有n场婚礼,开始时间s结束时间e,每场婚礼要花超过一半时间做仪式,问能否所有婚礼都成功仪式。

哎。这题一开是想残了。一直想着用贪心+优先队列去做,wa了。。后面仔细想想才发现想法是错的。

思路:按每场婚礼最晚可以仪式的时间mid排序。然后维护一个Min作为区间左,然后每次判断是否超过mid。这样做的可行性建立在,每个区间即使从s开始,到他仪式完的时间肯定超过mid。所以mid越小的肯定要越先去完成。

代码:

#include <stdio.h>
#include <string.h>
#include <algorithm>
#define max(a,b) (a)>(b)?(a):(b)
using namespace std;

const int N = 100005;

int n;
struct D {
int s, e, mid;
} d
;

bool cmp(D a, D b) {
return a.mid < b.mid;
}

void init() {
for (int i = 0; i < n; i++) {
scanf("%d%d", &d[i].s, &d[i].e);
d[i].mid = d[i].e - ((d[i].e - d[i].s) / 2 + 1);
}
}

bool judge() {
int Min = 0;
for (int i = 0; i < n; i++) {
if (d[i].mid < Min) return false;
Min = max(Min, d[i].s);
Min += (d[i].e - d[i].s) / 2 + 1;
}
return true;
}

void solve() {
sort(d, d + n, cmp);
if (judge()) printf("YES\n");
else printf("NO\n");
}

int main() {
while (~scanf("%d", &n) && n) {
init();
solve();
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: