您的位置:首页 > 其它

UVa 1420 Priest John's Busiest Day 解题报告(贪心)

2014-07-23 17:01 387 查看

1420 - Priest John's Busiest Day

Time limit: 3.000 seconds

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

    解题报告: 给定n个任务,每个任务有起始时间si,ti。完成任务的时间大于起始时间段的一半,且为整数。只能在整数时间开始任务,且不能中断。问能否完成所有任务。

    按照贪心的思路,肯定是按照终止时间的先后排序,依次完成任务。不过这里的终止时间不是给定的ti了,而是在终止时间开始做该任务,能在ti前完成。也就是ti-wi。

    依次排序,遍历一遍即可。代码如下:

#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <set>
#include <map>
#include <vector>
#include <queue>
#include <string>
#include <iostream>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
#define ff(i, n) for(int i=0;i<(n);i++)
#define fff(i, n, m) for(int i=(n);i<=(m);i++)
#define dff(i, n, m) for(int i=(n);i>=(m);i--)
void work();
int main()
{
#ifdef ACM
freopen("in.txt", "r", stdin);
#endif // ACM

work();
}

/****************************************/

struct Node
{
int sta, end;
int w;

bool operator<(const Node & cmp) const
{
return end == cmp.end ? sta < cmp.sta : end < cmp.end;
}
} node[111111];

void work()
{
int n;
while(scanf("%d", &n) == 1 && n)
{
ff(i, n)
{
scanf("%d%d", &node[i].sta, &node[i].end);
node[i].w = (node[i].end-node[i].sta)/2+1;
node[i].end -= node[i].w;
}
sort(node, node+n);

bool flag = true;
int cur = 0;
ff(i, n)
{
cur = max(cur, node[i].sta);
if(cur > node[i].end)
{
flag = false;
break;
}
cur += node[i].w;
}

puts(flag?"YES":"NO");
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: