Java
import java.util.*;
public class Main {
static class Student implements Comparable<Student> {
private final String name;
private final String ID;
private final int grade;
public Student(String name, String ID, int grade) {
this.name = name;
this.ID = ID;
this.grade = grade;
}
@Override
public int compareTo(Student o) {
return o.grade - this.grade;
}
@Override
public String toString() {
return name + " " + ID;
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
ArrayList<Student> maleList = new ArrayList<>();
ArrayList<Student> femaleList = new ArrayList<>();
int n = scanner.nextInt();
for (int i = 0; i < n; i++) {
String name = scanner.next();
String gender = scanner.next();
String ID = scanner.next();
int grade = scanner.nextInt();
if (gender.equals("F")) {
femaleList.add(new Student(name, ID, grade));
} else if (gender.equals("M")) {
maleList.add(new Student(name, ID, grade));
}
}
Collections.sort(maleList);
Collections.sort(femaleList);
if (!maleList.isEmpty() && !femaleList.isEmpty()) {
System.out.println(femaleList.get(0));
System.out.println(maleList.get(maleList.size() - 1));
System.out.println(femaleList.get(0).grade - maleList.get(maleList.size() - 1).grade);
} else if (maleList.isEmpty() && !femaleList.isEmpty()) {
System.out.println(femaleList.get(0));
System.out.println("Absent");
System.out.println("NA");
} else if (!maleList.isEmpty()) {
System.out.println("Absent");
System.out.println(maleList.get(maleList.size() - 1));
System.out.println("NA");
} else {
System.out.println("Absent");
System.out.println("Absent");
System.out.println("NA");
}
}
}
C++
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int MAXN = 10010, INF = 1e7 + 10;
int n;
int boy_num = 0, girl_num = 0;
string first_girl, last_boy;
string first_g_id, last_b_id;
int first_g_score = -INF, last_b_score = INF;
int main() {
cin >> n;
string name, gender, id;
int score;
for (int i = 0; i < n; ++i) {
cin >> name >> gender >> id >> score;
if (gender == "M") {
boy_num++;
if (score < last_b_score) {
last_boy = name;
last_b_id = id;
last_b_score = score;
}
} else {
girl_num++;
if (score > first_g_score) {
first_girl = name;
first_g_id = id;
first_g_score = score;
}
}
}
if (girl_num == 0) printf("Absent\n");
else cout << first_girl << " " << first_g_id << endl;
if (boy_num == 0) printf("Absent\n");
else cout << last_boy << " " << last_b_id << endl;
if (girl_num == 0 || boy_num == 0) printf("NA\n");
else printf("%d", first_g_score - last_b_score);
return 0;
}