본문 바로가기
알고리즘 & 자료구조/백준

백준 1874

by 신재권 2021. 8. 5.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Stack;

public class Main1874 {

	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		Stack<Integer> stack = new Stack<Integer>();
		StringBuilder sb = new StringBuilder();

		int max = Integer.parseInt(br.readLine());
		int m = 0;

		while (max-- > 0) {
			int num = Integer.parseInt(br.readLine()); 
			if (num > m) {
				while (num > m) {
					stack.push(++m);
					sb.append("+\n");
				}
				stack.pop();
				sb.append("-\n");
			} else {
				boolean found = false;
				if (!stack.empty()) {
					int top = stack.peek();
					stack.pop();
					sb.append("-\n");
					if (num == top) {
						found = true;
					}
				}
				if (!found) {
					System.out.println("NO");
					return;
				}
			}

		}
		System.out.println(sb);

	}
}

'알고리즘 & 자료구조 > 백준' 카테고리의 다른 글

백준 10845  (0) 2021.08.06
백준 1406  (0) 2021.08.05
백준 9012  (0) 2021.08.05
백준 9093  (0) 2021.08.05
백준 2558  (0) 2021.08.04