20033번 : Square, Not Rectangle
문제)
A histogram is a polygon made by aligning adjacent rectangles that share a common base line. Each rectangle is called a bar. The -th bar from the left has width 1 and height .
Your goal is to find the area of the largest rectangle contained in the given histogram, such that one of the sides is parallel to the base line.
![](https://blog.kakaocdn.net/dn/UvIVm/btsEndl2pYN/UvFp7E6CYayBlVVCdntNNK/img.png)
Actually, no, you have to find the largest square. Since the area of a square is determined by its side length, you are required to output the side length instead of the area.
![](https://blog.kakaocdn.net/dn/d1sx8N/btsEpp7i3Cp/K1C9fAN4or3b5sAHrXCjx1/img.png)
입력 :
On the first line, a single integer is given, where 1≤ N ≤300000
On the next line, space-separated integers 1,2,⋯, , are given. (1≤ H_i ≤10^9) is the height of the -th bar.
출력 :
Output the side length of the largest square in the histogram, such that one of the sides is parallel to the base line.
풀이)
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
// 20033.
#include <iostream>
#include <vector>
#include <limits.h>
using namespace std;
int n;
vector<long long> vec;
// x의 크기의 정사각형이 만들어질 수 있을까?
bool check(long long x)
{
int cnt = 0;
for (int i = 0; i < n; i++)
{
// x크기보다 작은 막대가 나온다면 초기화
if (vec[i] < x) cnt = 0;
else
{
cnt++;
if (cnt >= x) return true;
}
}
return false;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n;
vec.resize(n);
for (long long& ele : vec) cin >> ele;
long long s = 0;
long long e = INT_MAX;
long long ans = 0;
while (s <= e)
{
long long mid = (s + e) / 2;
// mid의 크기로 해당 크기의 정사각형이 만들어질 수 있는지
// 확인
if (check(mid))
{
ans = mid;
s = mid + 1;
}
else e = mid - 1;
}
cout << ans;
return 0;
}
|
cs |
출처 : https://www.acmicpc.net/problem/20033
20033번: Square, Not Rectangle
On the first line, a single integer $N$ is given, where $1 \le N \leq 300\,000$. On the next line, $N$ space-separated integers $H_1, H_2, \cdots, H_N$, are given. $H_i$ $(1 \le H_i \le 10^9)$ is the height of the $i$-th bar.
www.acmicpc.net
'Develop > 백준 (Cpp)' 카테고리의 다른 글
[백준] 2512번 : 예산 (C++) (0) | 2024.02.07 |
---|---|
[백준] 8983번 : 사냥꾼 (C++) (0) | 2024.02.05 |
[백준] 28449번 : 누가 이길까 (C++) (0) | 2024.02.04 |
[백준] 16498번 : 작은 벌점 (C++) (0) | 2024.02.03 |
[백준] 2121번 : 넷이 놀기 (C++) (0) | 2024.02.02 |