A1012. The Best Rank

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 #include<cstdio>
  2 #include<iostream>
  3 #include<algorithm>
  4 #include<string.h>
  5 using namespace std;
  6 typedef struct{
  7     int A;
  8     int C;
  9     int M;
 10     int E;
 11     char id[7];
 12     char best;
 13     int bestRk, thisRk;
 14 }student;
 15 bool cmp1(student a, student b){
 16     return a.A > b.A;
 17 }
 18 bool cmp2(student a, student b){
 19     return a.C > b.C;
 20 }
 21 bool cmp3(student a, student b){
 22     return a.M > b.M;
 23 }
 24 bool cmp4(student a, student b){
 25     return a.E > b.E;
 26 }
 27 int main(){
 28     int N, M, a, c, m ,e;
 29     student info[2001];
 30     char search[7];
 31     scanf("%d%d", &N, &M);
 32     for(int i = 0; i < N; i++){
 33         scanf("%s%d%d%d", info[i].id, &info[i].C, &info[i].M, &info[i].E);
 34         info[i].A = (info[i].C + info[i].E + info[i].M) / 3;
 35     }
 36     sort(info, info + N, cmp4);
 37     info[0].bestRk = 1;
 38     info[0].thisRk = 1;
 39     info[0].best = 'E';
 40     for(int i = 1; i < N; i++){
 41         if(info[i].E == info[i - 1].E){
 42             info[i].bestRk = info[i - 1].bestRk;
 43             info[i].thisRk = info[i - 1].thisRk;
 44         }else{
 45             info[i].bestRk = i + 1;
 46             info[i].thisRk = i + 1;
 47         }
 48         info[i].best = 'E';
 49     }
 50     sort(info, info + N, cmp3);    
 51     info[0].thisRk = 1;
 52     if(info[0].thisRk <= info[0].bestRk){
 53         info[0].best = 'M';
 54         info[0].bestRk = 1;
 55     }
 56     for(int i = 1; i < N; i++){
 57         if(info[i].M == info[i - 1].M)
 58             info[i].thisRk = info[i - 1].thisRk;
 59         else
 60             info[i].thisRk = i + 1;
 61         if(info[i].thisRk <= info[i].bestRk){
 62             info[i].best = 'M';
 63             info[i].bestRk = info[i].thisRk;
 64         }
 65     }
 66     sort(info, info + N, cmp2);    
 67     info[0].thisRk = 1;
 68     if(info[0].thisRk <= info[0].bestRk){
 69         info[0].best = 'C';
 70         info[0].bestRk = 1;
 71     }
 72     for(int i = 1; i < N; i++){
 73         if(info[i].C == info[i - 1].C)
 74             info[i].thisRk = info[i - 1].thisRk;
 75         else
 76             info[i].thisRk = i + 1;
 77         if(info[i].thisRk <= info[i].bestRk){
 78             info[i].best = 'C';
 79             info[i].bestRk = info[i].thisRk;
 80         }
 81     }
 82 
 83     sort(info, info + N, cmp1);    
 84     info[0].thisRk = 1;
 85     if(info[0].thisRk <= info[0].bestRk){
 86         info[0].best = 'A';
 87         info[0].bestRk = 1;
 88     }
 89     for(int i = 1; i < N; i++){
 90         if(info[i].A == info[i - 1].A)
 91             info[i].thisRk = info[i - 1].thisRk;
 92         else
 93             info[i].thisRk = i + 1;
 94         if(info[i].thisRk <= info[i].bestRk){
 95             info[i].best = 'A';
 96             info[i].bestRk = info[i].thisRk;
 97         }
 98     }
 99     for(int i = 0; i < M; i++){
100         scanf("%s", search);
101         int index = -1;
102         for(int j = 0; j < N; j++){
103             if(strcmp(info[j].id, search) == 0){
104                 index = j;
105                 break;
106             }
107         }
108         if(index == -1)
109             printf("N/A\n");
110         else
111             printf("%d %c\n", info[index].bestRk, info[index].best);
112     }
113     cin >> M;
114     return 0;
115 }
View Code

总结:

1、排名依旧采用分数相同就名次相同,但需要占位的情况。对于题中要求的A、C、M、E的优先级,可以使优先级低的先比较。

posted @ 2018-01-22 15:46  ZHUQW  阅读(127)  评论(0编辑  收藏  举报