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. 19-3-1Python的PyCharm编辑器,以及格式化输出、while循环、运算符、编码初识

    第一次使用PyCharm,需要注意到是否使用的自己安装的python版本,或者使用的自带版本,如果使用的自带版本,注意切换. PyCharm使用优点,可以一步一步看程序的出错问题,即Debug的使用, ...

  2. SQL Server公用表达式CET递归查询所有上级数据

    with cte as( select bianma,fjbm from #tree where chkDisabled='true' union all select t.bianma,t.fjbm ...

  3. Mint-UI 的 DatetimePicker 日期时间插件的安装与使用

    简介:Mint-UI是饿了么出品的基于vue的移动端组件库(element-ui是桌面端) 官网:http://mint-ui.github.io/docs/#/zh-cn2 项目环境:vue-cli ...

  4. linux操作之软件安装(一)

    rpm 包安装 RedHat Package Manager的缩写 , linux 的软件包可能存在依赖关系,比如某某依赖某某才能使用. 挂载一个光盘 mount -t auto /dev/cdrom ...

  5. CSS翻转小效果

    CSS3翻转显示另外一张图: 1.backface-visibility:hidden;背面不可见 2.transform:rotate();旋转 (可以把图片换成本地图片看一下效果) <!DO ...

  6. Linux3.5—视屏模块学习与分析

    插入USB摄像头后,我看到了识别出的一些信息,在内核源码中搜到了相关信息: 搜索之后,在uvc_driver.c 帮助文档:linux-3.5/Documentation/video4linux/v4 ...

  7. SSH Secure :Algorithm negotiation failed,反复提示输入password对话框

    在嵌入式开发中,SSH Secure File Transfer Client 软件使用,方便了windows和linux之间文件拷贝,尤其是多台主机状况下. 最近装了Ubuntu 16.0.4,在V ...

  8. Codeforces Round #482 (Div. 2) : Kuro and GCD and XOR and SUM (寻找最大异或值)

    题目链接:http://codeforces.com/contest/979/problem/D 参考大神博客:https://www.cnblogs.com/kickit/p/9046953.htm ...

  9. BINARYSEARCH有り無しのパフォーマンスの違い

    BINARY SEARCHを使用したパフォーマンス検証を行ってみた.この例では.BKPFが約1万件.BSEGが約3万件になるよう調整している.また.SQLの実行に係る時間は無視する事にする. サンプル ...

  10. 【8086汇编-Day6】关于loop的实验

    实验内容 因为是要复制代码,所以常规来做是取代码段地址来用,所以把cs值mov给ax,但是这只是临时的,ax之后还有别的用途,那就把指令当作数据来存(把ax值 mov给ds,表示这一段地址用作代码段, ...