Algorithm

๋ฐฑ์ค€ 1158 | Queue, ์š”์„ธํ‘ธ์Šค ๋ฌธ์ œ

osean 2020. 11. 25. 00:28

Queue

Queue๋Š” ์ž๋ฃŒ๊ตฌ์กฐ ์ค‘ ํ•˜๋‚˜๋กœ, FIFO(First In First Out, ์„ ์ž…์„ ์ถœ) ๊ตฌ์กฐ๋ฅผ ๊ฐ€์ง€๋Š” ์ž๋ฃŒ๊ตฌ์กฐ์ด๋‹ค.
๋จผ์ € ๋“ค์–ด์˜จ ๋ฐ์ดํ„ฐ๊ฐ€ ๋จผ์ € ๋‚˜๊ฐ€๋Š” ๊ตฌ์กฐ๋ฅผ ๋œปํ•˜๋ฉฐ, ๋ฐ์ดํ„ฐ๊ฐ€ ์ž…๋ ฅ๋œ ์‹œ๊ฐ„ ์ˆœ์„œ๋Œ€๋กœ ์ฒ˜๋ฆฌํ•ด์•ผ ํ•  ํ•„์š”๊ฐ€ ์žˆ๋Š” ์ƒํ™ฉ์— ์‚ฌ์šฉํ•œ๋‹ค. ๋Œ€ํ‘œ์ ์ธ ์˜ˆ๋กœ ๋„ˆ๋น„ ์šฐ์„  ํƒ์ƒ‰(BFS)์—์„œ ์ฃผ๋กœ ์‚ฌ์šฉํ•œ๋‹ค.

์š”์„ธํ‘ธ์Šค ์ˆœ์—ด

 

1158๋ฒˆ: ์š”์„ธํ‘ธ์Šค ๋ฌธ์ œ

์ฒซ์งธ ์ค„์— N๊ณผ K๊ฐ€ ๋นˆ ์นธ์„ ์‚ฌ์ด์— ๋‘๊ณ  ์ˆœ์„œ๋Œ€๋กœ ์ฃผ์–ด์ง„๋‹ค. (1 ≤ K ≤ N ≤ 5,000)

www.acmicpc.net

SSAFY๋ฅผ ์ค€๋น„ํ•˜๋ฉฐ ์˜คํ”ˆ์นดํ†ก๋ฐฉ์—์„œ ์ด๋Ÿฐ ์ €๋Ÿฐ ์ •๋ณด๋ฅผ ๊ณต์œ ํ•˜๋˜ ์ค‘ CT๋ฅผ ๋Œ€๋น„ํ•˜๊ธฐ ์œ„ํ•ด ์š”์„ธํ‘ธ์Šค ์ˆœ์—ด์„ ๊ณต๋ถ€ํ•˜๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™๋‹ค๋Š” ์ด์•ผ๊ธฐ๋ฅผ ๋“ค์–ด ์ ‘ํ•˜๊ฒŒ ๋˜์—ˆ๋‹ค.

ํ•ด๋‹น ๋ฌธ์ œ๋Š” N๋ช…์˜ ์ธ์›์ˆ˜(๋ฐฐ์—ด์˜ ๊ธธ์ด)์™€ ์ˆœ์„œ๋Œ€๋กœ M ๋ฒˆ์งธ์— ์ œ๊ฑฐํ•  ์ •์ˆ˜(์ถœ๋ ฅ๋˜๋Š” ์ •์ˆ˜)๊ฐ€ ์ฃผ์–ด์ง„๋‹ค.

๋‚˜๋Š” ํ•ด๋‹น ๋ฌธ์ œ๋ฅผ Queue ์ž๋ฃŒ ๊ตฌ์กฐ๋ฅผ ์ด์šฉํ•ด์„œ ํ•ด๊ฒฐํ–ˆ๋‹ค.
๋ฌธ์ œ๋Š” ์ƒ๊ฐ๋ณด๋‹ค ๊ฐ„๋‹จํžˆ ํ•ด๊ฒฐํ–ˆ๊ณ , ์ •ํ™•ํžˆ ์ดํ•ดํ•˜๊ธฐ ์œ„ํ•ด์„œ ๊ทธ๋ฆผ๊ณผ ํ…Œ์ด๋ธ”์„ ๋งŒ๋“ค์–ด ํ•˜๋‚˜์”ฉ ์ƒ๊ฐํ•ด ๋ณด์•˜๋‹ค.

์ฝ”๋“œ๋กœ ํ†บ์•„๋ณด๊ธฐ

