Three farmers rise at 5 am each morning and head for the barn to milk three cows. The first farmer begins milking his cow at time 300 (measured in seconds after 5 am) and ends at time 1000. The second farmer begins at time 700 and ends at time 1200. The third farmer begins at time 1500 and ends at time 2100. The longest continuous time during which at least one farmer was milking a cow was 900 seconds (from 300 to 1200). The longest time no milking was done, between the beginning and the ending of all milking, was 300 seconds (1500 minus 1200).
Your job is to write a program that will examine a list of beginning and ending times for N (1 <= N <= 5000) farmers milking N cows and compute (in seconds):
- The longest time interval at least one cow was milked.
- The longest time interval (after milking starts) during which no cows were being milked.
PROGRAM NAME: milk2
INPUT FORMAT
Line 1: | The single integer, N |
Lines 2..N+1: | Two non-negative integers less than 1,000,000, respectively the starting and ending time in seconds after 0500 |
SAMPLE INPUT (file milk2.in)
3 300 1000 700 1200 1500 2100
OUTPUT FORMAT
A single line with two integers that represent the longest continuous time of milking and the longest idle time.SAMPLE OUTPUT (file milk2.out)
900 300
ID:名字
PROG:milk2
LANG:C++
*/
#include<iostream>
#include<algorithm>
#include<fstream>
using namespace std;
int N,T1,T2;
int lianxu = 0;
int jianduan = 0;
struct node{
int start;
int end;
};
int cmp(const node a,const node b)
{
if(a.start != b.start)
{
return a.start < b.start ;
}
else
{
return a.end < b.end ;
}
}
int main()
{
ofstream fout("milk2.out");
ifstream fin("milk2.in");
fin>>N;
node duan[5005];
for(int i = 0; i < N; i++)
{
fin>>duan[i].start >>duan[i].end ;
}
sort(duan,duan + N,cmp);
T1 = duan[0].start ;
T2 = duan[0].end ;
for(int i = 0; i < N; i++)
{
if(T2 >= duan[i].start )
{
T2 = max(duan[i].end ,T2) ;
lianxu = max(T2-T1,lianxu);
}
else
{
T1 = duan[i].start ;
jianduan = max(jianduan,T1 - T2);
T2 = duan[i].end ;
}
}
fout<<lianxu<<" "<<jianduan<<endl;
}
USER: Ting Yu [15209182] TASK: milk2 LANG: C++ Compiling... Compile: OK Executing... Test 1: TEST OK [0.000 secs, 4180 KB] Test 2: TEST OK [0.000 secs, 4180 KB] Test 3: TEST OK [0.000 secs, 4180 KB] Test 4: TEST OK [0.000 secs, 4180 KB] Test 5: TEST OK [0.000 secs, 4180 KB] Test 6: TEST OK [0.000 secs, 4180 KB] Test 7: TEST OK [0.000 secs, 4180 KB] Test 8: TEST OK [0.000 secs, 4180 KB] All tests OK.Your program ('milk2') produced all correct answers! This is your submission #12 for this problem. Congratulations!
Here are the test data inputs:
------- test 1 [length 10 bytes] ---- 1 100 200 ------- test 2 [length 34 bytes] ---- 4 100 200 201 301 302 402 403 503 ------- test 3 [length 54 bytes] ---- 10 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 1 20 ------- test 4 [length 36 bytes] ---- 3 100 102 64100 64102 128100 128102 ------- test 5 [length 1291 bytes] ---- 100 99000 101000 98000 102000 97000 103000 96000 104000 95000 105000 94000 106000 93000 107000 92000 108000 91000 109000 90000 110000 89000 111000 88000 112000 87000 113000 86000 114000 85000 115000 84000 116000 83000 117000 82000 118000 81000 119000 80000 120000 79000 121000 78000 122000 77000 123000 76000 124000 75000 125000 74000 126000 73000 127000 72000 128000 71000 129000 70000 130000 69000 131000 68000 132000 67000 133000 66000 134000 65000 135000 64000 136000 63000 137000 62000 138000 61000 139000 60000 140000 59000 141000 58000 142000 57000 143000 56000 144000 55000 145000 54000 146000 53000 147000 52000 148000 51000 149000 50000 150000 49000 151000 48000 152000 47000 153000 46000 154000 45000 155000 44000 156000 43000 157000 42000 158000 41000 159000 40000 160000 39000 161000 38000 162000 37000 163000 36000 164000 35000 165000 34000 166000 33000 167000 32000 168000 31000 169000 30000 170000 29000 171000 28000 172000 27000 173000 26000 174000 25000 175000 24000 176000 23000 177000 22000 178000 21000 179000 20000 180000 19000 181000 18000 182000 17000 183000 16000 184000 15000 185000 14000 186000 13000 187000 12000 188000 11000 189000 10000 190000 9000 191000 8000 192000 7000 193000 6000 194000 5000 195000 4000 196000 3000 197000 2000 198000 1000 199000 0 200000 ------- test 6 [length 52 bytes] ---- 6 100 200 200 400 400 800 800 1600 50 100 1700 3200 ------- test 7 [length 11308 bytes] ----