Skip to content

Commit 95febce

Browse files
committed
boj 25556 포스택
1 parent 3787d43 commit 95febce

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed

stack/25556.cpp

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#include <iostream>
2+
#include <algorithm>
3+
#include <stack>
4+
#define MAX 100001
5+
using namespace std;
6+
7+
stack<int> s[4];
8+
int list[MAX];
9+
int N;
10+
11+
void func() {
12+
s[0].push(0);
13+
s[1].push(0);
14+
s[2].push(0);
15+
s[3].push(0);
16+
for (int i = 1; i <= N; i++) {
17+
int minIdx = -1;
18+
int minDiff = 1e9;
19+
for (int j = 0; j < 4; j++) {
20+
if (s[j].empty()) {
21+
minIdx = j;
22+
break;
23+
}
24+
25+
if (s[j].top() > list[i]) continue;
26+
27+
int diff = abs(s[j].top() - list[i]);
28+
if (diff < minDiff) {
29+
minDiff = diff;
30+
minIdx = j;
31+
}
32+
}
33+
34+
if (minIdx == -1) {
35+
cout << "NO\n";
36+
return;
37+
}
38+
39+
s[minIdx].push(list[i]);
40+
}
41+
42+
cout << "YES\n";
43+
}
44+
45+
void input() {
46+
cin >> N;
47+
for (int i = 1; i <= N; i++) {
48+
cin >> list[i];
49+
}
50+
}
51+
52+
int main() {
53+
cin.tie(NULL); cout.tie(NULL);
54+
ios::sync_with_stdio(false);
55+
56+
input();
57+
func();
58+
59+
return 0;
60+
}

0 commit comments

Comments
 (0)