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");
}
}

HDU 2491 Priest John's Busiest Day(贪心)(2008 Asia Regional Beijing)的更多相关文章

  1. HDU 2491 Priest John's Busiest Day

    贪心.. #include<iostream> #include<string.h> #include<math.h> #include <stdio.h&g ...

  2. HDU 2487 Ugly Windows(暴力)(2008 Asia Regional Beijing)

    Description Sheryl works for a software company in the country of Brada. Her job is to develop a Win ...

  3. HDU 2494/POJ 3930 Elevator(模拟)(2008 Asia Regional Beijing)

    Description Too worrying about the house price bubble, poor Mike sold his house and rent an apartmen ...

  4. HDU 2490 Parade(DPの单调队列)(2008 Asia Regional Beijing)

    Description Panagola, The Lord of city F likes to parade very much. He always inspects his city in h ...

  5. HDU 2492 Ping pong(数学+树状数组)(2008 Asia Regional Beijing)

    Description N(3<=N<=20000) ping pong players live along a west-east street(consider the street ...

  6. HDU 2489 Minimal Ratio Tree(暴力+最小生成树)(2008 Asia Regional Beijing)

    Description For a tree, which nodes and edges are all weighted, the ratio of it is calculated accord ...

  7. hdu-----2491Priest John's Busiest Day(2008 北京现场赛G)

    Priest John's Busiest Day Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Jav ...

  8. 图论(2-sat):Priest John's Busiest Day

    Priest John's Busiest Day   Description John is the only priest in his town. September 1st is the Jo ...

  9. POJ 3683 Priest John's Busiest Day / OpenJ_Bailian 3788 Priest John's Busiest Day(2-sat问题)

    POJ 3683 Priest John's Busiest Day / OpenJ_Bailian 3788 Priest John's Busiest Day(2-sat问题) Descripti ...

随机推荐

  1. 剑指Offer_编程题之从尾到头打印链表

    题目描述 输入一个链表,从尾到头打印链表每个节点的值.

  2. Linux 学习第四天

    Linux学习第四天 一.常用命令 1.tar  (压缩.解压) A.添加压缩包  tar czvf 压缩包名称.tar.gz 源文件 B.添加压缩包  tar cjvf 压缩包名称.tar.bz2 ...

  3. wordpress | 网站访问速度优化方案(Avada)

    一.谷歌字体 原因: Wordpress系统默认使用谷歌字体,在国内谷歌域名被屏蔽,所以导致操作反应慢. 解决方法: 对于后台:找到Wordpress这个文件 /wp-includes/script- ...

  4. 静态导入方法即自动拆装箱(java)

    package example6;import static java.lang.System.out;import static java.util.Arrays.sort;import java. ...

  5. H5新增的标签以及改良的标签

    1>OL标签的改良 start type  reversed:翻转排序 2>datalist标签自动补全的使用 3>progress标签的使用:进度条 4>meter标签的应用 ...

  6. es6 Reflect对象详解

    Reflect是ES6为操作对象而提供的新API,而这个API设计的目的只要有: 将Object对象的一些属于语言内部的方法放到Reflect对象上,从Reflect上能拿到语言内部的方法.如:Obj ...

  7. python中mysql主从同步配置的方法

    1)安装mysql ubuntu中安装一台mysql了,docker安装另外一台mysql 获取mysql的镜像,主从同步尽量保证多台mysql的版本相同,我的ubuntu中存在的mysql是5.7. ...

  8. Hive(4)-Hive的数据类型

    一. 基本数据类型 Hive数据类型 Java数据类型 长度 例子 TINYINT byte 1byte有符号整数 20 SMALINT short 2byte有符号整数 20 INT int 4by ...

  9. linux使用logrotate工具管理日志轮替

    对于Linux系统安全来说,日志文件是极其重要的工具.logrotate程序是一个日志文件管理工具.用于分割日志文件,删除旧的日志文件,并创建新的日志文件,起到"转储"作用.可以节 ...

  10. ubuntu16.04 64位安装tftp服务器

    1.安装tftpd-hpa和xinetd sudo apt-get install tftpd-hpa xined2.针对64位操作系统安装openbsd-inetd apt-get install ...