Milking Cows milking USACO ordering simulation

1005: 1.2.1 Milking Cows milking

Time limit: 1 Sec   Memory Limit: 128 MB
submit: 15   Solution: 9
[ submit ] [ state ] [ Discussion Board ] [proposition man: External Import]

Title Description

1.2.1 Milking Cows milking    

(Milk2.pas / c / cpp) 

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

Your task is to compile a program that reads a farmer with N (1 <= N <= 5000) N cows crowded list of working hours, calculate the following two points (both in seconds): 

  • The longest period of at least one person in the milking.
  • The longest period unmanned milking. (Begin to run from someone milking



File name: milk2 

Input: 

(file milk2.in) 

Line 1: 

An integer N. 

Lines 2..N+1: 

Each line of the two non-negative integers less than 1000000, a farmer represents the start time and end time. 



Output: 

(file milk2.out) 

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

 

prompt

 

Source / classification

 

 

My God this question Is it so difficult

There are n (<= 5000) intervals

These n intervals between 0-1000000

ps personally think that is a simple sort + analog. . .

I think that the first row of these intervals in accordance with a sequence start time (fast discharge is relatively simple it nlogn)

So? Set up a pointer from a maximum run time of 0- +100 0000 and then again it is certainly time-out ah. . . .

 

 

It seems not so full ooo, ooo ~ ~ ~ ~

 

Aoao wailing seemed to understand between 1-1000000 In fact, there are still many points to no sweep

And since sweeping directly from 1-1000000 time out as long as the starting point for n intervals of sorted n intervals after this sweep from front to back again not to OK it +5000 certainly not time out it! ! ! (Secretly say: Yes! God ye what I just did not expect!)

 

That how when analog sweep it?

Hmmmm ...

I thought for a moment and then look at the title bar

 

 

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

The longest period unmanned milking. (Begin to run from someone milking

 

 

The first fight as long as a ring on the line next enumeration in the past when the interval to see if there is no pick up or overlap in fact, is whether or not connected

Simple point is then determined whether the start of the current interval and less tmpend tmpend each interval and the current takes a maximum value Gallants

 

 

the second. . God supposed. . Situ ignorant circle

Stop, stop Think again O (∩_∩) O haha ​​~

If the current interval begin larger than tmpend

It is equivalent to a connected and can not guarantee no other blending the interval came in between

why"

In fact, very simple tmpend actually stored is up to the maximum range of the current interval after the rearmost end value

So the most rearward in tmpend that tmpend ~ current course, no other interval between begin enumerate interval friends

6666

Knocked try it

 AC Ryo!

My code variable names may be very easy to understand O (∩_∩) O haha ​​~

 

 

There is also a very important issue

以后打擂台的时候假如要寻找最大值

初始的maxn一定要设成0 别设成-1 比如这一个题的最长的无人时间 可能每时每刻都有人啊  那你要是一开始的时候设得-1就输出了-1  其实应该是 0 !

下一次不要再犯错误了(~ ̄▽ ̄)~

#include<bits/stdc++.h>
using namespace std;
struct Node{
    int Begin,End;//结构体大法好 
}milk[5005];
int cmp(Node a,Node b){
    return a.Begin<b.Begin;//其实那个End不用考虑啊 
}
int main(){
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
        scanf("%d%d",&milk[i].Begin,&milk[i].End);
    sort(milk+1,milk+n+1,cmp);//sort排序一下 再加一个cmp
    int tmpend=milk[1].End   ,   max_at_least_1=milk[1].End-milk[1].Begin   ,   max_nobody=0;
    int at_least_1_tot=milk[1].End-milk[1].Begin;
    for(int i=2;i<=n;i++){
        if(milk[i].Begin<=tmpend){//区间相连
            if(milk[i].End>=tmpend)
                at_least_1_tot+=milk[i].End-tmpend;
            tmpend=max(tmpend,milk[i].End);
        }
        else{//有空隙 
            max_at_least_1=max(max_at_least_1,at_least_1_tot);//取最大值 
            at_least_1_tot=milk[i].End-milk[i].Begin;//还原重新累计 
            max_nobody=max(max_nobody,milk[i].Begin-tmpend);
            tmpend=max(tmpend,milk[i].End);
        }
    } 
    printf("%d %d", max_at_least_1 , max_nobody );
     
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/Tidoblogs/p/11291670.html