백준 알고리즘(C++)
백준 3015번 오아시스 재결합 ( C++ )
coding232624
2024. 9. 2. 00:05
문제
https://www.acmicpc.net/problem/3015
해설
키의 범위가 넓어 longlong을 사용해야 하는 문제
조금 까다롭지만 길이 제한이 생긴 괄호 문제로 생각하고 풀면 풀 수 있음
코드
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
|
#include <iostream>
#include <algorithm>
#include <stack>
using namespace std;
int n, cnt;
stack<pair<long long, int>> s;
long long hight;
long long ret;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n;
for (int i = 0; i < n; i++)
{
cin >> hight;
cnt = 1;
while (true)
{
if (s.empty())
{
s.push({hight, cnt});
break;
}
else if (s.top().first < hight)
{
ret += s.top().second;
s.pop();
}
else if (s.top().first == hight)
{
ret += s.top().second;
cnt += s.top().second;
s.pop();
}
else
{
ret++;
s.push({hight, cnt});
break;
}
}
}
cout << ret;
return 0;
}
|
cs |