Milk2 Problem Solving report--icedream61 Blog Park (reproduced please specify the source)
--------------------------------------------------------------------------------------------------------------- ---------------------------------
Topic
N farmers, each farmer from l[i] to r[i] time to milk the cows. Ask the longest time a person has been milking, and the longest time when no one is milking the milk.
"Data Range"
1<=n<=5000
0<=l[i],r[i]<=1,000,000
"Input Sample"
3
300 1000
700 1200
1500 2100
"Output Example"
900 300
--------------------------------------------------------------------------------------------------------------- ---------------------------------
Analysis
First, the N farmers are sorted according to L[i] and then sequentially swept.
I used sum[0] to record the longest period of time when no one was milking, using sum[1] to record the longest time a person has been milking.
Using LT to record the currently considered start time, RT records the currently considered end time.
"A←b" means to update a with B, i.e. if (b>a) a=b;
Start lt=l[1]; RT=R[1];
If r[i]<l[i+1], stating I and i+1 time separate, then sum[0]←l[i+1]-rt; Sum[1]←rt-lt;
Otherwise, the description i+1 can and I milking time connected, then rt←r[i+1];
The algorithm description is complete, be careful not to write wrong just fine.
--------------------------------------------------------------------------------------------------------------- ---------------------------------
Summary
Once again AC.
Review the next Quick row ~
And because of the code directly on the handwriting, the code is not beautiful:
1.bool variable p is useless.
2.L and R if defined as the time type, and t[] to be consistent is better to see
--------------------------------------------------------------------------------------------------------------- ---------------------------------
Code
1 /*2 id:icedrea13 Prob:milk24 lang:c++5 */6 7#include <iostream>8#include <fstream>9 using namespacestd;Ten One intN; A structTime {intB,g; } t[1+ the]; - - voidQsort (Time t[],intLintR) the { - if(L>=R)return; - - inti=l,j=r,x=t[(l+r) >>1].b; + while(true) - { + while(t[i].b<x) + +i; A while(t[j].b>x)--J; at if(I>J) Break; -Time T=t[i]; T[I]=T[J]; t[j]=T; -++i; --J; - } - qsort (T,L,J); qsort (t,i,r); - } in - voidChangeint&a,intb) to { + if(b>a) a=b; - } the * intMain () $ {Panax NotoginsengIfstreaminch("milk2.in"); -Ofstream out("Milk2.out"); the + inch>>N; A for(intI=1; i<=n;++i)inch>>T[i].b>>t[i].g; the +Qsort (T,1, N); - $ BOOLP//ture milking, F idle $ intL,r;//Current time period - intsum[2]={0,0};//Maximum--0 idle time, 1 milking time - thel=t[1].b; r=t[1].g; - for(intI=1; i<=n;++i)Wuyi { the if(r<t[i+1].B)//end of milking time - { WuChange (sum[0],t[i+1].b-R); -Change (sum[1],r-L); Aboutl=t[i+1].b; r=t[i+1].g; $ } - Else //Update Milking Time - { -Change (r,t[i+1].g); A } + } theChange (sum[1],r-L); - $ out<<sum[1]<<" "<<sum[0]<<Endl; the the inch. Close (); the out. Close (); the return 0; -}
Usaco Section1.2 Milking cows problem solving report