Problem in aggressive cows

Revision en1, by AriaB, 2019-05-27 21:10:31

Hi, This is my code for this problem==>

https://www.spoj.com/problems/AGGRCOW/

code==>

include<bits/stdc++.h>

using namespace std;

bool Comperator(int x,int a[],int n,int k){// We want to know if we can get at least x distance with k cows int currentCows=1;int leftmost=0; for(int i=1;i<n;++i){ if(a[i]-a[leftmost]>=x){ leftmost=i;++currentCows; if(currentCows==k)return 1; } }return 0; }

int main() { int t;cin >> t; for(int i=0;i<t;++i){ int n,k;cin >> n >> k; int a[100000]; for(int j=0;j<n;++j){ cin >> a[i]; }sort(a,a+n); int l=0,r=a[n-1]-a[0]+1;// If we can do with x distance then obviosult we can do it with <=x. // So We need to update it //True True True True True True True . .. . . False False False ==> We want to find the last true while(r-l>1){ int m=(l+r)/2; if(Comperator(m,a,n,k)==true){ l=m; // l is true now }else r=m; // R is false now }cout << l; } }

I don't know why this doesn't return the correct answer please help!

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en2 English AriaB 2019-05-27 21:12:12 1019
en1 English AriaB 2019-05-27 21:10:31 1213 Initial revision (published)