Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
PlabonKumarsaha authored May 24, 2019
1 parent e4c5ae3 commit 159d295
Showing 1 changed file with 67 additions and 0 deletions.
67 changes: 67 additions & 0 deletions aggresive cows SPOJ.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <vector>
using namespace std;
int v1[100002];
int p,q;
bool solution(int var)
{
int st = v1[0];
int cow =1;
for(int i =0;i<p;i++)
{
if(v1[i]-st>=var)
{
cow++;
if(cow == q)
return true;
st = v1[i];

}
}
return false;
}
int binarySearch()
{
int fi =0; //0 <= xi <= 1,000,000,000)
int la = v1[p -1];
// cout<<"p = "<<p<<endl;
// cout<<"la = "<<la<<endl;
while(fi<la)
{
int mid = (la+fi)/2;
// cout<<" mid in"<<mid <<endl;
if(solution(mid))
{
// cout<<" mid sol "<<mid<<endl;
fi = mid+1;
// cout<<"fi if ="<<fi<<endl;
}
else{
la = mid;
}
}
// cout<<"fi"<<fi<<endl;
return fi-1;
}
int main()
{
int test ;
cin>>test;
while(test--)
{
cin>>p>>q;
for(int j=0 ;j<p;j++)
cin>>v1[j];

sort(v1,v1+p);

printf("%d\n", binarySearch());



}

return 0;
}

0 comments on commit 159d295

Please sign in to comment.