hdu2491(贪心)

Priest John's Busiest Day

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1400    Accepted Submission(s): 402


Problem Description
John is the only priest in his town. October 26th is the John's busiest day in a year because there is an old legend in the town that the couple who get married on that day will be forever blessed by the God of Love. This year N couples plan to get married on the blessed day. The i-th couple plan to hold their wedding from time Si to time Ti. According to the traditions in the town, there must be a special ceremony on which the couple stand before the priest and accept blessings. Moreover, this ceremony must be longer than half of the wedding time and can’t be interrupted. Could you tell John how to arrange his schedule so that he can hold all special ceremonies of all weddings?

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
The input consists of several test cases and ends with a line containing a zero.


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
For each test, if John can hold all special ceremonies, print "YES"; otherwise, print “NO”.
 

Sample Input
  
  
3 1 5 2 4 3 6 2 1 5 4 6 0
 

Sample Output
  
  
NO YES
 

Source
 

Recommend
gaojie
 
         本题要定n场婚礼的起止时间并规定牧师出席每场婚礼的时间要超过婚礼时间的一半,求牧师是否能顺利出席所有的婚礼。
         本题是个贪心问题。我们需找到正确的贪心策略。本题的贪心策略为:
         将所有的婚礼按牧师出席最晚到达时间(过了这个时间就不能保证牧师出席每场婚礼的时间要超过婚礼时间的一半)从小到大排序,然后牧师依次出席每个婚礼。在当前婚礼下,若牧师到达时间大于最晚到达时间,则不能顺利出席该场婚礼,即牧师的行程不能满足,退出;否则继续进行下去,直到出席了所有的婚礼或有一场不能满足,退出。
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;

const int MAXN=100000+10;
struct node
{
	int start,end,last,tim;
}Wed[MAXN];
int n;

bool cmp(node a,node b)
{
	return a.last<b.last;
}

void Solve()
{
	int i,cur;
	for(cur=Wed[1].start,i=1;i<=n;)
	{
		if(cur<Wed[i].start)
			cur=Wed[i].start;
		if(cur<=Wed[i].last)
			cur+=Wed[i].tim;
		else break;
		i++;

	}
	if(i<=n)printf("NO\n");
	else printf("YES\n");
}

int main()
{
	int i,tmp;
	while(~scanf("%d",&n),n)
	{
		for(i=1;i<=n;i++)
		{
			scanf("%d%d",&Wed[i].start,&Wed[i].end);
			Wed[i].tim=(Wed[i].end-Wed[i].start)>>1;
			Wed[i].tim++;
			Wed[i].last=Wed[i].end-Wed[i].tim;
		}
		sort(Wed+1,Wed+1+n,cmp);

		Solve();
	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值