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 (1N100, 000) indicating the total number of the weddings. In the next N lines, each line contains two integers Si and Ti (0Si < Ti2147483647).
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,T], 然后你需要为每个婚礼祈祷? 时间要超过婚礼持续时间的一半,问能不能每个婚礼都祈祷。
思路:由于每个婚礼都必须要参加,那么我们把每个婚礼要求的最晚参加时间求出来,然后按这个排个序,按这个顺序参加,看满不满足就行了。
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <map>
#include <cassert>
#include <string>
#include <queue>
#include <set>
#include <cmath>
#include <cmath>
#include <algorithm>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define rep(i,a,b) for(int i=(a);i<(int)(b);++i)
#define rrep(i,b,a) for(int i=(b);i>=(int)(a);--i)
#define clr(a,x) memset(a,x,sizeof(a))
#define mp make_pair
#define eps 1e-10
#define LL long long
#define zero(x) (-eps < (x) && (x) < eps)
const int maxn = 100000 + 5;
int n;
struct Task
{
int S, T;
Task(int S = 0, int T = 0)
:S(S),T(T) { }
bool operator<(const Task&tk) const { return (S + T)/2 < (tk.S + tk.T) / 2; }
}task[maxn];
void input()
{
rep(i,0,n) scanf("%d%d",&task[i].S,&task[i].T);
sort(task,task+n);
}
bool solve()
{
int t = 0;
rep(i,0,n) {
t = max(t,task[i].S)+(task[i].T-task[i].S)/2 + 1;
if (t > task[i].T) return false;
}
return true;
}
int main()
{
#ifdef ACM
freopen("in.txt","r",stdin);
#endif // ACM
while (scanf("%d",&n),n) {
input();
puts(solve() ? "YES" : "NO");
}
}