• 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 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”.

    题目大意:有一个牧师要给大家举办婚礼,其中有一个祝福仪式,这个仪式的时间要大于婚礼的时间的一半(注意是大于)并且时间要是整数,而且不能中断。给n个婚礼的开始时间和结束时间,问能否合理安排祝福仪式使所有婚礼都受到祝福。

    思路:注意到仪式的时间要大于婚礼的一半,那么这个仪式必然经过婚礼的中间时间,即(S+T)/2。所以若存在合理的方案,那么祝福仪式的举行顺序一定是和(S+T)/2从小到大排序的顺序一样。排好序后进行贪心选择,让每个仪式尽量靠前(为后来的仪式腾出尽量多的时间),从小到大逐一枚举即可。

    PS:这题有一点比较坑爹的地方是你要算中间时间可能会用到(S+T)/2,S+T可能会爆int(可以写S +(T - S)/ 2)。我居然能发现这个坑爹的东西然后果断移项然后1A了好开心好开心O(∩_∩)O~~

    PS2:这题跟之前做过的某道2-SAT背景几乎一模一样我差点还以为自己做过了……

    代码(359MS):

     1 #include <cstdio>
     2 #include <cstring>
     3 #include <iostream>
     4 #include <algorithm>
     5 using namespace std;
     6 
     7 const int MAXN = 100010;
     8 const int INF = 0x3fff3fff;
     9 
    10 struct Node {
    11     int s, t;
    12     bool operator < (const Node &rhs) const {
    13         return s - rhs.s < rhs.t - t;//(s + t) < (rhs.s + rhs.t);
    14     }
    15 } wed[MAXN];
    16 
    17 int n;
    18 
    19 bool solve() {
    20     sort(wed, wed + n);
    21     int last = 0;
    22     for(int i = 0; i < n; ++i) {
    23         int len = (wed[i].t - wed[i].s) / 2 + 1;
    24         if(last + len > wed[i].t) return false;
    25         last = max(last, wed[i].s) + len;
    26     }
    27     return true;
    28 }
    29 
    30 int main() {
    31     while(scanf("%d", &n) != EOF) {
    32         if(n == 0) break;
    33         for(int i = 0; i < n; ++i) scanf("%d%d", &wed[i].s, &wed[i].t);
    34         if(solve()) puts("YES");
    35         else puts("NO");
    36     }
    37 }
    View Code
  • 相关阅读:
    猜数字游戏(补)
    团队项目五(项目回顾)
    项目回顾
    第二次阶段冲刺
    团队项目(任务三):第一次冲刺
    个人项目(一):新猜数字
    课后作业(一)
    团队任务二
    团队任务(一)
    课后作业(一)
  • 原文地址:https://www.cnblogs.com/oyking/p/3258601.html
Copyright © 2020-2023  润新知