1012 The Best Rank
分数 25
作者 CHEN, Yue
单位 浙江大学
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:
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.
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.
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
.
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
* 对n名同学按成绩平均数,C语言,数学,英语成绩优先级逐级递减,分别按成绩从高到低
* 进行排列,成绩相同者排名相同,平均成绩按四舍五入处理;
* 先用一个结构体存储学生的名字,各科成绩,后面分别按各优先级处理各科成绩,以及各科
* 成绩的排名;
*
* 最后用一个unordered_map 来存储每个学生的姓名,以及他最好的科目的成绩排名和最好的科目
* 的成绩排名下标;
* 最后输入m个学生处理,输出即可。
*
* 具体操作参见代码
/**
* 对n名同学按成绩平均数,C语言,数学,英语成绩优先级逐级递减,分别按成绩从高到低
* 进行排列,成绩相同者排名相同,平均成绩按四舍五入处理;
* 先用一个结构体存储学生的名字,各科成绩,后面分别按各优先级处理各科成绩,以及各科
* 成绩的排名;
*
* 最后用一个unordered_map 来存储每个学生的姓名,以及他最好的科目的成绩排名和最好的科目
* 的成绩排名下标;
* 最后输入m个学生处理,输出即可。
*
* 具体操作参见代码
*/
#include <iostream>
#include <string>
#include <algorithm>
#include <unordered_map>
using namespace std;
typedef pair<int, int> PII;
struct Grade
{
string name;
int soc[4];
int rank[4];
};
const int N = 1e4+10;
struct Grade stu[N];
unordered_map<string, PII> ump;
int n, m;
int flag = 0;
//用一个flag变量对四个成绩进行排名,后面用一个for循环处理,真的很高明啊;
//我最开始用了四个for循环,眼睛都看痛了,又学到一招
bool cmp(Grade a, Grade b)
{
return a.soc[flag] > b.soc[flag];
}
int main()
{
cin >> n >> m;
for(int i=0; i<n; ++i)
{
int a[4];
double db = 0;
cin >> stu[i].name;
for(int j=1; j<4; ++j)
{
cin >> stu[i].soc[j];
db += stu[i].soc[j];
}
db /= 3;
db += 0.5; //平均数四舍五入
stu[i].soc[0] = db; //soc[0]存储平均数
}
//对A,C,M,E 按成绩进行排序,从高到低排名,成绩相等并列排名
for(flag = 0; flag < 4; ++flag)
{
sort(stu, stu + n, cmp);
int cur = 1;
for(int i=0; i<n; ++i)
{
if(i == 0 || stu[i].soc[flag] != stu[i-1].soc[flag])
stu[i].rank[flag] = cur;
else
stu[i].rank[flag] = stu[i-1].rank[flag];
++cur;
}
}
for(int i=0; i<n; ++i)
{
int maxv = N, idx = 0;
for(int j=0; j<4; ++j)
{
if(stu[i].rank[j] < maxv)
{
maxv = stu[i].rank[j];
idx = j;
}
}
ump[stu[i].name] = {idx, maxv};
}
string str[] = {"A", "C", "M", "E"};
while(m--)
{
string query;
cin >> query;
if(ump.find(query) == ump.end())
puts("N/A");
else
{
auto t = ump[query];
cout << t.second << ' ' << str[t.first] << endl;
}
}
return 0;
}