๐Ÿ˜‰/์ฝ”๋”ฉํ…Œ์ŠคํŠธ

[์•Œ๊ณ ๋ฆฌ์ฆ˜] ํ”„๋ฆฐํ„ฐ ํ (๋ฐฑ์ค€, 1966)

ruhz 2022. 1. 27. 13:35

๋ฌธ์ œ

 

1966๋ฒˆ: ํ”„๋ฆฐํ„ฐ ํ

์—ฌ๋Ÿฌ๋ถ„๋„ ์•Œ๋‹ค์‹œํ”ผ ์—ฌ๋Ÿฌ๋ถ„์˜ ํ”„๋ฆฐํ„ฐ ๊ธฐ๊ธฐ๋Š” ์—ฌ๋Ÿฌ๋ถ„์ด ์ธ์‡„ํ•˜๊ณ ์ž ํ•˜๋Š” ๋ฌธ์„œ๋ฅผ ์ธ์‡„ ๋ช…๋ น์„ ๋ฐ›์€ ‘์ˆœ์„œ๋Œ€๋กœ’, ์ฆ‰ ๋จผ์ € ์š”์ฒญ๋œ ๊ฒƒ์„ ๋จผ์ € ์ธ์‡„ํ•œ๋‹ค. ์—ฌ๋Ÿฌ ๊ฐœ์˜ ๋ฌธ์„œ๊ฐ€ ์Œ“์ธ๋‹ค๋ฉด Queue ์ž๋ฃŒ๊ตฌ์กฐ์—

www.acmicpc.net

 

ํ’€์ด

 

GitHub - ruhz3/coding-test: To prepare for coding test

To prepare for coding test. Contribute to ruhz3/coding-test development by creating an account on GitHub.

github.com

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

public class Main {
	public static int print(LinkedList<Integer> queue, int[] priority) {
		int first;
		boolean canPrint;
		while(true) {
			// 00. canPrint๊ฐ€ ์ค‘๊ฐ„์— false๋กœ ๋ฐ”๋€Œ์ง€ ์•Š๋Š”๋‹ค๋ฉด ์ถœ๋ ฅ๋  ๊ฒƒ์ด๋‹ค. 
			canPrint = true;
			
			// 01. ๋Œ€๊ธฐ์—ด์˜ ์ฒซ ๋ฒˆ์งธ ์›์†Œ ๋’ค์— ์šฐ์„ ์ˆœ์œ„๊ฐ€ ๋†’์€ ๊ฒƒ์ด ์žˆ๋‹ค๋ฉด, ๋งจ ๋’ค๋กœ ๋ณด๋‚ด๊ณ  canPrint๋ฅผ false๋กœ ๋ณ€๊ฒฝํ•œ๋‹ค.  
			first = queue.peek();
			for(int i = 1, len = queue.size(); i < len; i++) {
				if(priority[queue.get(i)] > priority[first]) {
					queue.add(queue.poll());
					canPrint = false;
					break;
				}
			}
			
			// 02. ๋’ค์— ์šฐ์„ ์ˆœ์œ„๊ฐ€ ๋” ํฐ ๊ฒƒ์ด ์—†์–ด์ง€๋ฉด, ์ด ์กฐ๊ฑด๋ฌธ์„ ํ†ต๊ณผํ•  ๊ฒƒ์ด๋‹ค. 
			if(canPrint)
				break;
		}
		
		return queue.poll();
	}
	
	public static void main(String args[]) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st;
		int T = Integer.parseInt(br.readLine());
		int N, M;
		LinkedList<Integer> queue = new LinkedList<>();
		int[] priority;
		int count;
		while(T > 0) {
			// 00. N, M์„ ์ž…๋ ฅ ๋ฐ›๋Š”๋‹ค.
			st = new StringTokenizer(br.readLine());
			N = Integer.parseInt(st.nextToken());
			M = Integer.parseInt(st.nextToken());
			
			// 01. ํ˜„์žฌ ํ ์ƒํƒœ๋ฅผ ์ž…๋ ฅ ๋ฐ›๋Š”๋‹ค.
			queue.clear();
			priority = new int[N];
			st = new StringTokenizer(br.readLine());
			for(int i = 0; i < N; i++) {
				priority[i] = Integer.parseInt(st.nextToken()); 
				queue.add(i);
			}
			
			// 02. ์ถœ๋ ฅ์„ ์ง„ํ–‰ํ•˜๊ณ , ์›ํ•˜๋Š” ๋ฌธ์„œ ๋ฒˆํ˜ธ๊ฐ€ ๋‚˜์˜ค๋ฉด ๋ช‡ ๋ฒˆ๋งŒ์— ๋‚˜์™”๋Š”์ง€ ์ถœ๋ ฅํ•œ๋‹ค.
			count = 1;
			while(true) {
				if(print(queue, priority) == M) {
					System.out.println(count);
					break;
				} else {
					count++;
				}
			}
			T--;
		}
	}
}

๋ฌธ์ œ๊ฐ€ ์ฃผ๋Š” ๋Œ€๋กœ ๊ทธ๋Œ€๋กœ ๊ตฌํ˜„ํ–ˆ๋‹ค. ํ”„๋ฆฐํ„ฐ 'ํ'๋ผ๋Š” ๋ง์— ์•„๋ฌด ์ƒ๊ฐ์—†์ด Collection์˜ Queue๋กœ ๊ตฌํ˜„์„ ์‹œ๋„ ํ–ˆ๋‹ค. ํ•˜์ง€๋งŒ ์ž‘์„ฑํ•˜๋‹ค ๋ง‰ํžˆ๊ณ  ๋‚˜์„œ์•ผ, Queue๋Š” ์ค‘๊ฐ„์˜ ์›์†Œ๋“ค์„ ์ˆœํšŒํ•  ์ˆ˜๊ฐ€ ์—†๋‹ค๋Š” ์‚ฌ์‹ค์ด ๊ธฐ์–ต๋‚ฌ๋‹ค! ๋”ฐ๋ผ์„œ ์ค‘๊ฐ„์˜ ์›์†Œ๋“ค์„ ์ˆœํšŒํ•  ์ˆ˜ ์žˆ๋Š” ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ๋กœ ์ฝ”๋“œ๋ฅผ ์ˆ˜์ •ํ–ˆ๋‹ค. ์‹ค์ œ ์ฝ”๋”ฉ ํ…Œ์ŠคํŠธ์—์„œ ํ—ค๋งค์ง€ ์•Š์œผ๋ ค๋ฉด, Collection๋“ค์˜ ์ƒ์†๊ด€๊ณ„์™€ ๊ฐ ๋ฉ”์†Œ๋“œ๋ฅผ ์•Œ๊ณ  ์žˆ๋Š” ๊ฒƒ์ด ์ข‹์„ ๊ฒƒ ๊ฐ™๋‹ค.

 

Collection์˜ ์ƒ์†๊ด€๊ณ„๋ฅผ ํ•œ ๋ฒˆ ์ •๋ฆฌํ•ด์•ผ ํ•  ๊ฒƒ ๊ฐ™๋‹ค.