Priest John's Busiest Day (hdu 2491 greedy

Topic links: http://acm.hdu.edu.cn/showproblem.php?pid=2491

Greedy to find the midpoint of each segment because it is the presence of all the possible time period pastor's bound to be a point in time

Then press the Sort

#include<bits/stdc++.h>
using namespace std;
struct node
{
    int s,t,z,d;
}a[100005];
int n;
bool cmp(node x,node y)
{
    return x.z<y.z;
}
int main()
{
    int i,j;
    while(scanf("%d",&n),n)
    {
        for(i=0;i<n;i++)
        {
            scanf("%d%d",&a[i].s,&a[i].t);
            a[i].z=a[i].s+(a[i].t-a[i].s)/2;//中点 根据这个排序
            a[i].d=(a[i].t-a[i].s)/2+1;//需要牧师的时间段
        }
        sort(a,a+n,cmp);
        int last=0;
        int f=1;
        for(i=0;i<n;i++)
        {
            if(max(last,a[i].s)+a[i].d<=a[i].t)last=max(last,a[i].s)+a[i].d;
            else f=0;
        }
        if(f==0)cout<<"NO\n";
        else cout<<"YES\n";
    }
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/ydw--/p/11352576.html