Advertisement
tepyotin2

KM-Sleepy Cow Herding

Oct 17th, 2023 (edited)
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.54 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. int main(){
  6.     ios_base::sync_with_stdio(0), cin.tie(0);
  7.     freopen("herding.in", "r", stdin);
  8.     //freopen("herding_silver_feb19/2.in", "r", stdin);
  9.     freopen("herding.out", "w", stdout);
  10.     int n;
  11.     cin >> n;
  12.    
  13.    
  14.     int pos[n];
  15.     for(int i=0; i<n; i++){
  16.         cin >> pos[i];
  17.     }
  18.     sort(pos, pos+n);
  19.     int minsize = 0;
  20.    
  21.     //cout << "n => " << n << endl;
  22.     //for (int i = 0; i < n; i++)
  23.     //{
  24.         //cout << "i: " << pos[i] << endl;
  25.     //}
  26.    
  27.     if(pos[n-1]- pos[1] == n-2 && pos[1] - pos[0]  > 2){
  28.         //exception will apply only gap > 2  since 2 eg.12346 will cause only one move
  29.         minsize = 2;
  30.     }else if(pos[n-2]-pos[0] == n-2 && pos[n-1] - pos[n-2] > 2){
  31.         //exception will apply only gap > 2  since 2 eg.12346 will cause only one move
  32.         minsize = 2;
  33.     }else{
  34.        
  35.        
  36.         int j = 1, best = 0;
  37.         for (int i = 0; i < n ; i++)
  38.         {
  39.             while (j < n  )
  40.             {
  41.                 int cover = j- i + 1;
  42.                 int gap = (pos[j] - pos[i]  + 1) - cover;
  43.                 int remain = n - cover ;
  44.                 //cout << "i: " << i << ", j: " << j << ", val[i]: " << pos[i] << ", val[j]: " << pos[j] << ", gap: " << gap << " , remain. " << remain << endl;
  45.                 if (gap > remain)
  46.                 {
  47.                     break;
  48.                 }
  49.                 best = max(best, cover);
  50.                 //cout << "To best: " << j - i + 1 << " => " << best << endl;
  51.                 j++;
  52.             }
  53.         }
  54.         minsize = n - best;
  55.     }
  56.      cout << minsize << '\n';
  57.      int maxval = max(pos[n-1]-pos[1], pos[n-2]-pos[0])   - (n - 2);
  58.      cout << maxval << '\n';
  59.      
  60.      
  61.     return 0;
  62. }
  63.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement