UVA1420 Priest John's Busiest Day【贪心】
题意简介
有一个司仪,要主持n场婚礼,给出婚礼的起始时间和终止时间,每个婚礼需要超过一半的时间做为仪式,并且仪式不能终止。问说司仪能否主持n场婚礼。
输入格式
多组数据,每组数据输入一个\(N\)(\(N\)<=100000),接下来N行,每行输入\(Si\),\(Ti\)两个数,当输入\(n=0\)时输入结束
输出格式
每行对应每组数据,用"YES"或"NO"代表能否主持完n场婚礼
算法分析
一眼贪心,要求主持完全部婚礼,每个婚礼主持时间为 \((Ti-Si)/2+1\) 因为时间必须超过一半,所以要加一
然后按照每个婚礼结束时间排序,贪心策略优先选择越早结束的婚礼解决,留时间解决后面的婚礼,然后Judge判断一下就可以了
代码
#include<bits/stdc++.h>
#define re register
#define ll long long
using namespace std;
inline int read()
{
int k=1,sum=0;
char c=getchar();
for(;c<'0' || c>'9';c=getchar()) if(c=='-') k=-1;
for(;c>='0' && c<='9';c=getchar()) sum=sum*10+c-'0';
return sum*k;
}
const int N=1e5+10;
int n;
struct Task{
int s,t,d;
}a[N];
inline bool cmp(Task x,Task y){
return x.s+x.d<y.s+y.d;
}
inline bool Judge(){
int cnt=0;
for(re int i=1;i<=n;++i) {
cnt=max(cnt,a[i].s)+a[i].d;
if(cnt>a[i].t) return 0;
}
return 1;
}
int main()
{
while(scanf("%d",&n)==1 && n>0) {
for(re int i=1;i<=n;++i) {
a[i].s=read(),a[i].t=read();
a[i].d=((a[i].t-a[i].s)>>1)+1;
}
sort(a+1,a+n+1,cmp);
cout<<((Judge()==1?"YES":"NO"))<<endl;
}
return 0;
}
/*
3
1 5
2 4
3 6
2
1 5
4 6
0
*/
UVA1420 Priest John's Busiest Day【贪心】的更多相关文章
- 图论(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 ...
- 【POJ3683】Priest John's Busiest Day
题目 John is the only priest in his town. September 1st is the John's busiest day in a year because th ...
- poj 3686 Priest John's Busiest Day
http://poj.org/problem?id=3683 2-sat 问题判定,输出一组可行解 http://www.cnblogs.com/TheRoadToTheGold/p/8436948. ...
- POJ 3683 Priest John's Busiest Day (2-SAT)
Priest John's Busiest Day Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 6900 Accept ...
- POJ 3683 Priest John's Busiest Day(2-SAT+方案输出)
Priest John's Busiest Day Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10010 Accep ...
- Priest John's Busiest Day(POJ 3683)
原题如下: Priest John's Busiest Day Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 12162 ...
- HDU 2491 Priest John's Busiest Day(贪心)(2008 Asia Regional Beijing)
Description John is the only priest in his town. October 26th is the John's busiest day in a year be ...
- POJ 3683 Priest John's Busiest Day(2-SAT 并输出解)
Description John is the only priest in his town. September 1st is the John's busiest day in a year b ...
随机推荐
- android 定时提醒 - Notification
定时弹出 demo ,代码如下: 1.MainActivity.java public class MainActivity extends Activity implements OnClickLi ...
- BeetleX之FastHttpApi服务使用详解
BeetleX是开个轻量级高性能的开源TCP通讯应用框架,通过BeetleX可以轻松扩展不同场的TCP应用服务和客户端组件.框架开源地址:https://github.com/IKende/Beetl ...
- Kali Linux 安装open-vm-tools
Kali Linux是基于Debian的Linux发行版,集成了精心挑选的渗透测试和安全审计的工具,供渗透测试和安全设计人员使用.(以及一些各种颜色的hacker ^-^) 首先需要安装好虚拟机(V ...
- 反射的应用、获取Class类实例
*准备工作: 包结构:* ======================= Person.java代码: package com.atguigu.java; public class Person { ...
- Windows server 2008 快速搭建域环境
之前根据网上的教程搭建,然后出现了很多问题,最后摸索出了一个比较稳妥一点的方法. 对于选系统这里,虽然上一篇文章已经说过了,这里也再强调一下,我使用的是08的系统,使用其他系统的暂不做评价,使用08系 ...
- Linux中安装传输的命令行工具
创建lrzsz目录: mkdir /usr/local/lrzsz 进入lrzsz目录: cd /usr/local/lrzsz 下载lrzsz压缩包: wget http://www.ohse.de ...
- shell 获取当前目录下的jar文件
1.示例 function getDir() { ` do fileName=$"/"$item if [ -d $fileName ] then echo $fileName&q ...
- 动态set mybatis与ibatis的写法
mybatis: <set> <if test="obj.buyerId != null"> buyerId = #{obj.buyerId}, </ ...
- HTML学习之轮播图
这可以说是一种非常简单的实现轮播图的方法了,由于时间仓促所以没写自动轮播部分.简单说一下原理吧,就是把所有图片堆叠在一个盒子里,设置所有图片的透明度为0,这样就把所有图片都隐藏了,第一张图片除外(第一 ...
- 树上数据结构——LCT
目录 树上数据结构--LCT 概述 基本概念 核心操作 其他操作 完整模板 树上数据结构--LCT 概述 LCT是一种强力的树上数据结构,支持以下操作: 链上求和 链上求最值 链上修改 子树修改 子树 ...