Advertisement
Korotkodul

D. Дождевая вода

Mar 29th, 2025 (edited)
354
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.94 KB | None | 0 0
  1. #include <iostream>
  2. #include <sstream>
  3. #include <exception>
  4. #include <cmath>
  5. #include <unordered_set>
  6. #include <vector>
  7. #include <unordered_map>
  8. #include <algorithm>
  9. using namespace std;
  10.  
  11. void cv(vector <int> v) {
  12.     for (int el: v) {
  13.         cout << el << ' ';
  14.     } cout << "\n";
  15. }
  16.  
  17. int n;
  18. vector <int> a;
  19.  
  20. int l(int R) {
  21.     cout << "R: " << R <<"\n";
  22.     if  (R == 0 || R == n - 1){
  23.         return 0;
  24.     }
  25.     auto max_it = max_element(a.begin(), a.begin() +  R - 1);
  26.     int mxi = R - (abs(distance(a.begin() + R, max_it)));
  27.     cout << "mxi: " << mxi << "\n";
  28.     cout << "dst: " << abs(distance(a.begin() + R, max_it)) << "\n";
  29.     int s = 0;
  30.     int h = a[mxi];
  31.     cout << "h: " << h << "\n";
  32.     for (int i = R - 1; i >= mxi; --i) {
  33.         if (h < a[i]) {
  34.             continue;
  35.         }
  36.         cout << "i: " << i << "\n";
  37.         s += h - a[i];
  38.     }
  39.     cout << "s: " << s << "\n";
  40.     cout << "\n";
  41.     int add = l(mxi);
  42.     return s + add;
  43. }
  44.  
  45. int r(int L) {
  46.     cout << "L: " << L << "\n";
  47.     if (L == n - 1 || L == 0) {
  48.         return 0;
  49.     }
  50.     auto max_it = max_element(a.begin() + L + 1, a.end());
  51.     int mxi = L + abs(distance(a.begin() + L, max_it));
  52.     cout << "mxi: " << mxi << "\n";
  53.     cout << "dst: " << abs(distance(a.begin() + L, max_it)) << "\n";
  54.     int s = 0;
  55.     int h = a[mxi];
  56.     cout << "h: " << h << "\n";
  57.     for (int i = L + 1; i <= mxi; ++i) {
  58.         if (h < a[i]) {
  59.             continue;
  60.         }
  61.         cout << "i: " << i << "\n";
  62.         s += h - a[i];
  63.     }
  64.     cout << "s: " << s << "\n";
  65.     cout << "\n";
  66.     int add = r(mxi);
  67.     return s + add;
  68. }
  69. /*
  70. 10
  71. 7 0 4 2 5 0 6 4 0 5
  72.  
  73. 16
  74. 1 2 4 0 0 5 4 2 6 8 2 3 4 5 5 3
  75. */
  76. int main() {
  77.      cin >> n;
  78.     a.resize(n);
  79.     for (int &el: a) cin >> el;
  80.     auto max_it = max_element(a.begin(), a.end());
  81.     int mxi = distance(a.begin(), max_it);
  82.     int s = l(mxi) + r(mxi);
  83.     cout << s << "\n";
  84. }
  85.  
  86.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement