|
| 1 | +import java.io.*; |
| 2 | +import java.util.*; |
| 3 | + |
| 4 | +public class Main { |
| 5 | + |
| 6 | + static int T, n; |
| 7 | + static int[] arr; |
| 8 | + static Map<Integer, Integer> teams; |
| 9 | + static Map<Integer, Team> gameInfo; |
| 10 | + static StringTokenizer st; |
| 11 | + static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); |
| 12 | + static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 13 | + |
| 14 | + public static void main(String[] args) throws Exception { |
| 15 | + T = Integer.parseInt(br.readLine()); |
| 16 | + |
| 17 | + for (int t = 0; t < T; t++) { |
| 18 | + preSetting(); |
| 19 | + teams = findCount(); |
| 20 | + gameInfo = calTeamScore(); |
| 21 | + |
| 22 | + ArrayList<Map.Entry<Integer, Team>> list = new ArrayList<>(gameInfo.entrySet()); |
| 23 | + |
| 24 | + list.sort((e1, e2) ->{ |
| 25 | + Team o1 = e1.getValue(); |
| 26 | + Team o2 = e2.getValue(); |
| 27 | + |
| 28 | + if(o1.score == o2.score){ |
| 29 | + return o1.fiveScore - o2.fiveScore; |
| 30 | + } |
| 31 | + return o1.score - o2.score; |
| 32 | + }); |
| 33 | + |
| 34 | + bw.append(String.valueOf(list.get(0).getKey())).append("\n"); |
| 35 | + } |
| 36 | + bw.close(); |
| 37 | + } |
| 38 | + |
| 39 | + static Map<Integer, Team> calTeamScore(){ |
| 40 | + Map<Integer, Team> answer = new HashMap<>(); |
| 41 | + |
| 42 | + int s = 0; |
| 43 | + int team; |
| 44 | + Team info; |
| 45 | + for(int i = 0; i < n ; i++){ |
| 46 | + if(!teams.containsKey(arr[i])) continue; |
| 47 | + s++; |
| 48 | + |
| 49 | + team = arr[i]; |
| 50 | + if(answer.containsKey(team)){ |
| 51 | + info = answer.get(team); |
| 52 | + |
| 53 | + if(info.cnt < 4) info.score += s; |
| 54 | + info.cnt++; |
| 55 | + |
| 56 | + if(info.cnt == 5) info.fiveScore = s; |
| 57 | + |
| 58 | + answer.put(team, info); |
| 59 | + } else{ |
| 60 | + answer.put(team, new Team(s)); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + return answer; |
| 65 | + } |
| 66 | + |
| 67 | + static Map<Integer, Integer> findCount(){ |
| 68 | + Map<Integer, Integer> answer = new HashMap<>(); |
| 69 | + |
| 70 | + int count; |
| 71 | + for(int i = 0; i < n ; i++){ |
| 72 | + if(answer.containsKey(arr[i])){ |
| 73 | + count = answer.get(arr[i]); |
| 74 | + |
| 75 | + answer.put(arr[i], count + 1); |
| 76 | + } else answer.put(arr[i], 1); |
| 77 | + } |
| 78 | + |
| 79 | + answer.values().removeIf(cnt -> cnt < 6); |
| 80 | + |
| 81 | + return answer; |
| 82 | + } |
| 83 | + |
| 84 | + static void preSetting() throws Exception{ |
| 85 | + n = Integer.parseInt(br.readLine()); |
| 86 | + arr = new int[n]; |
| 87 | + |
| 88 | + st = new StringTokenizer(br.readLine()); |
| 89 | + for(int i = 0; i < n ; i++) arr[i] = Integer.parseInt(st.nextToken()); |
| 90 | + } |
| 91 | + |
| 92 | + static class Team { |
| 93 | + int fiveScore, score, cnt; |
| 94 | + Team(int score){ |
| 95 | + this.score = score; |
| 96 | + this.cnt = 1; |
| 97 | + } |
| 98 | + } |
| 99 | +} |
0 commit comments