728x90
๐๋ฌธ์ ๋งํฌ
9017๋ฒ: ํฌ๋ก์ค ์ปจํธ๋ฆฌ
์ ๋ ฅ ๋ฐ์ดํฐ๋ ํ์ค์ ๋ ฅ์ ์ฌ์ฉํ๋ค. ์ ๋ ฅ์ T ๊ฐ์ ํ ์คํธ ์ผ์ด์ค๋ก ์ฃผ์ด์ง๋ค. ์ ๋ ฅ ํ์ผ์ ์ฒซ ๋ฒ์งธ ์ค์ ํ ์คํธ ์ผ์ด์ค์ ์๋ฅผ ๋ํ๋ด๋ ์ ์ T ๊ฐ ์ฃผ์ด์ง๋ค. ๋ ๋ฒ์งธ ์ค๋ถํฐ๋ ๋ ์ค์ ํ๋์
www.acmicpc.net
๐ป์ฝ๋
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
int T = Integer.parseInt(br.readLine());
for (int t = 0; t < T; t++) {
int N = Integer.parseInt(br.readLine());
int[] team = new int[201]; // ํ ์ ๋ณด๋ฅผ ์ ์ฅํ๋ ๋ฐฐ์ด(index : ํ ๋ฒํธ, value : ํ์ ์)
int[][] teamRunner = new int[N][2]; // ์ ์ ์ ๋ณด๋ฅผ ์ ์ฅํ๋ ๋ฐฐ์ด(index : ๋ฑ์, [0]ํ, [1]์ ์)
int[] teamScore = new int[201];
StringTokenizer st = new StringTokenizer(br.readLine());
for (int i = 0; i < N; i++) {
int teamNumber = Integer.parseInt(st.nextToken());
team[teamNumber]++;
teamRunner[i][0] = teamNumber;
}
//๋ฑ์ ๋ณ๋ก ์ ์ ๊ณ์ฐ
int point = 1;
for (int i = 0; i < N; i++) {
if (team[teamRunner[i][0]] < 6) {// ํ์ ์ฐธ๊ฐ ์ ์๊ฐ 6๋ช
๋ณด๋ค ์์ ๊ฒฝ์ฐ๋ ์ ์ ๊ณ์ฐ์์ ์ ์ธ
for (int j = 0; j < N; j++) {
if (teamRunner[j][0] == teamRunner[i][0]) teamRunner[j][1] = -1;
}
} else {
teamRunner[i][1] = point++;
}
}
/*for (int i = 0; i < N; i++) {
System.out.print(teamRunner[i][1] + " ");
}*/
int winner = 0;
int winnerScore = Integer.MAX_VALUE;
int winnerFifthScore = 0;
for (int i = 1; i < 201; i++) {
if (team[i] < 6) continue;
int count = 0;
int score = 0;
int fifthScore = 0;
for (int j = 0; j < N; j++) {
if (teamRunner[j][0] != i) continue;
if (teamRunner[j][1] == -1) continue;
count++;
if (count == 5) {//5๋ฒ์งธ ์ ์๋ผ๋ฉด ์ ์๋ง ์ ์ฅํ๊ณ ๋์ด๊ฐ
fifthScore = teamRunner[j][1];
break;
}
score += teamRunner[j][1];
}
if (score < winnerScore) { // ์ ์๊ฐ ๋ฎ์ ๊ฒฝ์ฐ
winner = i;
winnerScore = score;
winnerFifthScore = fifthScore;
} else if (score == winnerScore) { //์ ์๊ฐ ๊ฐ์ ๊ฒฝ์ฐ
if (fifthScore < winnerFifthScore) { //5๋ฒ์งธ ์ ์ ์ ์๋ฅผ ๋น๊ต
winner = i;
winnerScore = score;
winnerFifthScore = fifthScore;
}
}
//System.out.println(winner+" "+winnerScore+" "+winnerFifthScore);
}
sb.append(winner).append("\\n");
}
System.out.print(sb);
}
}
โณํ๊ณ
- ์ฒ์์๋ ๋ฌธ์ ์กฐ๊ฑด์ด ๊น๋ค๋ก์์ ์ด๋ป๊ฒ ์ ๋ณด๋ฅผ ์ ์ฅํ๊ณ ๊ณ์ฐํด์ผ ํ ์ง ๋ง๋งํ๋๋ฐ, ์กฐ๊ฑด๋๋ก ์ฐจ๋ถํ ๋ฐ๋ผ๊ฐ๋ค ๋ณด๋ ํ์ดํ ์ ์์๋ค.
- ์ ์๋ฅผ ๊ณ์ฐํ๋ ๋ฐ๋ณต๋ฌธ์์๋ 6๋ช ์ด ์ดํ์ธ ํ์ ๋์ด๊ฐ๋ ์ฝ๋๊ฐ ์์๋๋ฐ, ์ฐ์น์๋ฅผ ํ์ํ๋ ๋ฐ๋ณต๋ฌธ์์ ๋นผ๋จน์ด์ ํ๋ฆฐ ๊ณณ์ ์ฐพ๋๋ฐ ์ด๋ ค์์ด ์์๋ค.
728x90
'Algorithm > Baekjoon' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Algorithm/Baekjoon] ์ฃผ์ - 11501 (S2/JAVA) (0) | 2023.08.01 |
---|---|
[Algorithm/Baekjoon] ๋น๋ฌผ - 14719 (G5/JAVA) (0) | 2023.07.25 |
[Algorithm/Baekjoon] 1, 2, 3 ๋ํ๊ธฐ 4 - 15989 (S1/JAVA) (0) | 2023.07.19 |
[Algorithm/Baekjoon] ๋ฑ์ ๊ตฌํ๊ธฐ - 1205 (S4/JAVA) (0) | 2023.07.18 |
[Algorithm/Baekjoon] ๋ง๋ฒ์ฌ ์์ด์ ํ์ด์ด๋ณผ - 20056 (G4/JAVA) (1) | 2023.07.17 |