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
N
N and
M
(
≤
2000
)
M (≤2000)
M(≤2000), which are the total number of students, and the number of students who would check their ranks, respectively. Then
N
N
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
M
M lines, each containing a student ID.
Output Specification:
For each of the M M 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
Smaple Output:
1 C
1 M
1 E
1 A
3 A
N/A
Solution:
// Talk is cheap, show me the code
// Created by Misdirection 2021-08-11 13:37:18
// All rights reserved.
#include <iostream>
#include <vector>
#include <unordered_map>
#include <algorithm>
#include <cmath>
using namespace std;
bool cmp(pair<string, int>& x, pair<string, int>& y){
return x.second > y.second;
}
int main(){
int n, m;
scanf("%d %d", &n, &m);
unordered_map<string, vector<int>> stu;
// stu vector<int> 里的含义:
// [0] 表示 c 成绩
// [1] 表示 m 成绩
// [2] 表示 e 成绩
// [3] 表示 a 成绩
// [4] 表示 最好的排名
// [5] 表示最好排名对应的类别(0 表示 a,1 表示 c,2 表示 m,3 表示 e)
vector<pair<string, int>> cRank, mRank, eRank, aRank;
for(int i = 0; i < n; ++i){
string tmp;
int c, m, e, a;
cin >> tmp;
cin >> c >> m >> e;
a = round((c + m + e) / 3.0);
stu[tmp].push_back(c);
stu[tmp].push_back(m);
stu[tmp].push_back(e);
stu[tmp].push_back(a);
cRank.emplace_back(tmp, c);
mRank.emplace_back(tmp, m);
eRank.emplace_back(tmp, e);
aRank.emplace_back(tmp, a);
}
// 接下来要对四个 rank 排序,每对一个 rank 排好序就可以更新一下 stu 里的排名和对应的科目
sort(aRank.begin(), aRank.end(), cmp);
int rank = 1;
stu[aRank[0].first].push_back(1); // 表示最好的排名是 1
stu[aRank[0].first].push_back(0); // 表示最好排名对应的类别是 a
for(int i = 1; i < n; ++i){
if(aRank[i - 1].second > aRank[i].second) rank = i + 1;
stu[aRank[i].first].push_back(rank);
stu[aRank[i].first].push_back(0);
}
sort(cRank.begin(), cRank.end(), cmp);
rank = 1;
if(stu[cRank[0].first][4] > 1){
stu[cRank[0].first][4] = 1;
stu[cRank[0].first][5] = 1; // 表示最好排名对应的类别是 c
}
for(int i = 1; i < n; ++i){
if(cRank[i - 1].second > cRank[i].second) rank = i + 1;
if(stu[cRank[i].first][4] > rank){
stu[cRank[i].first][4] = rank;
stu[cRank[i].first][5] = 1; // 表示最好排名对应的类别是 c
}
}
sort(mRank.begin(), mRank.end(), cmp);
rank = 1;
if(stu[mRank[0].first][4] > 1){
stu[mRank[0].first][4] = 1;
stu[mRank[0].first][5] = 2; // 表示最好排名对应的类别是 m
}
for(int i = 1; i < n; ++i){
if(mRank[i - 1].second > mRank[i].second) rank = i + 1;
if(stu[mRank[i].first][4] > rank){
stu[mRank[i].first][4] = rank;
stu[mRank[i].first][5] = 2; // 表示最好排名对应的类别是 m
}
}
sort(eRank.begin(), eRank.end(), cmp);
rank = 1;
if(stu[eRank[0].first][4] > 1){
stu[eRank[0].first][4] = 1;
stu[eRank[0].first][5] = 3; // 表示最好排名对应的类别是 e
}
for(int i = 1; i < n; ++i){
if(eRank[i - 1].second > eRank[i].second) rank = i + 1;
if(stu[eRank[i].first][4] > rank){
stu[eRank[i].first][4] = rank;
stu[eRank[i].first][5] = 3; // 表示最好排名对应的类别是 e
}
}
for(int i = 0; i < m; ++i){
string check;
cin >> check;
if(stu.find(check) == stu.end()) printf("N/A\n");
else{
printf("%d ", stu[check][4]);
switch(stu[check][5]){
case 0: printf("A\n"); break;
case 1: printf("C\n"); break;
case 2: printf("M\n"); break;
case 3: printf("E\n"); break;
default: break;
}
}
}
return 0;
}