Description
Vettel, Fernando Alonso, Hamilton at the F1 Shanghai Station to race, each person must enter the repair station to change the tires once (who out of the boring rules?!) ), and can only be used in the maintenance station once.
Fernando Alonso has been in the pit for 20 seconds to get out of the station for 90 seconds. The Vettel starts in 60 seconds and ends in 130 seconds. Hamilton ended at 220 seconds in 160 seconds. The longest period of time at least one rider in the repair area is 110 seconds (from 20 seconds to 130 seconds), while the longest continuous time in the repair area (from the point of someone entering the depot to the service station) is 30 seconds (from 130 seconds to 160 seconds).
Assuming that there is a race of n riders now, your task is to make up a program that reads a list of working times for N Riders (1 <= n <= 5000) into n times, and calculates the following two points (all in seconds):
The maximum length of time at which at least one person is in the repair station.
The longest unmanned time period in the depot. (starting from someone's pit stop)
Input Format
Line 1th: an integer n.
Lines 2nd to n+1: Two non-negative integers less than 1000000 per line, indicating a rider's pit stop and exit time.
Output Format
One line, two integers, which is the two answers required by the topic.
Sample Input
3200 900600 13001600 2200
Sample Output
1100 300
The most intuitive idea is to combine the time of a car that can be merged into a period of time, build a number of intervals that are not coincident and discontinuous, and then you can easily get the answer.
When building, you can consider the first of these intervals (according to the start time order), convenient logic processing.
In the process of merging, two intervals are classified and discussed. There are cross (equal) inclusions or no intersections at all.
#include <iostream>#include<stdlib.h>using namespacestd;structcarperiod{intstart; intend;}; Carperiod cars[5001];carperiod newcars[5001];intCmp_carperiod_1 (Const void* _a,Const void*_b) {Carperiod* A = (carperiod*) _a; Carperiod* B = (carperiod*) _b; return(*a). Start-(*b). Start; intMainintargcChar Const*argv[]) { intN;cin>>N; for(inti =0; i < N; ++i) Cin>>cars[i].start>>Cars[i].end; intmaxin=0, maxout=0; Qsort (Cars, N,sizeof(Carperiod), cmp_carperiod_1);//Sort by time of entry intNewn =0;//number of new time periodsNewcars[newn].start= cars[0].start;//first time periodNewcars[newn].end = cars[0].end; //Objective: To combine all the overlapping time periods . for(inti =1; i < N; ++i) {if(Cars[i].start <= newcars[newn].end and cars[i].end >= newcars[newn].end) {//Cross RelationshipNewcars[newn].end = Cars[i].end;//Pick up } Else if(Cars[i].start >newcars[newn].start and cars[i].end<newcars[newn].end)Continue;//include relationship does not take any action Else{newn++;//create a new fragment without intersectionNewcars[newn].start =Cars[i].start; Newcars[newn].end=Cars[i].end; } } //cout<< "------" <<endl; //for (int i = 0; I <= newn; ++i)//cout<<newcars[i].start<< "" <<newcars[i].end<<endl; //cout<< "------" <<endl; //Maxin for(inti =0; I <= newn; ++i) {intPeriod = (Newcars[i].end-Newcars[i].start); if(Period >maxin) Maxin=period; } //Maxout for(inti =1; I <= newn; ++i) {intInterval = newcars[i].start-newcars[i-1].end; if(Interval >maxout) Maxout=interval; } cout<<maxIN<<" "<<maxOUT<<Endl; return 0;}/*510 10110 10180 102*/
View Code
"Algorithmic Learning Notes" 37. Interval merging problem SJTU OJ 1262 milking Cow