1012. The Best Rank (25)

来源:互联网 发布:阿里云app名师课堂在哪 编辑:程序博客网 时间:2024/06/10 08:28

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 6310101 98 85 88310102 70 95 88310103 82 87 94310104 91 91 91310105 85 90 90310101310102310103310104310105999999

Sample Output

1 C1 M1 E1 A3 AN/A

分析:
定义结构体

struct node {        int ID, score[4], rank[4];    };score分别用于记录A,C,M,E的分数      rank 分别用于记录A,C,M,E的名次  

在输上成绩时计算平均值,然后按照每个科目从小到大排名,另外还要定义一个m数组用于标记某个同学是否存在,m[ID]=1,我用的map方法,不知道为什么,我原先定义的m[1000001],根本就不能运行。
在查找时,先判断m【ID】是否等于1,不是输出N/A,是的话在通过ID找到元素,再寻找排名最小的。

最后在输出排序时永远不要忘记值相等的情况

代码

#include<iostream>#include<vector>#include<map>#include<algorithm>using namespace std;struct node {    int ID, score[4], rank[4];};int k;//用于标记要排哪一科的名次bool cmp1(node a, node b) {    return a.score[k] > b.score[k];}int main(){    int N, M, ID;    char C[5] = "ACME";    cin >> N >> M;    vector<node> v(N);    map<int, int> m;    for (int i = 0; i < N; i++) {        cin >> v[i].ID >> v[i].score[1] >> v[i].score[2] >> v[i].score[3];        v[i].score[0] = (v[i].score[1] + v[i].score[2] + v[i].score[3]) / 3;        m[v[i].ID] = 1;    }    for (k = 0; k < 4; k++) {        sort(v.begin(), v.end(), cmp1);        for (int i = 0; i < N; i++) {            if (i > 0 && v[i].score[k] == v[i - 1].score[k])                v[i].rank[k] = v[i - 1].rank[k];            else                v[i].rank[k] = i;        }    }    for (int i = 0; i < M; i++) {        cin >> ID;        if (m[ID]) {            int i = 0;            for (i = 0; i < N; i++)                if (v[i].ID == ID) break;            int best = 0;            for (int j = 1; j < 4; j++)                if (v[i].rank[best] > v[i].rank[j])best = j;            printf("%d %c\n", v[i].rank[best]+1, C[best]);        }        else {            printf("N/A\n");        }    }    return 0;}
原创粉丝点击