- 풀지 못한 문제이다. 코딩테스트에서 나올 수 있는 최고난이도 수준의 스택 문제
- sol1: 이전 블록의 높이보다 작거나 같은 높이의 블록을 만날 때마다, 최대 넓이 계산
- 마지막에 스택에 남은 블록에 대해 넓이 계산
- 계산한 넓이 중 최 대값만을 기억해 출력
#include <bits/stdc++.h>
using namespace std;
#define H first
#define X second
int main(void)
{
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
while (true) {
int n;
cin >> n;
if (n == 0) break;
stack<pair<long long, long long>> S;
long long ans = 0;
for (int i = 0; i < n; i++) {
int h;
cin >> h;
int idx = i;
while (!S.empty() && S.top().H >= h) {
ans = max(ans, (i - S.top().X) * S.top().H);
idx = S.top().X;
S.pop();
}
S.push({h, idx});
}
while (!S.empty()) {
ans = max(ans, (n - S.top().X) * S.top().H);
S.pop();
}
cout << ans << '\n';
}
}