这么水的代码也能过,USACO的数据弱爆了
/* ID: aznfy1 PROG: milk2 LANG: C++ */ #include<stdio.h> #include<string.h> #include<iostream> #include<algorithm> using namespace std; struct node { int start; int over; }farmer[6000]; bool compare(const node &a,const node &b) { if(a.start==b.start) return a.over>b.over; else return a.start<b.start; } int main() { freopen("milk2.in","r",stdin); freopen("milk2.out","w",stdout); int n; int nowstart; int nowover; int ans1; int ans2; while(cin>>n) { ans1=0; ans2=0; for(int i=0;i<n;i++) cin>>farmer[i].start>>farmer[i].over; sort(farmer,farmer+n,compare); //计算最长的时间,至少有一个人在工作 for(int i=0;i<n;i++) { nowstart=farmer[i].start; nowover=farmer[i].over; int tmp=i; do { if(nowover<=farmer[tmp].over&&nowover>=farmer[tmp].start) { nowover=farmer[tmp].over; tmp++; } else tmp++; } while(farmer[tmp].start<=nowover&&tmp<n); if(nowover-nowstart>ans1) ans1=nowover-nowstart; } //以下计算最长的时间没有一个人在工作 int tmp=0; do { nowstart=farmer[tmp].start; nowover=farmer[tmp].over; do { if(nowover<=farmer[tmp].over&&nowover>=farmer[tmp].start) { nowover=farmer[tmp].over; tmp++; } else tmp++; } while(farmer[tmp].start<=nowover&&tmp<n); if(farmer[tmp].start-nowover>ans2) ans2=farmer[tmp].start-nowover; } while(tmp<n); cout<<ans1<<" "<<ans2<<endl; } return 0; }