Question:
Three farmers get up at every morning and then go to the cowshed to milk three cows. The first farmer milked his ox in 300 seconds (starting from) until 1000 seconds. The second farmer started in 700 seconds and ended in 1200 seconds. The third farmer ended in 1500 seconds. During the period, at least one farmer has been milking for 900 seconds (from 300 seconds to 1200 seconds), and the longest continuous time (from milking to milking) it is 300 seconds (from 1200 seconds to 1500 seconds ).
Your task is to compile a program, read a list of N farmers (1 <= N <= 5000) Working hours of ntou cattle, and calculate the following two points (both in seconds):
At least one person is in the milking period.
The longest period of time for unattended milking. (Starting from someone milking)
Question: Set an array to indicate the current time point. The initialization value is 0. Each time you read the start end point, add one at the start point, subtract one from the end point, and then traverse from the start, use sum to add the value of each time point. When sum is 0, it indicates no milking. If sum is greater than 0, it indicates that the milking is performed.
Code:
[Cpp]
/*
ID: lishicao
PROG: milk2
LANG: C ++
*/
# Include <iostream>
# Include <fstream>
# Include <cstring>
Using namespace std;
Int vis [1000500];
Ifstream fin ("milk2.in ");
Ofstream fout ("milk2.out ");
Int main ()
{
Int N;
Int start, end, Max = 0, Min = 99999999, milked = 0, notmilked = 0;
Memset (vis, 0, sizeof (vis ));
Fin> N;
While (N --)
{
Fin> start> end;
If (end> Max) Max = end;
If (start <Min) Min = start;
Vis [start] ++;
Vis [end] --;
}
Int sum = 0;
Int Count = 0;
Int flag = 0;
For (int I = Min; I <= Max; I ++)
{
Sum + = vis [I];
If (flag = 0 ){
If (sum = 0 ){
Flag = 1;
If (milked <Count) milked = Count;
Count = 0;
}
Count ++;
} Www.2cto.com
Else {
If (sum> 0 ){
Flag = 0;
If (notmilked <Count) notmilked = Count;
Count = 0;
}
Count ++;
}
}
Fout <milked <"" <notmilked <endl;
Return 0;
}