Milking Cows
分析:我们需要求出以该点开始可以到达的最大值以及该区域最大值和下一个开始地方的差值,然后不断求二者最大即可
1 /* 2 ID:wanghan 3 PROG:milk2 4 LANG:C++ 5 */ 6 #include "iostream" 7 #include "cstdio" 8 #include "cstring" 9 #include "string" 10 #include "algorithm" 11 using namespace std; 12 const int maxn=5050; 13 int n; 14 struct Node{ 15 int x,y; 16 }; 17 Node p[maxn]; 18 bool cmp(Node a,Node b){ 19 return a.x<b.x; 20 } 21 int main() 22 { 23 freopen("milk2.in", "r", stdin); 24 freopen("milk2.out", "w", stdout); 25 cin>>n; 26 for(int i=0;i<n;i++) 27 cin>>p[i].x>>p[i].y; 28 sort(p,p+n,cmp); 29 int ans=0; 30 for(int i=0;i<n;i++){ 31 int left=p[i].x,right=p[i].y; 32 for(int j=i;j<n;j++){ 33 if(p[j].x>right){ 34 break; 35 }else{ 36 right=max(p[j].y,right); 37 } 38 } 39 //cout<<"print: "<<left<<" "<<right<<endl; 40 ans=max(ans,(right-left)); 41 } 42 43 int cnt=0; 44 int r=p[0].y; 45 for(int i=0;i<n;i++){ 46 if(p[i].x>r){ 47 cnt=max((p[i].x-r),cnt); 48 } 49 r=max(r,p[i].y); 50 } 51 cout<<ans<<" "<<cnt<<endl; 52 }