1 minute read

๐Ÿ“Œ ๋‚œ์ด๋„

๐Ÿฅˆ Silver 3


๐Ÿ“Œ ๋ฌธ์ œ

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


image

image


๐Ÿ“Œ ํ’€์ด

์ด ๋ฌธ์ œ๋ฅผ ์ฝ๋Š”๋ฐ, ์ฒ˜์Œ์—๋Š” ๋Œ€์ฒด ์ž…๋ ฅ์ด ๋ฌด์Šจ ์†Œ๋ฆฌ์ธ๊ฐ€โ€ฆ ํ•œ์ฐธ ๋‹ค์‹œ ์ฝ์–ด๋ณด์•˜๋‹ค.
์ฐฌ์ฐฌํžˆ ์ฝ์–ด๋ณด๋‹ˆ, ๋งจ ์ฒ˜์Œ ์ฃผ๋Š” ์ˆซ์ž๋Š” ํ…Œ์Šคํ„ฐ ์ผ€์ด์Šค์˜ ์ˆซ์ž์ด๊ณ , ๊ทธ ์ˆซ์ž๋งŒํผ 2์ค„์งœ๋ฆฌ์˜ ํ…Œ์Šคํ„ฐ ์ผ€์ด์Šค์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ์ฃผ๋Š” ๋ฌธ์ œ์˜€๋‹ค.

์ฒ˜์Œ์—๋Š” Queue๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด์„œ ๋นผ์„œ ๋’ค์— ๋„ฃ์„๋–„๋งˆ๋‹ค, target์˜ ์ฃผ์†Œ๋ฅผ -1์”ฉ ํ•˜๋‹ค๊ฐ€ target ์ฃผ์†Œ๊ฐ’์ด -1๋ณด๋‹ค ์ž‘์•„์ง€๋ฉด ์ตœ๋Œ€ ์ˆซ์ž๊ฐ€ ๋˜๋Š” ์‹์œผ๋กœ ๋˜๊ฒŒ ๋ณต์žกํ•˜๊ฒŒ ๊ตฌํ˜„์„ ํ–ˆ๋‹ค.

๊ทธ๋Ÿฌ๋‹ค๊ฐ€ ๋„ˆ๋ฌด ๋ณต์žกํ•ด์ ธ์„œ ๊ทธ๋ƒฅ 000100๊ฐ™์ด ์ฃผ์†Œ๊ฐ’์„ ์ €์žฅํ•˜๊ณ  ์žˆ๋Š” Queue๋ฅผ ์ƒˆ๋กœ ๋งŒ๋“ค์–ด ์ฃผ์—ˆ๋‹ค.
๊ทธ๋ž˜์„œ, Queue์— ๊ฐ’์„ ๋„ฃ์–ด์ค„๋•Œ ๊ฐ€์žฅ ํฐ ๊ฐ’์„ ์ €์žฅํ•ด ๋†“์•˜๋‹ค๊ฐ€ max-0๊ฐ’๊นŒ์ง€์˜ for๋ฌธ์„ ๋งŒ๋“ค๊ณ , Queue๊ฐ’์ด i๊ฐ’๊ณผ ๊ฐ™์„๋•Œ ๊ทธ ๊ฐ’์„ ๋นผ๋ฒ„๋ฆฌ๊ณ , result์˜ ๊ฐ’์„ +1ํ•ด์ฃผ์—ˆ๋‹ค.
์ดํ›„ Index์˜ ๊ฐ’์— 0์ด ์•„๋‹Œ 1์ด ๋‚˜์˜ค๋ฉด result๊ฐ’์„ ์ถœ๋ ฅํ•ด์ฃผ์—ˆ๋‹ค.

์ž๋ฃŒ๊ตฌ์กฐ ๊ด€๋ จ๋œ ๋ฌธ์ œ๋Š” ์•„์ง๊นŒ์ง€๋Š” ์‰ฌ์šด ๊ฒƒ ๊ฐ™๋‹ค.


๐Ÿ“Œ Code

package DS;

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

public class N1966 {
    public static Queue<Integer> box;
    public static Queue<Integer> targetBox;

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st;
        int num = Integer.parseInt(br.readLine());
        int target;
        int count;
        box = new LinkedList<>();
        targetBox = new LinkedList<>();
        for (int i = 0; i < num; i++) {
            st = new StringTokenizer(br.readLine());
            count = Integer.parseInt(st.nextToken());
            target = Integer.parseInt(st.nextToken());

            st = new StringTokenizer(br.readLine());
            PrinterQueue(count, target, st);
            box.clear();
            targetBox.clear();
        }
    }

    public static void PrinterQueue(int count, int target, StringTokenizer st) {
        int num;
        int targetNum;
        int max = 0;
        int result = 0;
        for (int i = 0; i < count; i++) {
            num = Integer.parseInt(st.nextToken());
            max = Math.max(max, num);
            box.add(num);
            if(i == target) {
                targetBox.add(1);
            }else{
                targetBox.add(0);
            }
        }
        int totalCount;
        int currentCount = count;
        boolean flag = false;
        for (int i = max; i > -1; i--) {
            totalCount = currentCount;
            for (int j = 0; j < totalCount; j++) {
                num = box.poll();
                targetNum = targetBox.poll();

                if (num == i) {
                    currentCount -= 1;
                    result += 1;
                    if (targetNum == 1) {
                        System.out.println(result);
                        return;
                    }
                    flag = true;
                    break;
                } else {
                    box.offer(num);
                    targetBox.offer(targetNum);
                }
            }
            if(flag){
                flag = false;
                i++;
            }
        }
    }
}

package (์ด๋ฆ„); ๋ฅผ ๋•Œ๊ณ , class ์ด๋ฆ„์„ Main์œผ๋กœ ๋ณ€๊ฒฝํ•˜๋ฉด ๋œ๋‹ค.



๊ฐœ์ธ ๊ณต๋ถ€ ๊ธฐ๋ก์šฉ ๋ธ”๋กœ๊ทธ์ž…๋‹ˆ๋‹ค.
ํ‹€๋ฆฌ๊ฑฐ๋‚˜ ์˜ค๋ฅ˜๊ฐ€ ์žˆ์„ ๊ฒฝ์šฐ ์ œ๋ณดํ•ด์ฃผ์‹œ๋ฉด ๊ฐ์‚ฌํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.๐Ÿ˜