ann8497

AggCow samsung/spoj

Aug 18th, 2019
898
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.54 KB | None | 0 0
  1. /*
  2. Farmer John has built a new long barn, with N (2 <= N <= 100,000) stalls. The stalls are located along a straight line at positions x1,...,xN (0 <= xi <= 1,000,000,000).
  3.  
  4. His C (2 <= C <= N) cows don't like this barn layout and become aggressive towards each other once put into a stall. To prevent the cows from hurting each other, FJ wants to assign the cows to the stalls, such that the minimum distance between any two of them is as large as possible. What is the largest minimum distance?
  5. Input
  6. t – the number of test cases, then t test cases follows.
  7. * Line 1: Two space-separated integers: N and C
  8. * Lines 2..N+1: Line i+1 contains an integer stall location, xi
  9. Output
  10. For each test case output one integer: the largest minimum distance.
  11. */
  12.  
  13. #include <bits/stdc++.h>
  14. using namespace std;
  15. int ans;
  16. void aggcow(int n , int c, int a[], int l , int h  ){
  17.     if(h>=l){
  18.           int mid = (h+l)/2;
  19.          // cout<<l<<" "<<mid<<" "<<h<<endl;
  20.         int k=0;
  21.         int cnt =1;
  22.     for(int i=1;i<n;i++){
  23.      if(a[i]-a[k]>=mid){
  24.         cnt++;
  25.         k=i;
  26.      }
  27.     }
  28.    // cout<<cnt<<endl;
  29.      if(cnt>=c){
  30.         ans=mid;
  31.        aggcow(n,c,a,mid+1,h);
  32.              
  33.     }
  34.     else {
  35.           //cout<<mid<<"is to high"<<endl;
  36.         aggcow(n,c,a,l,mid-1);
  37.      
  38.         }
  39.  
  40.     }
  41.     else return;
  42. }
  43.  
  44. int main() {
  45.     int t; cin>>t;
  46.     while(t--){
  47.         int n,c; cin>>n>>c;
  48.     int a[n];
  49.     for(int i=0;i<n;i++)cin>>a[i];
  50.     sort(a,a+n);
  51.     aggcow(n,c,a, 0,10e9 );
  52.     cout<<ans<<endl;
  53.     }
  54.    
  55. }
Add Comment
Please, Sign In to add comment