Priest John's Busiest Day
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1734 Accepted Submission(s): 479
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.
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)
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
3
1 5
2 4
3 6
2
1 5
4 6
0
Sample Output
NO
YES
Source
#include<stdio.h> #include<string.h> #include<iostream> #include<algorithm> using namespace std; struct node{ int u,v,m,h; }que[100005]; bool cmp(struct node t1,struct node t2){ if(t1.m==t2.m) return t1.u<t2.u; else return t1.m<t2.m; } int main(){ int n; while(scanf("%d",&n)!=EOF){ if(n==0) break; for(int i=0;i<n;i++){ scanf("%d%d",&que[i].u,&que[i].v); que[i].h=(que[i].v-que[i].u)/2+1; que[i].m=que[i].u+que[i].h; } sort(que,que+n,cmp); int flag=0; for(int i=0;i<n-1;i++){ if(que[i].m>=que[i+1].m){ flag=1; break; } if(que[i].m<que[i+1].u) continue; double temp=que[i].m-que[i+1].u; que[i+1].m=temp+que[i+1].h+que[i+1].u; } if(flag) printf("NO "); else printf("YES "); } return 0; }