zoukankan      html  css  js  c++  java
  • Priest John's Busiest Day HDU2491 ACM算法设计

    Priest John's Busiest Day

    Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

    Total Submission(s): 1104    Accepted Submission(s): 306

    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
     

    贪心。

    #include <iostream>
    #include <algorithm>
    //#include <fstream>
    using namespace std;
    
    struct Wed
    {    
        int s,t,arrive,leave,timeSpan;
        int operator < (Wed& w)
        {
            if(arrive == w.arrive)
                return t < w.t;
            else 
                return arrive < w.arrive;
        }
    };
    const int MAXn = 100010;
    Wed wed[MAXn];
    
    int main()
    {
        //ifstream cin("in.txt");
        int testCases;
        while (cin>>testCases,testCases)
        {
            //input
            for(int i = 0;i < testCases;i ++)
            {
                cin>>wed[i].s>>wed[i].t;
                wed[i].timeSpan = (wed[i].t - wed[i].s) / 2 + 1;
                wed[i].arrive = wed[i].t - wed[i].timeSpan;
            }
            sort(wed,wed+testCases);
            //Judge
            bool flag = 0;
            wed[0].leave = wed[0].s + wed[0].timeSpan;
            for(int i = 1;i < testCases;i ++)
            {
                if(wed[i].arrive < wed[i-1].leave)
                {
                    flag = 1;
                    break;
                }            
                wed[i].leave = (wed[i-1].leave > wed[i].s ? wed[i-1].leave : wed[i].s) + wed[i].timeSpan;
            }
            //output
            if(flag)
                cout<<"NO"<<endl;
            else
                cout<<"YES"<<endl;
        }
        return 0;
    }
    本博客所有博文,若无专门说明皆为原创,转载请注明作者和出处!
  • 相关阅读:
    线程池类型场景和问题
    react Antdesign Select添加全选功能
    API与ESB 、ServiceMesh、微服务究竟关系如何?
    RabbitMQ四种Exchange类型
    RabbitMq安装
    kafka 部署
    共享文件夹重启后每次都要输入密码
    algorithm 12 partial_sort_copy
    algorithm 11 nth_element
    algorithm 11 none_of
  • 原文地址:https://www.cnblogs.com/ifinver/p/3032591.html
Copyright © 2011-2022 走看看