To evaluate the performance of our first year CS majored students, we consider their grades of three courses only: C
- C Programming Language, M
- Mathematics (Calculus or Linear Algrbra), and E
- English. At the mean time, we encourage students by emphasizing on their best ranks -- that is, among the four ranks with respect to the three courses and the average grade, we print the best rank for each student.
For example, The grades of C
, M
, E
and A
- Average of 4 students are given as the following:
为了评估CS专业一年级学生的成绩,我们只考虑他们三门课程的成绩:C-C程序设计语言、M-数学(微积分或线性代数)和E-英语。同时,我们通过强调学生的最佳排名来鼓励学生——也就是说,在三门课程和平均成绩的四个排名中,我们为每个学生打印最佳排名。
例如,C、M、E和A的成绩——4名学生的平均成绩如下:
StudentID C M E A
310101 98 85 88 90
310102 70 95 88 84
310103 82 87 94 88
310104 91 91 91 91
Then the best ranks for all the students are No.1 since the 1st one has done the best in C Programming Language, while the 2nd one in Mathematics, the 3rd one in English, and the last one in average.
然后所有学生的最佳排名都是第一,因为第一名在C编程语言方面表现最好,而第二名在数学方面,第三名在英语方面,平均排名最后。
Input Specification:
Each input file contains one test case. Each case starts with a line containing 2 numbers N and M (≤2000), which are the total number of students, and the number of students who would check their ranks, respectively. Then N lines follow, each contains a student ID which is a string of 6 digits, followed by the three integer grades (in the range of [0, 100]) of that student in the order of C
, M
and E
. Then there are M lines, each containing a student ID.
每个输入文件包含一个测试用例。每种情况都以一行开始,其中包含2个数字N和M(≤2000),这两个数字分别是学生总数和将检查其排名的学生人数。接下来是N行,每行包含一个学生ID,该ID是一个由6位数字组成的字符串,后面是该学生的三个整数等级(在[0100]的范围内),顺序为C、M和E。然后是M行,每行包含一个学生ID。
Output Specification:
For each of the M students, print in one line the best rank for him/her, and the symbol of the corresponding rank, separated by a space.
The priorities of the ranking methods are ordered as A
> C
> M
> E
. Hence if there are two or more ways for a student to obtain the same best rank, output the one with the highest priority.
If a student is not on the grading list, simply output N/A
.
对于M名学生中的每一位,在一行中打印出他/她的最佳排名和相应排名的符号,用空格隔开。
排序方法的优先级顺序为A>C>M>E。因此,如果一个学生有两种或多种方法可以获得相同的最佳排名,则输出优先级最高的方法。
如果学生不在评分表上,只需输出N/a即可。
Sample Input:
5 6
310101 98 85 88
310102 70 95 88
310103 82 87 94
310104 91 91 91
310105 85 90 90
310101
310102
310103
310104
310105
999999
Sample Output:
1 C
1 M
1 E
1 A
3 A
N/A
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StreamTokenizer;
import java.util.*;
class Student{
public int number; // 学号
public int[] grade; // 分数数组
public int[] rank; // 排名数组
public int best; // 最佳排名
}
public class Main {
static int flag = 0; // 分数数组排序列的索引,如flag = 0是对平均分进行排序
public static void main(String[] args) throws IOException {
StreamTokenizer sc = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
sc.nextToken();
int n = (int)sc.nval;
sc.nextToken();
int m = (int)sc.nval;
Student[] data = new Student[n];
int[] exist = new int[1000001]; // 映射数组,判断当前学号是否存在以及在Student数组中的位置
char[] subject = new char[]{'A', 'C', 'M', 'E'};
for (int i=0; i<n; ++i){
data[i] = new Student();
data[i].grade = new int[4];
data[i].rank = new int[4];
sc.nextToken();
data[i].number = (int)sc.nval;
sc.nextToken();
data[i].grade[1] = (int)sc.nval;
sc.nextToken();
data[i].grade[2] = (int)sc.nval;
sc.nextToken();
data[i].grade[3] = (int)sc.nval;
data[i].grade[0] = ( data[i].grade[1] + data[i].grade[2] + data[i].grade[3]) / 3;
}
for(int i=0; i<4; ++i){
flag = i;
Arrays.sort(data, new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
return o2.grade[flag] - o1.grade[flag] ;
}
});
// 平均分或学科分数的排名情况
data[0].rank[flag] = 1;
for (int j = 1; j < n; j++) {
if(data[j].grade[flag] == data[j-1].grade[flag]){
data[j].rank[flag] = data[j-1].rank[flag];
}else{
data[j].rank[flag] = j + 1;
}
}
}
// 获取每个人最优的排名情况
for (int i = 0; i < n; i++) {
int k = 0;
int min = data[i].rank[0];
exist[data[i].number] = i + 1;
for (int j = 1; j < 4; j++) {
if(min > data[i].rank[j]){
min = data[i].rank[j];
k = j;
}
}
data[i].best = k;
}
for (int i = 0; i < m; i++) {
sc.nextToken();
int id = (int)sc.nval;
flag = exist[id];
if(flag > 0){
System.out.println(data[flag-1].rank[data[flag-1].best] + " " + subject[data[flag-1].best]);
}else{
System.out.println("N/A");
}
}
}
}