Notice
Recent Posts
Recent Comments
05-21 07:17
«   2024/05   »
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
Archives
Today
Total
관리 메뉴

Byeol Lo

N12789 - 도키도키 간식드리미 본문

Algorithm/Stack & Queue

N12789 - 도키도키 간식드리미

알 수 없는 사용자 2023. 12. 8. 20:15
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.util.StringTokenizer;
import java.util.Stack;

public class Main {
    static final BufferedReader BR = new BufferedReader(new InputStreamReader(System.in));
    static StringTokenizer ST;

    public static void main(String[] args) throws IOException{
        Integer.parseInt(BR.readLine());
        input();
    }

    private static void input() throws IOException {
        Stack<Integer> newLine = new Stack<>();
        int min = 0;
        int target;

        ST = new StringTokenizer(BR.readLine());

        while(ST.hasMoreTokens()) {
            target = Integer.parseInt(ST.nextToken());

            if(target == min+1) {
                min++;
            } else {
                while (!newLine.empty() && newLine.peek() == min + 1) {
                    min++;
                    newLine.pop();
                }
                newLine.add(target);
            }
        }
        if(newLine.empty())
            System.out.println("Nice");
        else if(newLine.peek() == min+1)
            System.out.println("Nice");
        else
            System.out.println("Sad");
    }
}

'Algorithm > Stack & Queue' 카테고리의 다른 글

N24511 - queuestack  (0) 2023.12.09
N2346 - 풍선 터뜨리기  (0) 2023.12.09
N28279 - 덱 2  (1) 2023.12.08
N28278 - 스택 2  (0) 2023.12.08
Comments