PAT 1025,需要注意的一个坑是当分数相同的时候,按照id从小到大排序
package pat.pat_1025; // 坑,当分数相同的时候按照id顺序排序 import java.util.*; import java.io.*; class FastReader{ BufferedReader reader; StringTokenizer tokenizer; public FastReader(InputStream stream){ reader = new BufferedReader(new InputStreamReader(stream)); tokenizer = null; } public String next(){ while (tokenizer == null || !tokenizer.hasMoreTokens()){ try{ tokenizer = new StringTokenizer(reader.readLine()); } catch (Exception e){ throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int next_int(){ return Integer.parseInt(next()); } } class Testee{ String id; int score; int location_num; int location_rank; int final_rank; } public class Main { public static void main(String[] args){ FastReader reader = new FastReader(System.in); int N = reader.next_int(); ArrayList<Testee> global_testee = new ArrayList<Testee>(); for (int i = 0; i < N; i++){ int K = reader.next_int(); ArrayList<Testee> local_testee = new ArrayList<Testee>(); for (int j = 0; j < K; j++){ Testee testee = new Testee(); testee.id = reader.next(); testee.score = reader.next_int(); testee.location_num = i + 1; local_testee.add(testee); global_testee.add(testee); } Collections.sort(local_testee, new Comparator<Testee>(){ public int compare(Testee t1, Testee t2) { if (t2.score != t1.score) return t2.score - t1.score; else return t1.id.compareTo(t2.id); } }); int cur_rank = 1; local_testee.get(0).location_rank = cur_rank; for (int k = 1; k < local_testee.size(); k++){ cur_rank++; if (local_testee.get(k).score == local_testee.get(k - 1).score) local_testee.get(k).location_rank = local_testee.get(k - 1).location_rank; else local_testee.get(k).location_rank = cur_rank; } } Collections.sort(global_testee, new Comparator<Testee>(){ public int compare(Testee t1, Testee t2) { if (t2.score != t1.score) return t2.score - t1.score; else return t1.id.compareTo(t2.id); } }); int cur_rank = 1; global_testee.get(0).final_rank = cur_rank; for (int k = 1; k < global_testee.size(); k++){ cur_rank++; if (global_testee.get(k).score == global_testee.get(k - 1).score) global_testee.get(k).final_rank = global_testee.get(k - 1).final_rank; else global_testee.get(k).final_rank = cur_rank; } System.out.println(global_testee.size()); for (int i = 0; i < global_testee.size(); i++){ Testee cur_testee = global_testee.get(i); System.out.println(cur_testee.id + " " + cur_testee.final_rank + " " + cur_testee.location_num + " " + cur_testee.location_rank); } } }