Advertisement
Korotkodul

2_B_v2

Oct 25th, 2023 (edited)
556
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.20 KB | None | 0 0
  1. #include <cmath>
  2. #include <iostream>
  3. #include <stack>
  4. #include <vector>
  5.  
  6. using std::cin;
  7. using std::cout;
  8. using std::max;
  9. using std::min;
  10. using std::stack;
  11. using std::string;
  12. using std::vector;
  13. using Ll = long long;
  14. using Pll = std::pair<long long, long long>;
  15.  
  16. int main() {
  17.   int num;
  18.   cin >> num;
  19.   vector<Ll> arr(num);
  20.   for (Ll& el : arr) {
  21.     cin >> el;
  22.   }
  23.   vector<int> rt(num);
  24.   vector<int> lt(num);
  25.   rt[num - 1] = num;
  26.   lt[0] = -1;
  27.   stack<Pll> st;
  28.   st.push({arr[num - 1], num - 1});
  29.   for (int i = num - 2; i >= 0; --i) {
  30.     while (!st.empty() && arr[i] <= st.top().first) {
  31.       st.pop();
  32.     }
  33.     if (st.empty()) {
  34.       rt[i] = num;
  35.     } else {
  36.       rt[i] = st.top().second;
  37.     }
  38.     st.push({arr[i], i});
  39.   }
  40.  
  41.   while (!st.empty()) {
  42.     st.pop();
  43.   }
  44.   st.push({arr[0], 0});
  45.   for (int i = 1; i < num; ++i) {
  46.     while (!st.empty() && arr[i] <= st.top().first) {
  47.       st.pop();
  48.     }
  49.     if (!st.empty()) {
  50.       lt[i] = st.top().second;
  51.     } else {
  52.       lt[i] = -1;
  53.     }
  54.     st.push({arr[i], i});
  55.   }
  56.   Ll ans = -1;
  57.   for (int i = 0; i < num; ++i) {
  58.     Ll now = (rt[i] - lt[i] - 1) * arr[i];
  59.     ans = max(ans, now);
  60.   }
  61.   cout << ans;
  62. }
  63.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement