문제 출처: https://www.acmicpc.net/problem/1874




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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Stack;
 
public class algo6_1874_stack_sequence {
 
    public static void main(String[] args) throws NumberFormatException, IOException {
        BufferedReader reader= new BufferedReader(new InputStreamReader(System.in));
        StringBuilder builder = new StringBuilder();
        
        int N = Integer.parseInt(reader.readLine());
        Stack<Integer> stack = new Stack<>();
        boolean isPossible = true;
    
        for(int i=0, j=1 ; i<N ; i++){
            int input = Integer.parseInt(reader.readLine());
            if(!isPossible)
                continue;
            
            while(j <= input){
                stack.push(j++);
                builder.append("+\n");
            }
            if(stack.peek() == input){
                stack.pop();
                builder.append("-\n");
            }
            else{
                builder = new StringBuilder("NO\n");
                isPossible = false;
            }
        }
        
        System.out.println(builder);
    }
}
cs




'Algorithm' 카테고리의 다른 글

Divide and Conquer (분할정복법)  (0) 2018.08.07
백준 2504번: 괄호의 값  (0) 2018.08.01
백준 10799번: 쇠막대기  (0) 2018.07.20
백준 9012번: 괄호  (0) 2018.07.18
백준 10828번: 스택  (0) 2018.07.17

+ Recent posts