Advertisement
mfgnik

Untitled

Jul 12th, 2020
1,074
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.19 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3.  
  4.  
  5. int main() {
  6.     int numbers_amount;
  7.     std::cin >> numbers_amount;
  8.     std::vector<int> histogram(numbers_amount + 2, -1);
  9.     for (int index = 1; index <= numbers_amount; ++index) {
  10.         std::cin >> histogram[index];
  11.     }
  12.     std::vector<int> right(numbers_amount + 2);
  13.     std::vector<int> stack(1, 0);
  14.     for (int index = 1; index <= numbers_amount + 1; ++index) {
  15.         while (histogram[index] < histogram[stack.back()]) {
  16.             right[stack.back()] = index;
  17.             stack.pop_back();
  18.         }
  19.         stack.push_back(index);
  20.     }
  21.     int64_t max_square = 0;
  22.     std::vector<int> left(numbers_amount + 2, 0);
  23.     for (int index = numbers_amount; index >= 0; --index) {
  24.         while (histogram[index] < histogram[stack.back()]) {
  25.             int a = stack.back();
  26.             stack.pop_back();
  27.             left[a] = index;
  28.             int64_t current_square = static_cast<int64_t>(histogram[a]) * (right[a] - left[a] - 1);
  29.             if (current_square > max_square) {
  30.                 max_square = current_square;
  31.             }
  32.         }
  33.         stack.push_back(index);
  34.     }
  35.     std::cout << max_square;
  36.     return 0;
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement