USACO Milking Cows

Luo Gu P1204 [USACO1.2] milking Milking Cows

Luo Gu Portal

JDOJ 1656: Milking Cows

JDOJ Portal

Description

Three farmers get up at 5 every morning, then went to the bullpen to 3 milking cows. The first farmer to his 300 milking cows at the time (from 5:00 start time, in seconds), up to 1000 times. the second time farmers 700 begins and ends at time 1200. 1500 farmers third time start time of the end of 2100. the longest period with at least one milking farmer continuous time is 900 seconds (from time 300 to time 1200 ), and the longest continuous unmanned milking time (from the end of milking the milking until the start) to 300 seconds (from the time 1200 to time 1500).

Your task is to compile a program that reads a farmer with N (1 <= N <= 5000) working hours to squeeze a list of N cow, calculate the following

Two (are in seconds):

• longest period of at least one person in the milking.

• The longest period of time no one milking.

Input

Line 1: An integer N.

Lines 2..N + 1: Each row two non-negative integer less than 1,000,000, showing a farmer's start time and end time.

Output

Line, two integers, that is the subject of the required two answers.

Sample Input

3 300 1000 700 1200 1500 2100

Sample Output

900 300

answer:

I reiterate once again! The kinds of questions is very important ! ! !

Simulation algorithm is not to say, the key is this question how simulation.

If you think of the array mark, then you're a brother.

(Because I am beginning to do so)

So I consider the structure of the sort.

You think, now covering several segments with each other, so we think, first descending order according to the left endpoint of each interval, and then began to traverse all the interval, if the right end of the range than the current range (with a ll, rr keep both endpoints) bigger, then we update, and so on.

So we are dealing with a ans1, ans2, the final output on the line.

Look at the code to understand better equipped.

#include<bits/stdc++.h>
using namespace std;
int N; 
struct node
{
    int l,r;
}p[5005];
bool cmp(node a,node b)
{
    return a.l<b.l;
}
int main()
{
    scanf("%d",&N);
    for(int i=1;i<=N;i++)
        scanf("%d%d",&p[i].l,&p[i].r);
    sort(p+1,p+1+N,cmp);
    int ll=p[1].l;
    int rr=p[1].r;
    int ans1=0,ans2=0;
    for(int i=2;i<=N;i++)
    {
        if(p[i].l<=rr)
            rr=max(rr,p[i].r);
        else
        {
            ans1=max(ans1,rr-ll);
            ans2=max(ans2,p[i].l-rr);
            ll=p[i].l;
            rr=p[i].r;
        }
    }
    ans1=max(ans1,rr-ll);
    printf("%d %d",ans1,ans2);
    return 0;
}

Guess you like

Origin www.cnblogs.com/fusiwei/p/11294563.html