Alex_tz307

USACO 2016 January Contest, Gold Problem 1. Angry Cows

Dec 6th, 2020 (edited)
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.23 KB | None | 0 0
  1. // http://www.usaco.org/index.php?page=viewproblem2&cpid=597
  2. #include <bits/stdc++.h>
  3. #define int long long
  4. #define ABS(x) ((x) >= 0 ? (x) : -(x))
  5.  
  6. using namespace std;
  7. using ld = long double;
  8.  
  9. ifstream fin("angry.in");
  10. ofstream fout("angry.out");
  11.  
  12. const int INF = 1e16L;
  13.  
  14. void min_self(ld &a, ld b) {
  15.     a = min(a, b);
  16. }
  17.  
  18. int32_t main() {
  19.     int N;
  20.     fin >> N;
  21.     vector<int> a(N);
  22.     for(int i = 0; i < N; ++i)
  23.         fin >> a[i];
  24.     sort(a.begin(), a.end());
  25.     vector<int> dp[2];
  26.     for(int it = 0; it < 2; ++it) {
  27.         dp[it].resize(N, INF);
  28.         dp[it][0] = -1;
  29.         int lastj = 0;
  30.         for(int i = 1; i < N; ++i) {
  31.             while(lastj + 1 < i && ABS(a[i] - a[lastj + 1]) > dp[it][lastj + 1] + 1)
  32.                 ++lastj;
  33.             dp[it][i] = min(ABS(a[i] - a[lastj]), dp[it][lastj + 1] + 1);
  34.         }
  35.         reverse(a.begin(), a.end());
  36.     }
  37.     reverse(dp[1].begin(), dp[1].end());
  38.     int i = 0, j = N - 1;
  39.     ld ans = INF;
  40.     while(i < j) {
  41.         min_self(ans, max((ld)(a[j] - a[i]) / 2.0, (ld)1.0 + max(dp[0][i], dp[1][j])));
  42.         if(dp[0][i + 1] < dp[1][j - 1])
  43.             ++i;
  44.         else
  45.             --j;
  46.     }
  47.     fout << fixed << setprecision(1) << ans;
  48. }
  49.  
  50.  
Advertisement
Add Comment
Please, Sign In to add comment