728x90
1725번: 히스토그램
첫 행에는 N (1 ≤ N ≤ 100,000) 이 주어진다. N은 히스토그램의 가로 칸의 수이다. 다음 N 행에 걸쳐 각 칸의 높이가 왼쪽에서부터 차례대로 주어진다. 각 칸의 높이는 1,000,000,000보다 작거나 같은
www.acmicpc.net
- C++
#include <algorithm>
#include <iostream>
#include <stack>
using namespace std;
stack<int> st; // histogram's index
int arr[100002]; // histogram's height
int N, answer;
int main() {
cin.tie(0);
ios_base::sync_with_stdio(0);
cin >> N;
for (int i = 1; i <= N; i++) cin >> arr[i];
st.push(0);
for (int i = 1; i <= N + 1; i++) {
while (!st.empty() && arr[st.top()] > arr[i]) {
int height = arr[st.top()];
st.pop();
int width = i - st.top() - 1;
answer = max(answer, height * width);
}
st.push(i);
}
cout << answer;
return 0;
}
|
cs |
728x90
'PS' 카테고리의 다른 글
BOJ 2304 - 창고 다각형 (0) | 2020.10.26 |
---|---|
BOJ 14719 - 빗물 (0) | 2020.10.26 |
BOJ 1935 - 후위 표기식 2 (0) | 2020.10.25 |
BOJ 1238 - 파티 (0) | 2020.10.02 |
BOJ 1929 - 소수 구하기 (0) | 2020.09.30 |