UV-1420 Priest John & amp; #39; s Busiest Day

Source: Internet
Author: User

UV-1420 Priest John & #39; s Busiest Day

There is a ceremonies that host n weddings and give the start time and end time of the wedding. Each wedding takes more than half of the time as the ceremony, and the ceremony cannot be terminated. Ask if the ceremonies can host n weddings.

Solution: greedy. In order to host as many weddings as possible, every ceremony time must be as short as possible d = (t-s)/2 + 1 (because it must be greater than half, so add 1 ). Then, sort t-d according to the last ending time of each wedding. (to satisfy all the weddings, try to solve the early ceremony and free up time for the subsequent wedding ), maintain a time value.

#include 
  
   #include using namespace std;struct Wedding {    int S, T, D;    bool operator < (const Wedding& a) const {        return T - D < a.T - a.D;    }} W[100010];bool judge(int N) {    int cur = 0;    for (int i = 0; i < N; i++) {        cur = max(cur, W[i].S) + W[i].D;        if (cur > W[i].T)            return false;    }    return true;}int main() {    int N;    while (scanf(%d, &N) && N) {        for (int i = 0; i < N; i++) {            scanf(%d%d, &W[i].S, &W[i].T);            W[i].D = (W[i].T - W[i].S) / 2 + 1;        }        sort(W, W + N);        printf(%s, judge(N) ? YES : NO);    }    return 0;}
  

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.