๐ŸŒžAlgorithm/๐Ÿ”ฅBaekjoon

[Baekjoon] 11637_์ธ๊ธฐ ํˆฌํ‘œ

๋ฟŒ์•ผ._. 2023. 10. 25. 22:37

Silver V

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

< ์ธ๊ธฐ ํˆฌํ‘œ >

 

๋ฌธ์ œ ํ’€์ด 

 

์šฐ์„ ์ˆœ์œ„ ํ์— R๊ณผ ํˆฌํ‘œ์ˆ˜๋ฅผ ์ €์žฅํ•œ๋‹ค. ํˆฌํ‘œ์ˆ˜๊ฐ€ ๋งŽ์€ ์ˆœ์„œ๋Œ€๋กœ ์ •๋ ฌํ•˜์—ฌ ํˆฌํ‘œ ์ˆ˜๊ฐ€ 2๋ช… ์ด์ƒ ๊ฐ™๋‹ค๋ฉด "no winner"๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค. 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;

public class _11637_ { // ์ธ๊ธฐ ํˆฌํ‘œ

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

		int t = Integer.parseInt(bf.readLine());

		PriorityQueue<int[]> queue = new PriorityQueue<>(new Comparator<int[]>() {

			@Override
			public int compare(int[] o1, int[] o2) {
				return o2[1] - o1[1];
			}

		});

		for (int i = 0; i < t; i++) {
			int n = Integer.parseInt(bf.readLine());
			int sum = 0;
			for (int k = 0; k < n; k++) {
				int x = Integer.parseInt(bf.readLine());
				sum += x;
				queue.add(new int[] { k + 1, x });
			}
			int temp[] = queue.poll();
			if (temp[1] == queue.peek()[1]) {
				bw.write("no winner \n");
			} else {
				if (temp[1] > sum / 2)
					bw.write("majority winner " + temp[0] + "\n");
				else
					bw.write("minority winner " + temp[0] + "\n");
			}
			queue.clear();
		}
		bw.flush();
	}
}

 

Main

๋ณ€์ˆ˜)
t : ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค ์ˆ˜
queue : ์šฐ์„ ์ˆœ์œ„ ํ <ํ›„๋ณด์ž ๋ฒˆํ˜ธ, ๋“ํ‘œ ์ˆ˜>
n : ํ›„๋ณด์ž ์ˆ˜
sum : ์ด ๋“ํ‘œ ์ˆ˜

 

- ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค ์ˆ˜(t) ์ž…๋ ฅ

- ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค ์ˆ˜(t) ๋งŒํผ ๋ฐ˜๋ณต

: ํ›„๋ณด์ž ์ˆ˜(n) ์ž…๋ ฅ

: ํ›„๋ณด์ž ์ˆ˜๋งŒํผ ํ›„๋ณด์ž์˜ ๋“ํ‘œ ์ˆ˜๋ฅผ ์ž…๋ ฅ๋ฐ›์•„ queue์— ์ €์žฅ ๋ฐ ์ด ๋“ํ‘œ ์ˆ˜์— ํ•ฉ

: queue์—์„œ ๋ฝ‘์€ ๊ฒƒ๊ณผ ๋‹ค์Œ queue ๊ฐ’์˜ ๋“ํ‘œ ์ˆ˜๊ฐ€ ๊ฐ™๋‹ค๋ฉด ์ตœ๋‹ค ๋“ํ‘œ์ž๊ฐ€ ์—†๋Š” ๊ฒƒ์ด๋ฏ€๋กœ "no winner" ์ถœ๋ ฅ

: ๊ฐ™์ง€ ์•Š๋‹ค๋ฉด ์ด ๋“ํ‘œ ์ˆ˜์˜ ์ ˆ๋ฐ˜์ด ๋„˜๋Š”์ง€ ํ™•์ธ ํ›„ ๋„˜์œผ๋ฉด "majority winner" ์ถœ๋ ฅ, ์ ˆ๋ฐ˜์ด ๋„˜์ง€ ์•Š์œผ๋ฉด "minority winner" ์ถœ๋ ฅ