# PAT 甲级 1012（上）

1012 The Best Rank (25)（25 分）

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 Algebra), 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

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

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

1.使用数组代替字母存储学生各项成绩，规定特定下标代表特定项成绩，并用一个char数组来储存他们的symbol

2.使用一个标志位来记录当前进行比较的单项成绩代表的下标，这样就可以将四个比较函数合为一个，大大减少冗余代码

3.又因为学生的id只包含6位数字，所以我们可以使用一个长7位的包含一个4个int值的数组的数组，也就是 int example[1000000][4]，来做一个伪map，存储对应学生id的4项成绩排名

4.最后使用伪map的方法快速查找需要查找最好成绩的学生，并打印

AC代码：

#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>

using namespace std;

struct Student {
int id;
}s[2010];

char course[4] = {'A', 'C', 'M', 'E'};
int rankMe[1000000][4];
int now;

bool cmp(Student, Student);

int main(void) {
int n, m;
int sortArray[2010];
memset(rankMe, 0, sizeof(rankMe));

scanf("%d%d", &n, &m);

for(int i = 0; i < n; ++i) {
}

for(int i = 0; i < m; ++i) {
scanf("%d", &sortArray[i]);
}

for(int i = 0; i < 4; ++i) {
now = i;
sort(s, s + n, cmp);

int r = 0;
for(int j = 0; j < n; ++j) {
r = j;
}

int id = s[j].id;
rankMe[id][i] = r + 1;
}
}

for(int i = 0; i < m; ++i) {
if(rankMe[sortArray[i]][0] != 0) {
int max = 0;
for(int j = 1; j < 4; ++j) {
if(rankMe[sortArray[i]][max] > rankMe[sortArray[i]][j]) {
max = j;
}
}

printf("%d %c\n", rankMe[sortArray[i]][max], course[max]);
} else {
printf("N/A\n");
}
}

return 0;
}

bool cmp(Student s1, Student s2) {
}

#include <cstdio>
#include <cmath>
#include <algorithm>

using namespace std;

struct Student {
int id;
int c;
int m;
int e;
int a;
int rankMe;
char course;
} s[2010];

bool cmpid(Student s1, Student s2);
bool cmpa(Student s1, Student s2);
bool cmpc(Student s1, Student s2);
bool cmpm(Student s1, Student s2);
bool cmpe(Student s1, Student s2);
int binarySort(Student *, int, int, int);

int main(void) {
int n, m, r;
int findArray[2010];

scanf("%d%d", &n, &m);
for(int i = 0; i < n; ++i) {
scanf("%d%d%d%d", &(s[i].id), &(s[i].c), &(s[i].m), &(s[i].e));
s[i].a = (int)round(s[i].c + s[i].m + s[i].e);
}
for(int i = 0; i < m; ++i) {
scanf("%d", &findArray[i]);
}

sort(s, s + n, cmpa);

r = 0;
for(int i = 0; i < n; ++i) {
if(i != 0 && s[i - 1].a > s[i].a) {
r = i;
}

s[i].rankMe = r + 1;
s[i].course = 'A';
}

sort(s, s + n, cmpc);

r = 0;
for(int i = 0; i < n; ++i) {
if(i != 0 && s[i - 1].c > s[i].c) {
r = i;
}

if(s[i].rankMe > r + 1) {
s[i].rankMe = r + 1;
s[i].course = 'C';
}
}

sort(s, s + n, cmpm);

r = 0;
for(int i = 0; i < n; ++i) {
if(i != 0 && s[i - 1].m > s[i].m) {
r = i;
}

if(s[i].rankMe > r + 1) {
s[i].rankMe = r + 1;
s[i].course = 'M';
}
}

sort(s, s + n, cmpe);

r = 0;
for(int i = 0; i < n; ++i) {
if(i != 0 && s[i - 1].e > s[i].e) {
r = i;
}

if(s[i].rankMe > r + 1) {
s[i].rankMe = r + 1;
s[i].course = 'E';
}
}

sort(s, s + n, cmpid);

for(int i = 0; i < m; ++i) {
int pos = binarySort(s, 0, n - 1, findArray[i]);
if(pos != -1) {
printf("%d %c\n", s[pos].rankMe, s[pos].course);
} else {
printf("N/A\n");
}
}

return 0;
}

bool cmpid(Student s1, Student s2) {
return s1.id < s2.id;
}

bool cmpc(Student s1, Student s2) {
return s1.c > s2.c;
}

bool cmpm(Student s1, Student s2) {
return s1.m > s2.m;
}

bool cmpe(Student s1, Student s2) {
return s1.e > s2.e;
}

bool cmpa(Student s1, Student s2) {
return s1.a > s2.a;
}

int binarySort(Student s[], int start, int end, int tarId) {
int mid = (start + end) / 2;

if(start == end && s[start].id != tarId) {
return -1;
}

if(s[mid].id == tarId) {
return mid;
} else if(s[mid].id > tarId) {
return binarySort(s, start, mid - 1, tarId);
} else {
return binarySort(s, mid + 1, end, tarId);
}
}