Skip to content

Instantly share code, notes, and snippets.

@KhaledElshamy
Created November 24, 2016 20:04
Show Gist options
  • Select an option

  • Save KhaledElshamy/035ba0c28ab3906b8128bf111a5c70ff to your computer and use it in GitHub Desktop.

Select an option

Save KhaledElshamy/035ba0c28ab3906b8128bf111a5c70ff to your computer and use it in GitHub Desktop.
Milking Cows
#include <bits/stdc++.h>
using namespace std;
int main()
{
int num,start,end,max=0,gap=0;
cin>>num;
pair<int,int>x[5000];
for(int i=0;i<num;i++)
cin>>x[i].first>>x[i].second;
sort(x,x+num);
start=x[0].first,end=x[0].second;
for(int i=1;i<num;i++)
{
if(x[i].first<end)
end=(x[i].second<end?end:x[i].second);
else
{
max=(end-start>max)?end-start:max;
gap=(x[i].first-end>gap)?x[i].first-end:gap;
start=x[i].first,end=x[i].second;
}
max=(end-start>max)?end-start:max;
}
cout<<max<<' '<<gap<<endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment