๐ŸŒžAlgorithm/๐Ÿ”ฅBaekjoon

[Baekjoon] 6160_Election Time

๋ฟŒ์•ผ._. 2024. 10. 29. 13:36
๋ฌธ์ œ(์ถœ์ฒ˜: https://www.acmicpc.net/problem/6160)

< Election Time >

 

๋ฌธ์ œ ํ’€์ด 

 

1๋ผ์šด๋“œ) A ๊ฐ’์„ ๊ธฐ์ค€์œผ๋กœ ๋‚ด๋ฆผ์ฐจ์ˆœ ํ•œ ๋’ค K๊ฐœ๋ฅผ ๋ฝ‘์•„ ์ €์žฅํ•œ๋‹ค.

2๋ผ์šด๋“œ) B๊ฐ’์„ ๊ธฐ์ค€์œผ๋กœ ๋‚ด๋ฆผ์ฐจ์ˆœ ํ•œ ๋’ค 0๋ฒˆ์งธ ๊ฐ’์˜ index๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.

 

* 1๋ผ์šด๋“œ ์ง„ํ–‰ ํ›„ K๊ฐœ๋ฅผ ๋”ฐ๋กœ ๋ฝ‘์•„ ์ €์žฅํ•˜์ง€ ์•Š๊ณ  K๊ฐœ๋ฅผ ํƒ์ƒ‰ํ•˜์—ฌ ๋‹จ์ˆœ ๋น„๊ต๋กœ๋„ ๋‹ต์„ ๊ตฌํ•  ์ˆ˜ ์žˆ๋‹ค.

 

 my solution (Java)

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Comparator;
import java.util.StringTokenizer;
public class _6160_ { // Election Time
	static class Info {
		private int idx;
		private int A;
		private int B;
		public Info(int idx, int A, int B) {
			this.idx = idx;
			this.A = A;
			this.B = B;
		}
	}
	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 K = Integer.parseInt(st.nextToken());
		Info[] voteA = new Info[N];
		for (int i = 0; i < N; i++) {
			st = new StringTokenizer(bf.readLine());
			voteA[i] = new Info((i + 1), Integer.parseInt(st.nextToken()), Integer.parseInt(st.nextToken()));
		}
		Arrays.sort(voteA, new Comparator<Info>() {
			@Override
			public int compare(Info o1, Info o2) {
				return o2.A - o1.A;
			}
		});
		Info[] voteB = new Info[K];
		for (int i = 0; i < K; i++) {
			voteB[i] = voteA[i];
		}
		Arrays.sort(voteB, new Comparator<Info>() {
			@Override
			public int compare(Info o1, Info o2) {
				return o2.B - o1.B;
			}
		});
		System.out.println(voteB[0].idx);
	}
}
๋ณ€์ˆ˜)
N, K : ์‚ฌ๋žŒ ์ˆ˜, 1๋ผ์šด๋“œ์—์„œ ๋ฝ‘์„ ์ธ์›์ˆ˜
voteA, voteB : 1๋ผ์šด๋“œ, 2๋ผ์šด๋“œ ํˆฌํ‘œ ์ •๋ณด

 

Info

index, ์ฒซ ๋ฒˆ์งธ ๋ผ์šด๋“œ ํˆฌํ‘œ ๊ฒฐ๊ณผ, ๋‘ ๋ฒˆ์งธ ๋ผ์šด๋“œ ํˆฌํ‘œ ๊ฒฐ๊ณผ๋ฅผ ๋ณ€์ˆ˜๋กœ ๊ฐ€์ง

 

Main

N, K๋ฅผ ์ž…๋ ฅ๋ฐ›๋Š”๋‹ค. N๋งŒํผ ํˆฌํ‘œ ์ •๋ณด๋ฅผ ์ž…๋ ฅ๋ฐ›์•„ voteA ๋ฐฐ์—ด์— ์ €์žฅํ•œ๋‹ค. voteA ๋ฐฐ์—ด์„ ์ฒซ ๋ฒˆ์งธ ๋ผ์šด๋“œ ํˆฌํ‘œ ๊ฒฐ๊ณผ ๊ธฐ์ค€ ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•œ ํ›„ K๊ฐœ๋งŒํผ voteB ๋ฐฐ์—ด์— ์ €์žฅํ•œ๋‹ค. voteB ๋ฐฐ์—ด์„ ๋‘ ๋ฒˆ์งธ ๋ผ์šด๋“œ ํˆฌํ‘œ ๊ฒฐ๊ณผ ๊ธฐ์ค€ ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•œ ํ›„ 0๋ฒˆ์งธ ๊ฐ’์˜ index๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.



 

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

[Baekjoon] 11609_Class Time  (0) 2024.11.01
[Baekjoon] 6177_Statistics  (0) 2024.10.30
[Baekjoon] 10527_Judging Troubles  (0) 2024.10.28
[Baekjoon] 11968_High Card Wins  (0) 2024.10.25
[Baekjoon] 7596_MP3 Songs  (0) 2024.10.24