Advertisement
tepyotin2

KM-Sleepy Cow Herding

Oct 17th, 2023
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.15 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.out", "w", stdout);
  9.     int n;
  10.     cin >> n;
  11.     // cout << n << '\n';
  12.     int pos[n];
  13.     for(int i=0; i<n; i++){
  14.         cin >> pos[i];
  15.     }
  16.     sort(pos, pos+n);
  17.     int minsize = 0;
  18.    
  19.     if(pos[n-1]- pos[1] == n-2 && pos[1] - pos[0]  > 2){
  20.         minsize = 2;
  21.     }else if(pos[n-2]-pos[0] == n-2 && pos[n-1] - pos[n-2] > 2){
  22.         minsize = 2;
  23.     }else{
  24.        
  25.        
  26.         int j = 0, best = 0;
  27.         for (int i = 0; i < n ; i++)
  28.         {
  29.             while (j < n -1 )
  30.             {
  31.                 //cout << "i: " << i << ", j: " << j << ", val[i]: " << pos[i] << ", val[j+1]: " << pos[j+1] << ", diff: " << pos[j+1] - pos[i]  << " vs. " << n-1 << endl;
  32.                 if (pos[j+1] - pos[i] <= n-1)
  33.                 {
  34.                     j++;
  35.                 }else
  36.                 {
  37.                     break;
  38.                 }
  39.                 best = max(best,  j - i + 1);
  40.                 //cout << "To best: " << j - i + 1 << " => " << best << endl;
  41.             }
  42.         }
  43.         minsize = n - best;
  44.     }
  45.      cout << minsize << '\n';
  46.      int maxval = max(pos[n-1]-pos[1], pos[n-2]-pos[0])   - (n - 2);
  47.      cout << maxval << '\n';
  48. }
  49.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement