HDU 2491 Priest John's Busiest Day

来源:互联网 发布:微软软件下载 编辑:程序博客网 时间:2024/04/28 06:54

题目链接 : HDU2491

Priest John's Busiest Day

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1198    Accepted Submission(s): 336


Problem Description
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 N lines, 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
31 52 43 621 54 60
 


 

Sample Output
NOYES
 


 

Source
2008 Asia Regional Beijing
 


 

Recommend
gaojie
 
 
贪心  有点不一样的就是要从中点开始
 
#include<cstdio>#include<cstring>#include<cmath>#include<cstdlib>#include<algorithm>#include<string>#include<vector>#include<map>#include<queue>using namespace std;int num[100005][3];int xu[100005];bool cmp(int a,int b){return num[a][2]<num[b][2];}int main(){    int n;    while(scanf("%d",&n)==1)    {        if(n==0) break;        for(int i=0;i<n;i++)        {            scanf("%d%d",&num[i][0],&num[i][1]);            num[i][2]=(num[i][0]+num[i][1]+1)/2-1;            xu[i]=i;        }        sort(xu,xu+n,cmp);        bool flag=1;        int now=(num[xu[0]][0]+num[xu[0]][1])/2+1;        for(int i=1;i<n;i++)        {            if(now>num[xu[i]][2])            {                flag=0;                break;            }            now=max(now+(num[xu[i]][1]-num[xu[i]][0])/2+1,(num[xu[i]][1]+num[xu[i]][0]+1)/2-1);        }        puts(flag?"YES":"NO");    }}


 

 

 

原创粉丝点击