백준 9017 (크로스 컨트리) - Java
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int testCase = Integer.parseInt(br.readLine());
for (int i = 0; i < testCase; i++) {
int dataSize = Integer.parseInt(br.readLine());
int[] input= new int[dataSize];
int[] count = new int[201];
StringTokenizer st = new StringTokenizer(br.readLine());
for (int j = 0; j < dataSize ; j++) {
input[j] = Integer.parseInt(st.nextToken());
count[input[j]]++;
}
ArrayList<Integer>[] arr = new ArrayList[201];
for (int j = 1; j < 201; j++) {
arr[j] = new ArrayList<Integer>();
}
int rank = 1;
for (int j = 0; j < dataSize ; j++) {
// 해당 팀의 사람 수가 6명이 안되면 할당하지 않는다.
if(count[input[j]]<6) continue;
arr[input[j]].add(rank++);
}
int min = Integer.MAX_VALUE;
int idx = 1;
for (int j = 1; j < 201; j++) {
if (arr[j].size() >= 6) {
int sum = 0;
// 4번째 인덱스까지 합
for (int k=0;k<4;k++) {
sum += arr[j].get(k);
}
if (min > sum) {
min = sum;
idx = j;
}
else if (min == sum) {
// 5번째 인덱스까지 합 비교
if (arr[idx].get(arr[idx].size() - 2) > arr[j].get(arr[j].size() - 2)) {
idx = j;
}
}
}
}
bw.write(idx + "\n");
}
bw.flush();
bw.close();
br.close();
}
}