Advertisement
erek1e

IOI '05 P2 - Mean Sequence

Jan 23rd, 2023
899
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.73 KB | None | 0 0
  1. #include <iostream>
  2. #include <algorithm>
  3.  
  4. using namespace std;
  5.  
  6. const long long INF = 1e18;
  7.  
  8. int main() {
  9.     ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  10.     int n; cin >> n;
  11.     // no rounding will be needed since all m[i] are even
  12.     long long minX = -INF, maxX = INF;
  13.     long long a = 0; // a +/- x, where x is the first element
  14.     for (int i = 0; i < n; ++i) { // s[i+1]
  15.         long long m; cin >> m; m = 2*m; // convert from mean to sum
  16.         long long b = m - a;
  17.         if (i & 1) minX = max(minX, (a-b)/2); // b + x >= a - x, so x >= (a-b)/2
  18.         else maxX = min(maxX, (b-a)/2); // b - x >= a + x, so x <= (b-a)/2
  19.         a = b;
  20.     }
  21.     cout << max(maxX-minX+1, 0LL) << endl;
  22.     return 0;
  23. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement