AcWing 1484. 最佳排名
原题链接
中等
作者:
sy123
,
2020-12-06 12:29:27
,
所有人可见
,
阅读 424
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <set>
#include <unordered_map>
using namespace std;
struct node {
string id;
int best;
int score[4], rank[4];
}stu[2005];
int flag = -1;
set<string> exist;
bool cmp1(node a, node b) {return a.score[flag] > b.score[flag];}//一共排序四次
int main() {
int n, m;
scanf("%d %d", &n, &m);
char id[8];
for(int i = 0; i < n; i++) {
scanf("%s %d %d %d", id, &stu[i].score[1], &stu[i].score[2], &stu[i].score[3]);
stu[i].id=id;
stu[i].score[0] = (stu[i].score[1] + stu[i].score[2] + stu[i].score[3]) / 3.0 + 0.5;
exist.insert(stu[i].id);//id存进去
}
for(flag = 0; flag <= 3; flag++) {
sort(stu, stu + n, cmp1);
stu[0].rank[flag] = 1;
for(int i = 1; i < n; i++) {
stu[i].rank[flag] = i + 1;
if(stu[i].score[flag] == stu[i-1].score[flag])
stu[i].rank[flag] = stu[i-1].rank[flag];
}
}
unordered_map<string, int>map;
for(int i = 0; i < n; i++) { //遍历每个人的最小排名
map[stu[i].id]=i;//存储四次排序后位置固定的下标
stu[i].best = 0;
int minn = stu[i].rank[0];
for(int j = 1; j <= 3; j++) {
if(stu[i].rank[j] < minn) {
minn = stu[i].rank[j];
stu[i].best = j; //待会转成字符串就行
}
}
}
char c[5] = {'A', 'C', 'M', 'E'};
for(int i = 0; i < m; i++) {
string a;
cin>>a;
int temp=map[a];
if(exist.find(a)!=exist.end()) { //可以找到
int best = stu[temp].best;
printf("%d %c\n", stu[temp].rank[best], c[best]);
} else {
printf("N/A\n");
}
}
return 0;
}