kdzhr

Типичная ситуация

Feb 16th, 2020
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.09 KB | None | 0 0
  1. // OK BS http://contest.uni-smr.ac.ru/ru/problemset/6137/
  2.  
  3. # include <iostream>
  4. # include <vector>
  5. using namespace std;
  6.  
  7. bool check(vector<int32_t> &m, int32_t mid, int32_t n) {
  8.     vector<int32_t> cur(n, mid);
  9.     for (size_t i = 0; i < n; i++) {
  10.         int32_t m_i = m[i];
  11.         if (i > 0 && cur[i - 1] != 0) {
  12.             m_i -= min(cur[i - 1], m_i);
  13.         }
  14.         int32_t x = min(cur[i], m_i);
  15.         cur[i] -= x;
  16.         m_i -= x;
  17.         if (i < n - 1) {
  18.             x = min(cur[i + 1], m_i);
  19.             cur[i + 1] -= x;
  20.             m_i -= x;
  21.         }
  22.         if (m_i != 0) {
  23.             return false;
  24.         }
  25.     }
  26.     return true;
  27. }
  28.  
  29. int main() {
  30.     int32_t n;
  31.     cin >> n;
  32.     vector<int32_t> m(n);
  33.     for (size_t i = 0; i < n; i++) {
  34.         cin >> m[i];
  35.     }
  36.     int32_t left = 0;
  37.     int32_t right = 1000000;
  38.     while (right - left > 1) {
  39.         int32_t mid = (left + right) / 2;
  40.         if (check(m, mid, n)) {
  41.             right = mid;
  42.         }
  43.         else {
  44.             left = mid;
  45.         }
  46.     }
  47.     cout << right;
  48.     return 0;
  49. }
Add Comment
Please, Sign In to add comment