๐ŸŒžAlgorithm/๐Ÿ”ฅBaekjoon

[Baekjoon] 7774_์ฝ˜์„ผํŠธ

๋ฟŒ์•ผ._. 2024. 11. 5. 16:02
๋ฌธ์ œ(์ถœ์ฒ˜: https://www.acmicpc.net/problem/7774)

< ์ฝ˜์„ผํŠธ >

 

๋ฌธ์ œ ํ’€์ด 

 

์šฐ์„ ์ˆœ์œ„ ํ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ฝ˜์„ผํŠธ๊ฐ€ ๋งŽ์€ ์ˆœ์œผ๋กœ ๊ฝ‚๋Š”๋‹ค.

 

 my solution (Java)

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Collections;
import java.util.PriorityQueue;
import java.util.StringTokenizer;
public class _7774_ { // ์ฝ˜์„ผํŠธ
	public static void main(String[] args) throws IOException {
		BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st = new StringTokenizer(bf.readLine());
		int n = Integer.parseInt(st.nextToken());
		int m = Integer.parseInt(st.nextToken());
		PriorityQueue<Integer> a = new PriorityQueue<>(Collections.reverseOrder());
		PriorityQueue<Integer> b = new PriorityQueue<>(Collections.reverseOrder());
		st = new StringTokenizer(bf.readLine());
		for (int i = 0; i < n; i++) {
			a.add(Integer.parseInt(st.nextToken()));
		}
		st = new StringTokenizer(bf.readLine());
		for (int i = 0; i < m; i++) {
			b.add(Integer.parseInt(st.nextToken()));
		}
		int result = 0;
		while (!a.isEmpty() && !b.isEmpty()) {
			if (a.size() < n) {
				result -= 1;
			}
			int temp = a.poll();
			while (temp-- > 0 && !b.isEmpty()) {
				result += b.poll();
			}
		}
		System.out.println(result);
	}
}
๋ณ€์ˆ˜)
n, m : ์ฒซ ๋ฒˆ์งธ, ๋‘ ๋ฒˆ์งธ ๋ฉ€ํ‹ฐํƒญ์˜ ๊ฐœ์ˆ˜
a, b : ๋ฉ€ํ‹ฐํƒญ์— ์žˆ๋Š” ์ฝ˜์„ผํŠธ ๊ฐœ์ˆ˜
result : ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์ปดํ“จํ„ฐ ๊ฐœ์ˆ˜

 

์ฒซ ๋ฒˆ์งธ, ๋‘ ๋ฒˆ์งธ ๋ฉ€ํ‹ฐํƒญ์˜ ๊ฐœ์ˆ˜๋ฅผ ์ž…๋ ฅ๋ฐ›๋Š”๋‹ค. ๋ฉ€ํ‹ฐํƒญ์˜ ๊ฐœ์ˆ˜๋งŒํผ ์ฝ˜์„ผํŠธ ๊ฐœ์ˆ˜๋ฅผ ์ž…๋ ฅ๋ฐ›์•„ ๊ฐ ์šฐ์„ ์ˆœ์œ„ ํ์— ์ €์žฅํ•˜์—ฌ ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•œ๋‹ค. a์™€ b์˜ ์šฐ์„ ์ˆœ์œ„ ํ๊ฐ€ ๋น„์–ด์žˆ์ง€ ์•Š์„ ๋•Œ ๋‹ค์Œ ๊ณผ์ •์„ ๋ฐ˜๋ณตํ•œ๋‹ค.

1) a ์šฐ์„ ์ˆœ์œ„ ํ๊ฐ€ n๊ฐœ๋ณด๋‹ค ์ž‘๋‹ค๋ฉด ํ‘œ์ค€ A ์ฝ˜์„ผํŠธ๋ฅผ ํ•˜๋‚˜ ์‚ฌ์šฉํ•œ ๊ฒƒ์ด๋ฏ€๋กœ result-1

2) a ์šฐ์„ ์ˆ˜์œ„ ํ poll

3) ์•ž์—์„œ poll ํ•œ ๊ฐœ์ˆ˜๋งŒํผ b ์šฐ์„ ์ˆœ์œ„ ํ์—์„œ poll ํ•˜์—ฌ result์— ๊ฐ’ ์ถ”๊ฐ€

 

์ตœ์ข… result๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.



 

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

[Baekjoon] 7587_Anagrams  (0) 2024.11.07
[Baekjoon] 11785_Programming Contest Strategy  (0) 2024.11.06
[Baekjoon] 11235_Polling  (0) 2024.11.04
[Baekjoon] 11609_Class Time  (0) 2024.11.01
[Baekjoon] 6177_Statistics  (0) 2024.10.30