[USACO 1.2.1] milking (fast row)

Topic: http: //hustoj.sinaapp.com/problem.php id = 1820?

N intervals and to take the input set, find the longest interval and the longest interval spacing.

Idea: fast start another row without taking into account if there is only one interval

#include <stdio.h>
#include <stdlib.h>

struct worktime
{
    int start;
    int end;
}time[5005];

int cmp(const void *a,const void *b)
{
    return ((worktime *)a)->start-((worktime *)b)->start;
}

int main ()
{
    int n;
    int workmax=0,restmax=0;
    scanf("%d",&n);
    for(int i=0;i<n;i++)
    {
        scanf("%d%d",&time[i].start,&time[i].end);
    }
    qsort(time,n,sizeof(worktime),cmp);
    
    int start=time[0].start;
    int end=time[0].end;
    int work=0;
    int rest=0;
    for(int i=1;i<n;i++)
    {
        if (time[i].start>end) 
        {
            if(workmax<(end-start)) workmax=(end-start);
            if(restmax<(time[i].start-end)) restmax=(time[i].start-end);
            start=time[i].start;
            end=time[i].end;
        }
        else 
        {
            if(time[i].end>end) end=time[i].end;
            if(workmax<(end-start)) workmax=(end-start);
        }
    }
    if(workmax==0) workmax=time[n-1].end-time[0].start;
    printf("%d %d\n",workmax,restmax);
}

 

Reproduced in: https: //www.cnblogs.com/danielqiu/archive/2012/12/24/2831117.html

Guess you like

Origin blog.csdn.net/weixin_34072458/article/details/93795236