1 题目
2 代码
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
public class A1025 {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(br.readLine());
ArrayList<PATStudent> totalStudent = new ArrayList<>();
for (int i = 0; i < N; i++) {
int K = Integer.parseInt(br.readLine());
ArrayList<PATStudent> localStudents = new ArrayList<>();
for (int j = 0; j < K; j++) {
String[] input = br.readLine().split(" ");
localStudents.add(new PATStudent(input[0], Integer.parseInt(input[1]), (i + 1)));
}
Collections.sort(localStudents, new Comparator<PATStudent>() {
@Override
public int compare(PATStudent o1, PATStudent o2) {
return o2.getScore() - o1.getScore();
}
});
for (int j = 0; j < localStudents.size(); j++) {
if (j == 0)
localStudents.get(0).setLocal_rank((1));
else {
if (localStudents.get(j).getScore() == localStudents.get(j - 1).getScore())
localStudents.get(j).setLocal_rank(localStudents.get(j - 1).getLocal_rank());
else
localStudents.get(j).setLocal_rank(j + 1);
}
}
totalStudent.addAll(localStudents);
}
Collections.sort(totalStudent, new Comparator<PATStudent>() {
@Override
public int compare(PATStudent o1, PATStudent o2) {
if (o2.getScore() != o1.getScore())
return o2.getScore() - o1.getScore();
else
return o1.getRegistration_number().compareTo(o2.getRegistration_number());
}
});
for (int i = 0; i < totalStudent.size(); i++) {
if (i == 0)
totalStudent.get(0).setFinal_rank(1);
else {
if (totalStudent.get(i).getScore() == totalStudent.get(i - 1).getScore())
totalStudent.get(i).setFinal_rank(totalStudent.get(i - 1).getFinal_rank());
else
totalStudent.get(i).setFinal_rank(i + 1);
}
}
System.out.println(totalStudent.size());
for (PATStudent s : totalStudent) {
System.out.println(s.getRegistration_number() + " " + s.getFinal_rank() + " " + s.getLocation_number() + " " + s.getLocal_rank());
}
}
}
class PATStudent {
private String registration_number;
private int score;
private int final_rank;
private int location_number;
private int local_rank;
public PATStudent(String registration_number, int score, int location_number) {
this.registration_number = registration_number;
this.score = score;
this.location_number = location_number;
}
public String getRegistration_number() {
return registration_number;
}
public void setRegistration_number(String registration_number) {
this.registration_number = registration_number;
}
public int getFinal_rank() {
return final_rank;
}
public void setFinal_rank(int final_rank) {
this.final_rank = final_rank;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
public int getLocation_number() {
return location_number;
}
public void setLocation_number(int location_number) {
this.location_number = location_number;
}
public int getLocal_rank() {
return local_rank;
}
public void setLocal_rank(int local_rank) {
this.local_rank = local_rank;
}
}
3 要点
(1)思路:遍历输入N个地区的考生信息,每个地区的考生信息分别用List记录其id、score、地区编号,并对List排序,最后保存到TotalList中去。最后对TotalList进行排序即可,注意同分数的考生排名相同,并且按字符串升序排列,按规则输出即可。
(2)用Java还是超时了。