Milking Cows
用一个数组标记各个时间的使用情况,在便利一遍即可,注意开始和结束的时间段。
代码如下:
/*
ID: xhzdcyy1
PROB: milk2
LANG: C++
*/
#include <iostream>
#include <fstream>
#include <string>
#define cin fin
#define cout fout
using namespace std;
const int N=1000000;
bool arr[N+1];
int main()
{
ofstream fout ("milk2.out");
ifstream fin ("milk2.in");
int n;
cin>>n;
for(int i=0;i<n;i++){
int a,b;
cin>>a>>b;
for(int k=a;k<b;k++){
arr[k]=true;
}
}
bool flag=arr[0];
int last=0;
int tmp=0;
int max1=0,max2=0;
for(int i=1;i<=N;i++){
if(arr[i]==arr[i-1]){
// tmp++;
continue;
}
else{
tmp=i-last;
if(arr[i-1]==true&&tmp>max1){
max1=tmp;
}
if(arr[i-1]==false&&tmp>max2&&flag){
max2=tmp;
}
flag=true;
last=arr[i];
last=i;
}
}
cout<<max1<<" "<<max2<<endl;
return 0;
}