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”.

题目大意:有一个牧师要给大家举办婚礼,其中有一个祝福仪式,这个仪式的时间要大于婚礼的时间的一半(注意是大于)并且时间要是整数,而且不能中断。给n个婚礼的开始时间和结束时间,问能否合理安排祝福仪式使所有婚礼都受到祝福。

思路:注意到仪式的时间要大于婚礼的一半,那么这个仪式必然经过婚礼的中间时间,即(S+T)/2。所以若存在合理的方案,那么祝福仪式的举行顺序一定是和(S+T)/2从小到大排序的顺序一样。排好序后进行贪心选择,让每个仪式尽量靠前(为后来的仪式腾出尽量多的时间),从小到大逐一枚举即可。

PS:这题有一点比较坑爹的地方是你要算中间时间可能会用到(S+T)/2,S+T可能会爆int(可以写S +(T - S)/ 2)。我居然能发现这个坑爹的东西然后果断移项然后1A了好开心好开心O(∩_∩)O~~

PS2:这题跟之前做过的某道2-SAT背景几乎一模一样我差点还以为自己做过了……

代码(359MS):

 #include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; const int MAXN = ;
const int INF = 0x3fff3fff; struct Node {
int s, t;
bool operator < (const Node &rhs) const {
return s - rhs.s < rhs.t - t;//(s + t) < (rhs.s + rhs.t);
}
} wed[MAXN]; int n; bool solve() {
sort(wed, wed + n);
int last = ;
for(int i = ; i < n; ++i) {
int len = (wed[i].t - wed[i].s) / + ;
if(last + len > wed[i].t) return false;
last = max(last, wed[i].s) + len;
}
return true;
} int main() {
while(scanf("%d", &n) != EOF) {
if(n == ) break;
for(int i = ; i < n; ++i) scanf("%d%d", &wed[i].s, &wed[i].t);
if(solve()) puts("YES");
else puts("NO");
}
}

最新文章

  1. R语言XML包的数据抓取
  2. RabbitMQ入门教程——路由(Routing)
  3. iOS 验证邮箱手机号格式
  4. MVP+RXJAVA+RecyclerView实现sd卡根目录下的所有文件中的照片加载并显示
  5. oracle 强杀进程
  6. 多线程&amp;多进程解析:Python、os、sys、Queue、multiprocessing、threading
  7. PHP 判断协议是否为HTTPS
  8. Comet学习资料
  9. 【转】VC6.0打开或者添加工程文件崩溃的解决方法
  10. 浅谈session,cookie,sessionStorage,localStorage的区别及应用场景
  11. Python之路(第十七篇)logging模块
  12. python--第十二天总结(Python操作 RabbitMQ、Redis、Memcache、SQLAlchemy)
  13. Keras模型的导出和pb文件的转换
  14. python的命令行参数处理
  15. python中matplotlib 的简单使用
  16. 162. Find Peak Element (Array; Divide-and-Conquer)
  17. Codeforces Round #363 (Div. 1) B. Fix a Tree 树的拆环
  18. exit命令详解
  19. typedef那回事儿
  20. excel拼接数据宏

热门文章

  1. 19-3-1Python的PyCharm编辑器,以及格式化输出、while循环、运算符、编码初识
  2. Centos6.5 VM网络故障,可以Ping 通网关,无法上网或者访问其它网段
  3. scala 实现日期运算
  4. python应用:爬虫框架Scrapy系统学习第二篇——windows下安装scrapy
  5. jetty 服务器配置无项目名
  6. c语言中:strlen和sizeof的区别和它们分别交换各自作用领域(\0问题)时的细微差别!!!
  7. Vi中进行多行指定内容替换
  8. C语言跳水比赛预测结果
  9. python Tkinter 的 Text 保持焦点在行尾
  10. mybatis入门(三):mybatis的基础特性