๐ŸŒžAlgorithm/๐Ÿ”ฅBaekjoon

[Baekjoon] 28107_ํšŒ์ „์ดˆ๋ฐฅ

๋ฟŒ์•ผ._. 2023. 8. 9. 08:43

Silver I

๋ฌธ์ œ(์ถœ์ฒ˜: https://www.acmicpc.net/problem/28107)

< ํšŒ์ „์ดˆ๋ฐฅ >

 

๋ฌธ์ œ ํ’€์ด 

 

์šฐ์„ ์ˆœ์œ„ ํ 2๊ฐœ๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ–ˆ๋‹ค.

1) ์†๋‹˜์˜ ์ฃผ๋ฌธ ๋ชฉ๋ก ์ €์žฅํ•˜๋Š” ์šฐ์„ ์ˆœ์œ„ ํ [์ดˆ๋ฐฅ ์ข…๋ฅ˜, ์†๋‹˜ ๋ฒˆํ˜ธ]

2) ์š”๋ฆฌ๋˜๋Š” ์ดˆ๋ฐฅ ์ข…๋ฅ˜ ์ €์žฅํ•˜๋Š” ์šฐ์„ ์ˆœ์œ„ ํ

 

 my solution (Java)

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

public class _28107_ { // ํšŒ์ „์ดˆ๋ฐฅ

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

		int n = Integer.parseInt(st.nextToken());
		int m = Integer.parseInt(st.nextToken());

		PriorityQueue<int[]> queue = new PriorityQueue<>(new Comparator<int[]>() {
			@Override
			public int compare(int[] o1, int[] o2) {
				if (o1[0] == o2[0]) {
					return o1[1] - o2[1];
				}
				return o1[0] - o2[0];
			}
		});

		for (int i = 0; i < n; i++) {
			st = new StringTokenizer(bf.readLine());
			int k = Integer.parseInt(st.nextToken());
			for (int j = 0; j < k; j++) {
				queue.add(new int[] { Integer.parseInt(st.nextToken()), i });
			}
		}

		PriorityQueue<Integer> num = new PriorityQueue<>();
		st = new StringTokenizer(bf.readLine());
		for (int i = 0; i < m; i++) {
			num.add(Integer.parseInt(st.nextToken()));
		}

		int result[] = new int[n];
		while (!num.isEmpty()) {
			if(queue.isEmpty()) {
				break;
			}
			if (queue.peek()[0] == num.peek()) {
				int temp[] = queue.poll();
				num.poll();
				result[temp[1]] += 1;
			} else if (queue.peek()[0] > num.peek()) {
				num.poll();
			} else {
				queue.poll();
			}
		}

		for (int i = 0; i < n; i++) {
			bw.write(result[i] + " ");
		}
		bw.flush();
	}
}

 

Main

๋ณ€์ˆ˜)
n : ์†๋‹˜์˜ ์ˆ˜
m : ์ดˆ๋ฐฅ์˜ ์ˆ˜
queue : PriorityQueue <int []>์— [์ดˆ๋ฐฅ ์ข…๋ฅ˜, ์†๋‹˜ ๋ฒˆํ˜ธ] ์ €์žฅ
num: PriorityQueue <Integer>์— ์š”๋ฆฌ๋˜๋Š” ์ดˆ๋ฐฅ ์ข…๋ฅ˜ ์ €์žฅ
result: i๋ฒˆ ์†๋‹˜์ด ๋จน์€ ์ดˆ๋ฐฅ์˜ ๊ฐœ์ˆ˜ ์ €์žฅ

 

- ์†๋‹˜์˜ ์ˆ˜ (n)์™€ ์ดˆ๋ฐฅ์˜ ์ˆ˜ (m) ์ž…๋ ฅ

- ์šฐ์„ ์ˆœ์œ„ ํ์— [์ดˆ๋ฐฅ ์ข…๋ฅ˜, ์†๋‹˜ ๋ฒˆํ˜ธ] ์ž…๋ ฅ๋ฐ›์•„ ์ €์žฅ (queue)

- ์šฐ์„ ์ˆœ์œ„ ํ์— ์š”๋ฆฌ๋˜๋Š” ์ดˆ๋ฐฅ ์ข…๋ฅ˜ ์ €์žฅ (num)

- num์ด ๋นŒ ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณต

: queue๊ฐ€ ๋น„์–ด์žˆ๋‹ค๋ฉด ์†๋‹˜์ด ์ดˆ๋ฐฅ์„ ๋จน์ง€ ์•Š์œผ๋ฏ€๋กœ ์ข…๋ฃŒ

: queue์™€ num์˜ ์ดˆ๋ฐฅ ์ข…๋ฅ˜๊ฐ€ ์ผ์น˜ํ•œ๋‹ค๋ฉด ๊ทธ ์†๋‹˜์ด ์ดˆ๋ฐฅ์„ ๋จน์Œ

: queue์˜ ๋งจ ์•ž ์ดˆ๋ฐฅ ๋ฒˆํ˜ธ๊ฐ€ num์˜ ๋งจ ์•ž ์ดˆ๋ฐฅ ๋ฒˆํ˜ธ๋ณด๋‹ค ํฌ๋‹ค๋ฉด num์˜ ๋งจ ์•ž ์ดˆ๋ฐฅ์„ ๋จน์„ ์ผ์ด ์—†์œผ๋ฏ€๋กœ num.poll()

: num์˜ ๋งจ ์•ž ์ดˆ๋ฐฅ ๋ฒˆํ˜ธ๊ฐ€ queue์˜ ๋งจ ์•ž ์ดˆ๋ฐฅ ๋ฒˆํ˜ธ๋ณด๋‹ค ํฌ๋‹ค๋ฉด queue์˜ ๋งจ ์•ž ์ดˆ๋ฐฅ์ด ์š”๋ฆฌ๋  ์ผ์ด ์—†์œผ๋ฏ€๋กœ queue.poll()

- result ๋ฐฐ์—ด ์ถœ๋ ฅ

 



'๐ŸŒžAlgorithm > ๐Ÿ”ฅBaekjoon' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[Baekjoon] 24511_queuestack  (0) 2023.08.11
[Baekjoon] 26042_์‹๋‹น ์ž…๊ตฌ ๋Œ€๊ธฐ ์ค„  (0) 2023.08.10
[Baekjoon] 1726_๋กœ๋ด‡  (0) 2023.08.08
[Baekjoon] 5464_์ฃผ์ฐจ์žฅ  (0) 2023.08.08
[Baekjoon] 11559_Puyo Puyo  (0) 2023.08.07