๋งŽ์€ ์‚ฌ๋žŒ๋“ค์ด ArrayList ํด๋ž˜์Šค๋ฅผ ์ด์šฉํ•ด ๋” ๊ฐ„๋‹จํ•˜๊ณ  ๋น ๋ฅด๊ฒŒ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ๋„๋ก ํ–ˆ๋Š”๋ฐ, ๋‚˜๋Š” ์•„์ง ์ž๋ฃŒ๊ตฌ์กฐ์— ๋Œ€ํ•œ ์ดํ•ด๊ฐ€ ๋งŽ์ด ๋–จ์–ด์ ธ์„œ Queue๋ฅผ ์ด์šฉํ•ด ํ•ด๊ฒฐํ•˜๊ณ ์ž ํ–ˆ๋‹ค.

์œ„์˜ ๊ทธ๋ฆผ๊ณผ ๊ฐ™์ด 0 ๋ฒˆ์งธ ์ธ๋ฑ์Šค๋ฅผ ์ œ์™ธํ•˜๊ณ , cnt ๋ณ€์ˆ˜๋ฅผ ํ™œ์šฉํ•ด cnt ๋ณ€์ˆ˜์˜ ๊ฐ’์„ ์ฆ๊ฐ€ ์‹œ์ผœ ํ•ด๋‹น ๋ณ€์ˆ˜๊ฐ’์ด M ๋ฒˆ์งธ๋ผ๋ฉด queue ๋ณ€์ˆ˜์˜ ๊ฐ€์žฅ ์•ž์— ์žˆ๋Š” ๋ฐ์ดํ„ฐ๋ฅผ poll()ํ•˜๊ณ , ๋‹ค์‹œ ํ•ด๋‹น ๋ฐ์ดํ„ฐ๋ฅผ offer()ํ•˜๋Š” ํ˜•์‹์œผ๋กœ ๊ตฌํ˜„ํ•˜์˜€๋‹ค.

package ์‹ฌ์„ฑํ—Œ.์•Œ๊ณ ๋ฆฌ์ฆ˜_5์ฃผ์ฐจ.๋ฐฑ์ค€;

import java.io.*;
import java.util.*;

public class ์š”์„ธํ‘ธ์Šค๋ฌธ์ œ_1158 {

    static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
    static StringBuffer sb = new StringBuffer();
    static Queue<Integer> queue = new LinkedList<Integer>();
    static int n, m;

    public static void main(String[] args) throws Exception {
        StringTokenizer st = new StringTokenizer(br.readLine());
        n = Integer.parseInt(st.nextToken());
        m = Integer.parseInt(st.nextToken());
        int arr[] = new int[n + 1];

        for (int i = 1; i < arr.length; i++) {
            arr[i] = i;
            queue.offer(arr[i]);
        }
        String result = turn(arr, m);
        bw.write(result.toString());
        bw.flush();
        bw.close();
    }

    public static String turn(int[] arr, int turn) {
        int cnt = 1;
        sb.append("<");
        while (!queue.isEmpty()) {
            if (cnt == turn) {
                cnt = 1;
                int save = queue.poll();
                sb.append(save);
                if (!queue.isEmpty()) {
                    sb.append(", ");
                }
            } else {
                queue.offer(queue.poll());
                cnt++;
            }
        }
        sb.append(">");
        return sb.toString();
    }
}

์ฃผ์ €๋ฆฌ

๋งŽ์€ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์ ‘ํ•ด๋ณธ ๊ฒƒ์€ ์•„๋‹ˆ์ง€๋งŒ ์ง€๊ธˆ๊นŒ์ง€ ํ’€์–ด ๋ณธ ๋ฌธ์ œ ์ค‘์—์„œ ๋‚ด ์ˆ˜์ค€๊ณผ ๋”ฑ ๋งž๋Š” ๋ฌธ์ œ๋ผ๊ณ  ์ƒ๊ฐํ•œ๋‹ค.
์•„์ง ์—ฌ๋Ÿฌ๋ชจ๋กœ ๋ถ€์กฑํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋‚ด๊ฐ€ ์ง์ ‘ ์ง  ์ฝ”๋“œ๊ฐ€ ์•„๋‹Œ ๋‹ค๋ฅธ ์‚ฌ๋žŒ์˜ ์ฝ”๋“œ๋ฅผ ์ฐธ๊ณ ํ•˜๋ฉฐ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐ ํ–ˆ๋Š”๋ฐ, ์ด๋ฒˆ ๋ฌธ์ œ๋Š” ์˜ค๋กฏ์ด ๋‚˜ ํ˜ผ์ž ํ•ด๊ฒฐํ•ด์„œ ์กฐ๊ธˆ ๋ฟŒ๋“ฏํ•˜๋‹ค.

ํ•˜์ง€๋งŒ ์•„์ง ๊ฒช๊ณ  ๊นจ์•ผ ํ•  ๋ฒฝ๋“ค์ด ๋งŽ์ด ์žˆ์œผ๋‹ˆ ๋” ํƒ„ํƒ„ํžˆ ๊ณต๋ถ€ํ•ด์„œ ์‹ค๋ ฅ์„ ์Œ“์•„ ์˜ฌ๋ ค์•ผ๊ฒ ๋‹ค!