█████████ ████ ███░░░░░███ ░░███ ███ ░░░ ██████ ███████ ██████ ██████ ░███ ███░░███ ███░░███ ███░░███ ███░░███ ░███ ░███ ░███░███ ░███ ░███████ ░███ ░███ ░░███ ███░███ ░███░███ ░███ ░███░░░ ░███ ░███ ░░█████████ ░░██████ ░░████████░░██████ ░░██████ ░░░░░░░░░ ░░░░░░ ░░░░░░░░ ░░░░░░ ░░░░░░

Envío 7175

Problema 0x4f - Rectángulo de máxima área dentro de un histograma pequeño

  • Autor: danidiaztech
  • Fecha: 2023-10-07 04:13:16 UTC (Hace alrededor de 1 año)
Caso # Resultado Tiempo Memoria
#1
Correcto
0.002 s 0 KBi
#2
Correcto
0.001 s 0 KBi
#3
Correcto
0.001 s 0 KBi
#4
Correcto
0.002 s 0 KBi
#5
Correcto
0.001 s 0 KBi
#6
Correcto
0.002 s 0 KBi
#7
Correcto
0.002 s 0 KBi
#8
Correcto
0.001 s 0 KBi
#9
Correcto
0.002 s 0 KBi
#10
Correcto
0.002 s 0 KBi
#11
Correcto
0.001 s 0 KBi
#12
Correcto
0.002 s 0 KBi
#13
Correcto
0.001 s 0 KBi
#14
Correcto
0.001 s 0 KBi
#15
Correcto
0.002 s 0 KBi
#16
Correcto
0.001 s 0 KBi
#17
Correcto
0.001 s 0 KBi
#18
Correcto
0.001 s 0 KBi
#19
Correcto
0.002 s 0 KBi
Puntos totales: 100 / 100

Código

// Made by Daniel Diaz (@Danidiaztech)
#include <bits/stdc++.h>
using namespace std;

#define ll long long

const int mod = 1e9 + 7;
const string yes = "YES", no = "NO";

void solve(){
  int n;
  cin >> n;
  int a[n];
  for (int i =0; i < n; i++){
    cin >> a[i];
  }
  // simulate with stack
  // mono-increasing stack
  stack<pair<int,int>> s, s2;
  // prev element that is less than ith
  int prev[n], nxt[n];

  for (int i =0; i <n; i++){
    while (!s.empty() && s.top().first >= a[i]){
      s.pop();
    }
    prev[i] = (s.empty() ? i : ( -s.top().second + i - 1));
    s.push({a[i], i});
  }

  for (int i = n -1; i >= 0; i--){
    while (!s2.empty() && s2.top().first >= a[i]){
      s2.pop();
    }
    nxt[i] = (s2.empty() ? n - i - 1 : (s2.top().second - i - 1));
    s2.push({a[i], i});
  }

  int ans = 0;

  for (int i = 0; i < n; i++){
    int x = (nxt[i] + prev[i] + 1) * a[i];
    ans = max(ans, x);
  }
  cout << ans << '\n';
}

int main() {
  cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(0);

  #if LOCAL
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
  #endif

  int tc = 1;
  // cin >> tc;

  for (int t = 1; t <= tc; t++){
    solve();
  }
}