-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNo_1874.java
More file actions
53 lines (47 loc) · 1.64 KB
/
No_1874.java
File metadata and controls
53 lines (47 loc) · 1.64 KB
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
/*
Problem_1874_스택 수열
https://www.acmicpc.net/problem/1874
자료구조 : Stack
*/
import java.io.*;
import java.util.Stack;
public class No_1874 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
StringBuilder sb = new StringBuilder();
Stack<Integer> stack = new Stack<>();
int to = Integer.parseInt(br.readLine());
int max = 1;
for (int from = 0; from < to; from++) {
// 스택의 원소를 입력받고
int csn = Integer.parseInt(br.readLine());
/*
스택의 최대값보다 입력받은 원소가 큰 경우에
최대값부터 입력받은 원소까지 스택에 푸시해준다.
*/
if (csn >= max) {
for (int i = max; i <= csn; i++) {
stack.push(i);
sb.append("+\n");
}
// 최대값을 입력값 + 1을 대입한다.
max = csn + 1;
}
// 입력받은 원소의 값과 스택의 탑이 같다면 팝한다.
if (csn == stack.peek()) {
stack.pop();
sb.append("-\n");
}
// 만약 같지 않다면 모순이므로 종료한다.
else if (csn != stack.peek()) {
System.out.print("NO\n");
return;
}
}
bw.write(sb.toString());
bw.flush();
bw.close();
br.close();
}
}