-
Notifications
You must be signed in to change notification settings - Fork 0
/
NO1874.java
59 lines (49 loc) · 1.16 KB
/
NO1874.java
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
package baekjoon.DataStructure;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
public class NO1874 {
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
Stack<Integer> stack = new Stack<>();
int a = Integer.parseInt(in.readLine());
List<Integer> b = new ArrayList<>();
StringBuilder str = new StringBuilder();
boolean flag = true;
for(int i=0; i<a; i++) {
b.add(Integer.parseInt(in.readLine()));
}
int index = 1;
int pivot = 0;
int curVal = b.get(pivot);
int top = 0;
while(true) {
if(stack.isEmpty()) {
stack.push(index++);
str.append("+\n");
continue;
}
else
top = stack.peek();
if(top < curVal) {
stack.push(index++);
str.append("+\n");
} else if (top == curVal) {
stack.pop();
str.append("-\n");
if(pivot+1 == a)
break;
curVal = b.get(++pivot);
} else {
System.out.println("NO");
flag = false;
break;
}
}
if(flag)
System.out.println(str);
}
}