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 ...
随机推荐
- vue2.0 创建项目
准备 安装淘宝 npm镜像 npm install -g cnpm --registry=https://registry.npm.taobao.org 添加系统变量path的内容 因为cnpm会被安 ...
- 01 jvm学习过程概述
声明:本博客仅仅是一个初学者的学习记录.心得总结,其中肯定有许多错误,不具有参考价值,欢迎大佬指正,谢谢!想和我交流.一起学习.一起进步的朋友可以加我微信Liu__66666666 这是简单学习一遍之 ...
- activity的隐式和显式启动
显式Intent(Explicit intent):通过指定Intent组件名称来实现的,它一般用在知道目标组件名称的前提下,一般是在相同的应用程序内部实现的. 隐式Intent(Implicit i ...
- 将字符串转换成json格式
1.引入json依赖,在pom.xml文件里添加如下内容 <!--Json array start --> <dependency> <groupId>common ...
- html常见的块元素与内联(行内)元素用法说明(一)
html平时常见的块元素有:div, p, h1, h2, h3等,内联元素有:span, a, img等. 块元素的属性:无论内容是什么,都会独占一整行.主要用于页面布局. 内联元素的属性:只占自身 ...
- Linux常用命令(常用)
一,Linux常用命令二,CentOS没有盘符,只有根目录/,可以理解为只有一个盘有一个文件夹有点特殊,画了一个房子,代表当前用户的家 1.记住一个点:区分根目录和当前用户的家cd ~ 代表回家cd ...
- [Job] 找工作小结
有近2个月没有更新博客,主要精力放在了投递会议论文和秋招找工作方面.这里简单总结一下秋招笔试面试的几点建议和感受. 投递的NLP算法工程师岗位,主要参加过面试的公司有腾讯(春招),蚂蚁金服(春招),追 ...
- Mycat 配置文件server.xml
server.xml 几乎保存了所有 mycat 需要的系统配置信息. 1.system 标签: 该标签内嵌套的所有 property 标签都与系统配置有关. charset 属性: 该属性用于字符集 ...
- 12-z-index
z-index 这个东西非常简单,它有四大特性,每个特性你记住了,页面布局就不会出现找不到盒子的情况. z-index 值表示谁压着谁,数值大的压盖住数值小的, 只有定位了的元素,才能有z-index ...
- One layer SoftMax Classifier, "Handwriting recognition"
import lib needed¶ In [1]: from PIL import Image import numpy as np import matplotlib.pyplot as ...