-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBaekjoon1874.java
48 lines (39 loc) · 1.26 KB
/
Baekjoon1874.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
package Algorithms;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Stack;
/**
* https://www.acmicpc.net/problem/1874
* 백준 1874 스택 수열
*/
public class Baekjoon1874 {
private static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
private static Stack<Integer> stack = new Stack<>();
public static void main(String[] args) throws IOException {
int n = Integer.parseInt(br.readLine());
int[] inputs = new int[n];
int[] outputs = new int[n];
int j = 0;
for (int i = 0; i < n; i++) {
inputs[i] = Integer.parseInt(br.readLine());
}
Arrays.fill(outputs, -1);
StringBuilder sb = new StringBuilder();
int i = 1;
while (outputs[n - 1] == -1) {
if(stack.size()>n){
System.out.println("NO");
return;
}
stack.push(i++);
sb.append("+").append("\n");
while (!stack.isEmpty() && stack.peek() == inputs[j]) {
outputs[j++] = stack.pop();
sb.append("-").append("\n");
}
}
System.out.println(sb.toString());
}
}