๋ฌธ์ (์ถ์ฒ: 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 |