HDU 2491 Priest John's Busiest Day(贪心)(2008 Asia Regional Beijing)
Description
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
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
题目大意:有一个牧师要给大家举办婚礼,其中有一个祝福仪式,这个仪式的时间要大于婚礼的时间的一半(注意是大于)并且时间要是整数,而且不能中断。给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)的更多相关文章
- HDU 2491 Priest John's Busiest Day
贪心.. #include<iostream> #include<string.h> #include<math.h> #include <stdio.h&g ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 图论(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 ...
- 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 ...
随机推荐
- VMware Workstation 安装Vmware tools 是 出现vmware tools unavailable
这个问题是因为虚拟机安装的时候操作系统选择的不对,在Virtual Machine Settings中选择Options,在General中选择正确的操作系统类型 例如Guest operating ...
- npm常见配置收集
npm代理设置为走ss通道:npm config set proxy 'http://localhost:1080'
- 解决thinkphp query()执行原生SQL语句成功结果报错的问题
1.query方法 query方法用于执行SQL查询操作,如果数据非法或者查询错误则返回false,否则返回查询结果数据集(同select方法). 2.execute方法 execute用于更新和写入 ...
- ES5拓展
一.JSON拓展 1.JSON.parse(str,fun):将JSON字符串转为js对象 两个参数:str表示要处理的字符串:fun处理函数,函数有两个参数,属性名.属性值 // 定义json字符串 ...
- php 算法(冒泡排序)
//冒泡排序(从小到大) (从大到小改变for循环中的大于号为小于号即可) public function index(){ $arr = array(1,8,3, ...
- OpenWrt超时检测
参考http://www.right.com.cn/forum/thread-261702-1-1.html vim /home/ihid/chaos_calmer/feeds/luci/module ...
- 『Python基础-13』函数 Function
这篇笔记记录的知识点: 函数的基本概念 自定义函数 函数的几种参数 编程的三种方式: 1.OOP 面向对象编程,万物皆对象,以class为主,抽象化 2.POP 面向过程编程,万事皆过程,def定义过 ...
- 从Github开源项目《云阅》所学到的知识
感谢开源,感谢大神,才让我们这些菜鸟成长! 附上云阅开源项目地址:点我吧. 1.轮播图的实现. 现在的APP基本都会实现这个功能吧,然后一直都找不到好的第三方库,能够满足各种需求.然而碰到了这个开源库 ...
- JetBrains Makes its Products Free for Students(JetBrains 对学生免费了)
只要你有大学有些 后缀是 .edu的 如:@buaa.edu.cn,用你的邮箱注册,就可以免费试用 JetBrains了 下面是详细注册步骤: Hello everyone, If you’re o ...
- (AOSP)repo checkout指定版本
aosp 怎么切换分支? To properly switch Android version, all you need to change is branch for your manifest ...