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. 原生 JS 实现扫雷 (分析+代码实现)

    阅读这篇文章需要掌握的基础知识:Html5.CSS.JavaScript 在线Demo:查看 扫雷规则 在写扫雷之前,我们先了解下它的游戏规则 ● 扫雷是一个矩阵,地雷随机分布在方格上. ● 方格上的 ...

  2. 转:Java中的cas

    引自:https://blog.csdn.net/mmoren/article/details/79185862 本篇的思路是先阐明无锁执行者CAS的核心算法原理然后分析Java执行CAS的实践者Un ...

  3. Jquery之倒计时计算

      setInterval(); function setDate(setTime){ var date = new Date();//获取系统当前时间 )+)+"-"+date. ...

  4. 网页股票期货历史数据(API)

    //[和讯数据] //大商所DCE.郑商所CZCE.上期所SHFE3.中金所CFFEX //期货1分钟线http://webftcn.hermes.hexun.com/ ... I1709&d ...

  5. Mysql 关于处理NULL值的相关函数和操作符

    操作符 <=> NULL-safe equal. This operator performs an equality comparison like the = operator, bu ...

  6. 「PHP」设计模式介绍

    引言   最近再看PHP设计模式相关的一些技术文章,网上有关PHP的设计模式范例很少,这里做一些总结仅供参考,不足之处望提出. 参考资料: <大话设计模式>程杰   什么是设计模式   设 ...

  7. “子查询返回的值不止一个。当子查询跟随在 =、!=、<、<=、>、>= 之后,或子查询用作表达式时,这种情况是不允许的。”SQL查询错误解析

    为了实现下述代码,首先得有数据库和相应的表格,本文用的是https://blog.csdn.net/qaz13177_58_/article/details/5575711/中的案例,即先用链接中那些 ...

  8. rails应用中各数据平台的对接

    1.mongo #Gemfile添加如下两个gem包gem 'mongoid', '~> 5.1.0' gem 'mongo', '~> 2.4’ @client = Mongo::Cli ...

  9. 第四节 Go数据结构之栈

    一.什么是栈 这是杭州地铁1号线线路图.大卫哥考考大家,地铁列车如何调头? 我也不卖关子了,列车通常是用"人字轨道"来变换车道. 列车先从A轨道开进"人字轨道" ...

  10. SAP Odata実行命令(2)

    前言 $ skiptokenは.アプリケーションに送信されるエントリ数を制限するために使用されます. 膨大な数のエントリが要求された場合.これはパフォーマンスの向上にも役立ちます.次のリンクがアプリケ ...