一、题目链接
http://noi.openjudge.cn/ch0110/08/
二、解题思路
三、实施步骤
四、Java程序
import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;
class Patient implements Comparable<Patient> {
int register;
String id;
int age;
public Patient(int register, String id, int age) {
this.register = register;
this.id = id;
this.age = age;
}
@Override
public int compareTo(Patient o) {
return o.age - this.age;
}
@Override
public String toString() {
return String.valueOf(id);
}
}
public class Main {
public void sortByIDASC(Patient[] patients, int from, int to) {
Arrays.sort(patients, from, to + 1, new Comparator<Patient>() {
@Override
public int compare(Patient o1, Patient o2) {
return o1.register - o2.register;
}
});
}
public void sortByAgeDESC(Patient[] patients, int from, int to) {
Arrays.sort(patients, from, to + 1, new Comparator<Patient>() {
@Override
public int compare(Patient o1, Patient o2) {
if (o1.age == o2.age) {
return o1.register - o2.register;
}
return o2.age - o1.age;
}
});
}
public void sortPatients(Patient[] patients) {
Arrays.sort(patients);
int n = patients.length;
int to = 0;
while (to < n && patients[to].age >= 60) {
to++;
}
sortByAgeDESC(patients, 0, to - 1);
sortByIDASC(patients, to, n - 1);
}
public static void main(String[] args) {
Main test = new Main();
Scanner input = new Scanner(System.in);
int n = input.nextInt();
Patient[] patients = new Patient[n];
int register = 1;
for (int i = 0; i < n; i++) {
String id = input.next();
int age = input.nextInt();
patients[i] = new Patient(register, id, age);
register++;
}
test.sortPatients(patients);
for (Patient p : patients) {
System.out.println(p.toString());
}
}
}