ν•΄λ²„λ‹ˆ 2023. 11. 28. 10:11
λ°˜μ‘ν˜•

 

 

 

문제 

https://www.acmicpc.net/problem/12789

 

12789번: 도킀도킀 κ°„μ‹λ“œλ¦¬λ―Έ

μΈν•˜λŒ€ν•™κ΅ ν•™μƒνšŒμ—μ„œλŠ” 쀑간, 기말고사 λ•Œλ§ˆλ‹€ μ‹œν—˜ 곡뢀에 μ§€μΉœ ν•™μš°λ“€μ„ μœ„ν•΄ 간식을 λ‚˜λˆ μ£ΌλŠ” 간식 λ“œλ¦¬λ―Έ 행사λ₯Ό μ‹€μ‹œν•œλ‹€. μŠΉν™˜μ΄λŠ” μ‹œν—˜ 기간이 될 λ•Œλ§ˆλ‹€ 간식을 받을 생각에 두근두

www.acmicpc.net

 

 

 

 

κ·€μ—¬μš΄ 간식 문제

κ³Όμ—° μŠΉν™˜μ΄λŠ” 간식을 먹을 수 μžˆμ„κΉŒ...?

 

 

 

 

 

 

 

 

풀이

1. stack 2개둜 ν’€κΈ° 

μŠ€νƒ 2개둜 ν’€μ—ˆλŠ”λ° 큐 ν•˜λ‚˜ μŠ€νƒ ν•˜λ‚˜λ‘œλ„ 풀어도 될 것 κ°™λ‹€. 

거꾸둜 μŠ€νƒμ— λ„£μ—ˆλŠ”λ°, μ•Œκ³ λ³΄λ‹ˆ 큐λ₯Ό μ‚¬μš©ν•˜λ©΄ λ˜‘λ°”λ‘œ μ‚½μž…ν•˜κ³  μΆ”μΆœν•  수 μžˆμ—ˆλ‹€. 

 

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.Stack;
import java.util.StringTokenizer;

public class Main {
    public static int peekStack(Stack<Integer> stack) {
        if (stack.isEmpty()) {
            return -1;
        } else {
            return stack.peek();
        }
    }

    public static void popStack(Stack<Integer> stack) {
        if (!stack.isEmpty()) {
            stack.pop();
        }
    }

    public static void pushStack(int x, Stack<Integer> stack) {
        stack.push(x);
    }

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));

        int num = Integer.parseInt(br.readLine());
        int start = 1;
        int[] arr = new int[num];

        StringTokenizer st = new StringTokenizer(br.readLine());
        for (int i = 0; i < num; i++) {
            arr[i] = Integer.parseInt(st.nextToken());
        }

        Stack<Integer> stack1 = new Stack<>();
        Stack<Integer> stack2 = new Stack<>();

        for (int i = arr.length - 1; i >= 0; i--) {
            stack1.add(arr[i]);
        }

        while (!stack1.isEmpty()) {
            if (peekStack(stack1) == start) {
                popStack(stack1);
                start++;
            } else if (peekStack(stack2) == start) {
                popStack(stack2);
                start++;
            } else {
                int peekNum = peekStack(stack1);
                popStack(stack1);
                if (peekNum != -1) {
                    pushStack(peekNum, stack2);
                }
            }
        }

        int length = stack2.size();
        for (int i = 0; i < length; i++) {
            if (stack2.peek() == start) {
                popStack(stack2);
                start++;
            }
        }

        if (stack1.isEmpty() && stack2.isEmpty()) {
            bw.write("Nice");
        } else {
            bw.write("Sad");
        }

        bw.flush();
        bw.close();
    }
}

 

 

 

 

 

 

 

 

2. queueλž‘ stack으둜 ν’€κΈ° 

package algorithm;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;
import java.util.StringTokenizer;

public class Algorithm {
    public static int peekStack(Stack<Integer> stack) {
        if (stack.isEmpty()) {
            return -1;
        } else {
            return stack.peek();
        }
    }

    public static void popStack(Stack<Integer> stack) {
        if (!stack.isEmpty()) {
            stack.pop();
        }
    }

    public static void pushStack(int x, Stack<Integer> stack) {
        stack.push(x);
    }
    
    public static int peekQueue(Queue<Integer> queue) {
        if(queue.isEmpty()) {
            return -1;
        } else {
            return queue.peek();
        }
    }
    
    public static void popQueue(Queue<Integer> queue) {
        if(!queue.isEmpty()) {
            queue.poll();
        }
    }

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));

        int num = Integer.parseInt(br.readLine());
        int start = 1;
        int[] arr = new int[num];
        Queue<Integer> queue = new LinkedList<>();
        Stack<Integer> stack2 = new Stack<>();
        
        StringTokenizer st = new StringTokenizer(br.readLine());
        for (int i = 0; i < num; i++) {
            queue.add(Integer.parseInt(st.nextToken()));
        }
      

        while (!queue.isEmpty()) {
            if (peekQueue(queue) == start) {
                popQueue(queue);
                start++;
            } else if (peekStack(stack2) == start) {
                popStack(stack2);
                start++;
            } else {
                int peekNum = peekQueue(queue);
                popQueue(queue);
                if (peekNum != -1) {
                    pushStack(peekNum, stack2);
                }
            }
        }

        int length = stack2.size();
        for (int i = 0; i < length; i++) {
            if (stack2.peek() == start) {
                popStack(stack2);
                start++;
            }
        }

        if (queue.isEmpty() && stack2.isEmpty()) {
            bw.write("Nice");
        } else {
            bw.write("Sad");
        }

        bw.flush();
        bw.close();
    }
}

 

 

 

 

 

 

 

μ •λ‹΅

1. queueλž‘ stackμ΄λž‘ ν’€κΈ°

2. stack 2개둜 ν’€κΈ°

 

 

 

 

 

 

λ°˜μ‘ν˜•