| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | ||||
| 4 | 5 | 6 | 7 | 8 | 9 | 10 |
| 11 | 12 | 13 | 14 | 15 | 16 | 17 |
| 18 | 19 | 20 | 21 | 22 | 23 | 24 |
| 25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- cyphenengine
- JavaScript
- 이득우의 게임수학
- hanbit.co.kr
- 이득우
- booksr.co.kr
- 입출력과 사칙연산
- https://insightbook.co.kr/
- 게임 수학
- C++
- 메타버스
- C
- C#
- 일기
- 잡생각 정리글
- (주)책만
- Noam Nisan
- 주우석
- HANBIT Academy
- 데이터 통신과 컴퓨터 네트워크
- 김진홍 옮김
- 백준
- 전공자를 위한 C언어 프로그래밍
- unity6
- BOJ
- 박기현
- The Elements of Computing Systems 2/E
- 밑바닥부터 만드는 컴퓨팅 시스템 2판
- 생능출판
- 알고리즘
Archives
- Today
- Total
cyphen156
백준-분할 정복 6549 히스토그램에서 가장 큰 직사각형 본문
[백준] 6549번 : 히스토그램에서 가장 큰 직사각형 - JAVA [자바]
[백준] 6549번 : 히스토그램에서 가장 큰 직사각형 - JAVA [자바]
https://www.acmicpc.net/problem/6549 6549번: 히스토그램에서 가장 큰 직사각형 입력은 테스트 케이스 여러 개로 이루어져 있다. 각 테스트 케이스는 한 줄로 이루어져 있고, 직사각형의 수 n이 가장 처음
st-lab.tistory.com
참고하자
히스토그램이 주어졌을 때
가장 큰 넓이를 갖는 직사각형을 만들어라.

1차 알고리즘 -> 브루트 포싱
2차 알고리즘 -> 분할 정복
제약사항
- 1 <= n < 100,001
- 0 <= hi < 1,000,000,001
- If Input == 0 then exit Input
주의 사항
없다.
CPP풀이
히스토그램에서 가장 큰 직사각형_6549.cpp
/**
* 백준 히스토그램에서 가장 큰 직사각형_6549
* 히스토그램이 주어졌을 때
* 가장 큰 넓이를 갖는 직사각형을 만들어라.
*
* 제한사항
*****************************************
* 1 <= n < 100,001 *
* 0 <= hi < 1,000,000,001 *
* If Input == 0 then exit Input *
*****************************************
*
*
*
* 주의
* 없다.
*
* 풀이시간 (문제 해석 + 구현)
* 1 + 90분
*/
#include <iostream>
#include <vector>
#include <stack>
static const int MAX_SIZE = 100001;
using namespace std;
static int inputs[MAX_SIZE] = { 0 };
static int N;
static long long maxArea = 0;
void Calculate();
int main(void)
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
while (true)
{
cin >> N;
if (N == 0)
{
break;
}
maxArea = 0;
for (int i = 0; i < N; ++i)
{
cin >> inputs[i];
}
Calculate();
cout << maxArea << '\n';
}
return 0;
}
void Calculate()
{
// 1차 알고리즘 == 브루트 포싱
// start with 0
// for (int i = 0; i < N; ++i)
// {
// long long minHeight = inputs[i];
// for (int j = i; j < N; ++j)
// {
// if (inputs[j] < minHeight)
// {
// minHeight = inputs[j];
// }
// long long width = (long long)(j - i + 1);
// long long area = minHeight * width;
// if (area > maxArea)
// {
// maxArea = area;
// }
// }
// }
// 2차 알고리즘 단조 증가 방식
stack<int> indexStack;
for (int currentIndex = 0; currentIndex < N; ++currentIndex)
{
while (!indexStack.empty() && inputs[indexStack.top()] > inputs[currentIndex])
{
int poppedIndex = indexStack.top();
indexStack.pop();
long long rectangleHeight = (long long)inputs[poppedIndex];
int leftBoundary;
if (indexStack.empty())
{
leftBoundary = 0;
}
else
{
leftBoundary = indexStack.top() + 1;
}
int rectangleWidth = currentIndex - leftBoundary; // right = currentIndex - 1
long long rectangleArea = rectangleHeight * (long long)rectangleWidth;
if (rectangleArea > maxArea)
{
maxArea = rectangleArea;
}
}
indexStack.push(currentIndex);
}
while (!indexStack.empty())
{
int poppedIndex = indexStack.top();
indexStack.pop();
long long rectangleHeight = (long long)inputs[poppedIndex];
int leftBoundary;
if (indexStack.empty())
{
leftBoundary = 0;
}
else
{
leftBoundary = indexStack.top() + 1;
}
int rectangleWidth = N - leftBoundary; // right = N - 1
long long rectangleArea = rectangleHeight * (long long)rectangleWidth;
if (rectangleArea > maxArea)
{
maxArea = rectangleArea;
}
}
}
모든 예제 코드의 소스파일은 제 개인 깃허브 레포지토리에 있습니다.
Workspace/알고리듬 풀이 at main · cyphen156/Workspace
Studying . Contribute to cyphen156/Workspace development by creating an account on GitHub.
github.com
'컴퓨터공학 > 알고리듬 풀이' 카테고리의 다른 글
| 백준-이분 탐색 10816 숫자 카드 2 (1) | 2025.08.22 |
|---|---|
| 백준-이분 탐색 1920 수 찾기 (0) | 2025.08.22 |
| 백준-분할 정복 11444 피보나치 수 6 (1) | 2025.08.19 |
| 백준-분할 정복 10830 행렬 제곱 (1) | 2025.08.18 |
| 백준-분할 정복 2740 행렬 곱셈 (1) | 2025.08.18 |
