Stack
https://www.acmicpc.net/problem/1874
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
Stack<Integer> stack = new Stack<>();
Queue<Integer> answerQueue = new LinkedList<>();
for (int i = 0; i < n; i++) {
answerQueue.offer(Integer.parseInt(br.readLine()));
}
StringBuilder sb = new StringBuilder();
for (int i = 1; i <= n; i++) {
while (!stack.isEmpty() && stack.peek().equals(answerQueue.peek())) {
stack.pop();
answerQueue.poll();
sb.append('-').append("\n");
}
if (!stack.isEmpty() && stack.peek() > answerQueue.peek()) {
System.out.println("NO");
return ;
}
stack.push(i);
sb.append('+').append("\n");
}
while (!stack.isEmpty()) {
if (!stack.pop().equals(answerQueue.poll())) {
System.out.println("NO");
return;
} else {
sb.append('-').append("\n");
}
}
System.out.println(sb);
}
}
30분