[PAT 甲级]1012 The Best Rank (25 分)[多维度排序]

版权声明:原创博客,转载请标明出处! https://blog.csdn.net/caipengbenren/article/details/89210332

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 Algrbra), 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 Specification:
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 Specification:
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<iostream>
#include<vector>
#include<map>
#include<algorithm>
using namespace std;
struct node {
    vector<pair<string,int>>cp;
}subject[5];
struct node1 {
    map<string,int>cp;
}ranking[5];
char f[5] = {' ','A','C','M','E'};
bool cmp (pair<string,int>a,pair<string, int>b) {
    return a.second > b.second;
}
int main () {
    int n, m;
    cin >> n >> m;
    for (int i = 1; i <= n; i++) {
        string id;
        int sum = 0, k;
        cin >> id;
        for (int j = 2; j <= 4; j++) {
            cin >> k;
            sum += k;
            subject[j].cp.push_back(make_pair(id, k));
        }
        sum /= 3;
        subject[1].cp.push_back(make_pair(id, sum));
    }
    for (int i = 1; i <= 4; i++) {
        sort(subject[i].cp.begin(),subject[i].cp.end(),cmp);
        int len = int(subject[i].cp.size()), pro = 0;
        for (int j = 0; j < len; j++) {
            string id_ = subject[i].cp[j].first;
            if (j == 0) {
                ranking[i].cp[id_] = 1 + j;
                pro = j + 1;
            } else {
                if (subject[i].cp[j - 1].second == subject[i].cp[j].second) {
                    ranking[i].cp[id_] = pro;
                } else {
                    ranking[i].cp[id_] = j + 1;
                    pro = j + 1;
                }
            }
        }
    }
    string id_;
    for (int i = 1; i <= m; i++) {
        cin >> id_;
        if (ranking[1].cp.find(id_) == ranking[1].cp.end()) {
            cout << "N/A" << "\n";
        } else {
            char a1 = ' ';
            int a2 = 2001;
            for (int j = 1; j <= 4; j++) {
                if (ranking[j].cp[id_] < a2) {
                    a2 = ranking[j].cp[id_];
                    a1 = f[j];
                }
            }
            cout << a2 << " " << a1 << "\n";
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/caipengbenren/article/details/89210332