﻿PAT(A) 1012. The Best Rank (25)-Ocrosoft

PAT(A) 1012. The Best Rank (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
```

```#include <set>
#include <map>
#include <list>
#include <cmath>
#include <stack>
#include <queue>
#include <ctime>
#include <string>
#include <cstdio>
#include <vector>
#include <cctype>
#include <climits>
#include <sstream>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <functional>
#define strend string::npos
#define ms(a) memset(a,0,sizeof(a))
#define  rep(a,v,b) for(int a=v;a<b;a++)
#define  repe(a,v,b) for(int a=v;a<=b;a++)
typedef long long LL;
const LL LINF = LLONG_MAX / 2;
const int INF = INT_MAX / 2;
const int MAXN = 2000 + 10;
const int MOD = 1000000009;
LL GCD(LL a, LL b)
{
if (!b)return a;
return GCD(b, a%b);
}
/*(◕‿‿◕) (◕‿‿◕) (◕‿‿◕) (◕‿‿◕) (◕‿‿◕)*/
/*(◕‿‿◕) 签订契约，成为马猴烧酒吧! (◕‿‿◕)*/
/*(◕‿‿◕) (◕‿‿◕) (◕‿‿◕) (◕‿‿◕) (◕‿‿◕)*/
using namespace std;
struct Node
{
string no;
int a, c, m, e;
}v[MAXN];
int n, m;
map<string, bool> mp;
int find(string s, int f)
{
for (int i = 0; i < n; i++)
{
if (s == v[i].no)
{
for (int j = i - 1; j >= 0; j--)
{
if (f == 1 && v[j].a != v[i].a)return j + 2;
if (f == 2 && v[j].c != v[i].c)return j + 2;
if (f == 3 && v[j].m != v[i].m)return j + 2;
if (f == 4 && v[j].e != v[i].e)return j + 2;
if (!j)return 1;
}
return i + 1;
}
}
return -1;
}
int main()
{
cin >> n >> m;
for (int i = 0; i < n; i++) { cin >> v[i].no; scanf("%d%d%d", &v[i].c, &v[i].m, &v[i].e); v[i].a = (v[i].c + v[i].m + v[i].e) / 3; mp[v[i].no] = true; }
while (m--)
{
string s; cin >> s;
if (!mp.count(s))printf("N/A\n");
else
{
int a, c, m, e;
sort(v, v + n, [](Node p, Node q) { return p.a > q.a; });
a = find(s, 1);
sort(v, v + n, [](Node p, Node q) { return p.c > q.c; });
c = find(s, 2);
sort(v, v + n, [](Node p, Node q) { return p.m > q.m; });
m = find(s, 3);
sort(v, v + n, [](Node p, Node q) { return p.e > q.e; });
e = find(s, 4);
int minn = min(a, min(c, min(m, e)));
if (minn == a)printf("%d A\n", a);
else if (minn == c)printf("%d C\n", c);
else if (minn == m)printf("%d M\n", m);
else printf("%d E\n", e);
}
}
return 0;
}```