티스토리 뷰

https://www.acmicpc.net/problem/6549

 

6549번: 히스토그램에서 가장 큰 직사각형

문제 히스토그램은 직사각형 여러 개가 아래쪽으로 정렬되어 있는 도형이다. 각 직사각형은 같은 너비를 가지고 있지만, 높이는 서로 다를 수도 있다. 예를 들어, 왼쪽 그림은 높이가 2, 1, 4, 5, 1,

www.acmicpc.net

백준 소스코드 [C++] 6549 히스토그램에서 가장 큰 직사각형

#include <iostream>
#include <utility>
#include <vector>
#include <algorithm>
#include <stack>
#include <cmath>
using namespace std;
int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	int n=1;
	long long int M, top, width;
	int arr[100001];
	stack <int> s;
	while (n != 0) {
		cin >> n;
		if (n == 0) break;
		M = 0;

		for (int i = 0;i < n;i++) cin >> arr[i];
		for (int i = 0;i < n;i++) {
			while (s.empty() != true && arr[s.top()] > arr[i])
			{
				top = arr[s.top()];
				s.pop();
				if (s.empty() != true)width = i - s.top() - 1;
				else width = i;
				M = max(M, top * width);

			}
			s.push(i);
		}
		while (!s.empty())
		{
			top = arr[s.top()];
			s.pop();
			if (s.empty() != true)width = n - s.top() - 1;
			else width = n;
			M = max(M, top * width);

		}
		cout << M<<'\n';
		
		
	}
	
	return 0;
}
댓글