Alex_tz307

USACO 2019 February Contest, Silver Problem 1. Sleepy Cow Herding

Dec 5th, 2020
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.88 KB | None | 0 0
  1. // http://www.usaco.org/index.php?page=viewproblem2&cpid=918
  2. #include <bits/stdc++.h>
  3.  
  4. using namespace std;
  5.  
  6. ifstream fin("herding.in");
  7. ofstream fout("herding.out");
  8.  
  9. int N;
  10. vector<int> a;
  11.  
  12. void max_self(int &a, int b) {
  13.     a = max(a, b);
  14. }
  15.  
  16. int solve_min() {
  17.     if(a[N - 2] - a[0] == N - 2 && a[N - 1] - a[N - 2] > 2)
  18.         return 2;
  19.     if(a[N - 1] - a[1] == N - 2 && a[1] - a[0] > 2)
  20.         return 2;
  21.     int j = 0, best = 0;
  22.     for(int i = 0; i < N; ++i) {
  23.         while(j < N - 1 && a[j + 1] - a[i] < N)
  24.             ++j;
  25.         max_self(best, j - i + 1);
  26.     }
  27.     return N - best;
  28. }
  29.  
  30. int solve_max() {
  31.     return max(a[N - 2] - a[0], a[N - 1] - a[1]) - (N - 2);
  32. }
  33.  
  34. int main() {
  35.     fin >> N;
  36.     a.resize(N);
  37.     for(int i = 0; i < N; ++i)
  38.         fin >> a[i];
  39.     sort(a.begin(), a.end());
  40.     fout << solve_min() << '\n' << solve_max();
  41. }
  42.  
Advertisement
Add Comment
Please, Sign In to add comment