Guest User

Untitled

a guest
Nov 26th, 2019
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.53 KB | None | 0 0
  1. #include<iostream>
  2. #include<algorithm>
  3. #include<vector>
  4. #define ll long long
  5. using namespace std;
  6.  
  7. bool place(vector<ll> a, ll start, ll end, ll mid, ll c){
  8.    
  9.     if(c == 1)
  10.         return true;
  11.     if(c > (end - start + 1))
  12.     {
  13.         return false;
  14.     }
  15.     ll cow = 1;//assume one cow is placed at position 0;
  16.     ll i = 0;
  17.     while(cow <= c){
  18.         //   cout<<start<< " "<<end<<endl;
  19.         // cout<<start;
  20.          cout<<" "<<start<< "*"<<i<<endl;
  21.         for( i = start + 1; i <= end; i++ ){
  22.             if((a[i] - a[start]) >= mid){
  23.                 cow++;
  24.                
  25.                 //another cow positioned at index i;
  26.                  start = i;
  27.                 cout<<" "<<start<< "*"<<i<<endl;
  28.                 // cout<<a[i]<<" "<<a[start]<<" "<<i<<endl;
  29.                 break;
  30.             }
  31.         }
  32.      
  33.         if(i >= end && cow <c)
  34.             return false;
  35.         else
  36.             return true;
  37.    
  38.     }
  39.    
  40.    
  41.     return true;
  42. }
  43.  
  44. int main() {
  45.    ll n, c;
  46.    cin>>n>>c;
  47.    vector<ll> a(n);
  48.    
  49.    for(ll i = 0; i < n; i++){
  50.        cin>>a[i];
  51.        
  52.    }
  53.    
  54.    ll start = 0, end = n - 1, ans = 0;
  55.     sort(a.begin(), a.end());
  56. //      for(ll i = 0; i < n; i++){
  57. //       cout<<a[i]<<" "<<"*"<<endl;
  58. //   }
  59.     while(start <= end){
  60.         ll mid = (start + end)/2;
  61.         if(place (a, 0, n -1, mid, c)){
  62.             ans = mid;
  63.             start = mid + 1;
  64.         }
  65.         else{
  66.             end = mid - 1;
  67.         }
  68.     }
  69.   //  cout<<ans<<endl;
  70.     return 0;
  71. }
Add Comment
Please, Sign In to add comment