题意简介

有一个司仪,要主持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【贪心】的更多相关文章

  1. 图论(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 ...

  2. 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 ...

  3. 【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 ...

  4. poj 3686 Priest John's Busiest Day

    http://poj.org/problem?id=3683 2-sat 问题判定,输出一组可行解 http://www.cnblogs.com/TheRoadToTheGold/p/8436948. ...

  5. POJ 3683 Priest John's Busiest Day (2-SAT)

    Priest John's Busiest Day Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 6900   Accept ...

  6. POJ 3683 Priest John's Busiest Day(2-SAT+方案输出)

    Priest John's Busiest Day Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 10010   Accep ...

  7. Priest John's Busiest Day(POJ 3683)

    原题如下: Priest John's Busiest Day Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 12162   ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. android 屏幕切换

    1.将Activity固定位竖屏可以在配置文件这么写 <activity android:screenOrientation="portrait"> 横屏显示: < ...

  2. 【第十一篇】这一篇来说说MVC+EF+easyui datagrid的查询功能

    老规矩 直接上代码 <form class="form-horizontal"> <div class="box-body"> < ...

  3. 无法解析的外部符号,该符号在xxx函数中被引用

    无法解析的外部符号.........,该符号在函数.........被引用 在我们敲代码的过程中,我们偶尔会遇到这个问题,这个问题大多数都是因为你自己的程序有问题,而不是缺少相应的库文件.话不多说,直 ...

  4. ES6入门八:Promise异步编程与模拟实现源码

    Promise的基本使用入门: ——实例化promise对象与注册回调 ——宏任务与微任务的执行顺序 ——then方法的链式调用与抛出错误(throw new Error) ——链式调用的返回值与传值 ...

  5. (三)Spring 高级装配 bean的作用域@Scope

    1.默认情况下,spring通过@Autowared注入的bean是单例的bean,但有些情况是不满足的,例如:购物车,每个会话,或每个用户登录使用的购物车都是独立的 spring的定义的作用域: a ...

  6. 将SpringBoot部署在外部tomcat中

    一,前言 在文章SpringBoot之简单入门中提到了,SpringBoot是内置一个tomcat容器的,但是如果要将SpringBoot部署在一个外部的tomcat,要怎么办呢?这就是本篇文章的目的 ...

  7. Github 持续化集成 工作流 Npm包自动化发布

    Github 持续化集成 工作流 Npm包自动化发布 简介   持续集成指的是,频繁地(一天多次)将代码集成到主干. 它的好处主要有两个: 快速发现错误.每完成一点更新,就集成到主干,可以快速发现错误 ...

  8. 格子游戏Grid game CodeForce#1104C 模拟

    题目链接:Grid game 题目原文 You are given a 4x4 grid. You play a game — there is a sequence of tiles, each o ...

  9. idea创建javaweb原生项目

    使用idea创建javaweb项目 idea还是写框架项目比较爽,原生的javaweb项目不是特别方便,这篇文章就是记录一下创建的过程 图较多注意流量 选择创建web项目 配置tomcat服务器 配置 ...

  10. MySQL在Linux系统环境的安装和无主机登录配置

           将mysql 安装在单个Linux系统主机,并配置本地或远程(此处可仅单指局域状态下的环境)的无主机登录.谨记的是:操作完mysql 设置时,需以flush privileges进行权限 ...