Priest John's Busiest Day UVA - 1420

问题

https://vjudge.net/problem/UVA-1420

分析

贪心,不能只看t,应该看最早结束时间来排序,剩下更多的时间进行接下来的任务
例子:
2
1 10
5 9

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <map>
#include <string>
#include <vector>
#include <algorithm>
#include <queue>
using namespace std;
typedef long long LL;
const int maxn=100000+5;
const double eps=1e-6;
struct Wed{
    int s,t,d;
    bool operator < (const Wed &rhs) const {
        //最早结束时间
        return s+d<rhs.s+rhs.d;
    }
}wedding[maxn];
int n;
int main(void){
    while(scanf("%d",&n)==1 && n){
        for(int i=0;i<n;++i) {
            scanf("%d%d",&wedding[i].s,&wedding[i].t);
            wedding[i].d=(wedding[i].t-wedding[i].s)/2+1;
        }
        sort(wedding,wedding+n);
        int cur=0;
        bool flag=true;
        for(int i=0;i<n;++i){
            cur=max(wedding[i].s,cur)+wedding[i].d;
            if(cur>wedding[i].t) {
                flag=false;
                break;
            }
        }
        if(flag) printf("YES\n");
        else printf("NO\n");
    }
    return 0;
}
发布了180 篇原创文章 · 获赞 3 · 访问量 3467

猜你喜欢

转载自blog.csdn.net/zpf1998/article/details/104902330