hdu Priest John's Busiest Day

来源:互联网 发布:表面很傻的人可怕知乎 编辑:程序博客网 时间:2024/04/28 09:27

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

 


 

 

 

 

此题难度不大  排序+贪心

 

 

关键就在按照什么关键字进行排序,

首先介绍队友实验可行的排序思路:

 

       对于每个人的婚礼多存在一段时间,在这段时间内必须举行仪式,

对每组时间段计算出这段时间,假设婚礼从a开始到b。

则在[b-((b-a)/2+1),a+((b-a)/2+1)]内必须举行仪式

于是对于每组数据先按照b-((b-a)/2+1)排序,然后再维护一个end。

end记录对每组数据前一组仪式结束的时间,然后只要加上本组仪式所外需的时间然后进行比较

,如果end已经超过b-((b-a)/2+1)则输出no,否则直到最后输出yes

      

 

 

 

 

 

 

 另一种思路是按照每段的中点进行排序(更为简单),

其余和上一思路雷同, 此处不再赘述