题意简介

有一个司仪,要主持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. 「小技巧」使用Git从其他分支merge个别文件

    小明发现在实际项目开发过程中,总会遇到各种各样的情况,比如一个大型的项目或版本迭代可能不是一次上线,可能会分好几次上线,这时候就会涉及创建多个分支,分别开发. 项目背景 产品经理:我们本次开发三个功能 ...

  2. Redis继续学习

    1.Redis一共16个数据库 # Set the number of databases. The , you can select # a different one on a per-conne ...

  3. graphics.drawRect()方法

    drawRect方法的官方API文档描述 drawRect public void drawRect(int x, int y, int width, int height) Draws the ou ...

  4. POJ - 3984 迷宫问题 (搜索)

    Problem Description 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, ...

  5. 360大牛 全面解读 PHP面试

    360大牛全面解读PHP面试 第1章 课程介绍 让大家了解基本面试流程和面试的核心要求以及意义是什么并理解PHP面试考点主要以基础为核心,说明PHP面试考察范围. 第2章 PHP基础知识考察点 本章主 ...

  6. Day4 总结

  7. PTA A1011&A1012

    A1011 World Cup Betting (20 分) 题目内容 With the 2010 FIFA World Cup running, football fans the world ov ...

  8. Vue学习之todolist删除功能

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  9. MybatisPlus报错Invalid bound statement (not found)的解决方案

    今天使用MybatisPlus,测试时报错Invalid bound statement (not found) 使用自定义的mapper接口中的方法可以执行,而调用MybatisPlus中baseM ...

  10. selenium介绍及环境安装

      selenium简介: seelenium是web应用程序自动化工具,通过自动操作浏览器,进行点击.输入.回车.返回等来模拟用户的真实行为.如何理解和看待selenium项目,selenium官方 ...