hdu Priest John's Busiest Day

本文介绍了一种解决婚礼仪式调度问题的算法,通过排序和贪心策略确保每位新人能在规定时间内完成仪式,避免时间冲突。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

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

Sample Output

NO
YES

 


 

 

 

 

此题难度不大  排序+贪心

 

 

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

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

 

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

对每组时间段计算出这段时间,假设婚礼从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

      

 

 

 

 

 

 

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

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

 

 

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值