usaco:Milking Cows

额 要细心

/*
ID: Jang Lawrence
PROG: milk2
LANG: C++
*/
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
int Max(int x,int y){if(x>y) return x; return y;}
int n;
struct ps
{
    int l,r;
}a[5555];
bool cmp(ps p,ps q){return p.l<q.l||(p.l==q.l&&p.r<q.r);}
int main()
{
    freopen("milk2.in","r",stdin);
  freopen("milk2.out","w",stdout);
   scanf("%d",&n);
for(int i=1;i<=n;++i)
scanf("%d%d",&a[i].l,&a[i].r);
sort(a+1,a+1+n,cmp);
int ans1=0,ans2=0;
int now=a[1].l,end=a[1].r;
  for(int i=2;i<=n;++i)
  {
      if(a[i].l<=end)
      {
          if(a[i].r<=end) continue;
          else  end=a[i].r;
      }
      else
      ans1=Max(ans1,end-now),ans2=Max(ans2,a[i].l-end),now=a[i].l,end=a[i].r;
  }
  ans1=Max(ans1,end-now);
  printf("%d %d\n",ans1,ans2);
    return 0;
}


你可能感兴趣的:(usaco:Milking Cows)