PAT甲级(Advanced Level) Practice 1012 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 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 CME 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 CM 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

题目翻译

现在,要给 CS 专业一年级的学生进行成绩评估。

我们只考虑他们的三门成绩:C–c语言,M–数学,E–英语,除此之外,我们还会考虑 A –三门成绩平均值。平均成绩为三科成绩平均值四舍五入取整的结果。

例如,四个学生的成绩单如下:

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

每个学生都会有各科排名以及平均成绩排名,我们通过强调学生的最佳成绩来鼓励学生。

因此,每个学生的成绩单上只会有四个成绩中排名最高的那个成绩的排名,以及具体是哪项成绩(C、M、E、A中的一项)。

例如,第一个学生的成绩单上会显示 1 C,因为他的C语言排名第 1,这是他的最佳名次。

而第四个学生的成绩单上会显示 1 A,因为他的平均成绩排名第 1,这是他的最佳名次。

输入格式

第一行包含两个整数 N,M(< 2000),分别表示学生总数和查询成绩单次数。

接下来 N 行,每行首先包含一个由六位数字组成的学生 ID,然后包含三个整数分别表示该学生的 C,M,E 三科成绩(在[0, 100]之间)。

接下来 M 行,每行包含一个学生 ID,表示要查询该学生的成绩单。

输出格式

对于每个学生,输出他的最佳排名以及该排名对应的是哪项成绩。

当多项排名相同,且都为最佳时,按照 A>C>M>E 的优先级,选择输出哪项成绩。

如果无法查询到该学生的成绩,则输出 N/A

题目解析

这道题第一眼让我想到了excel的rank()函数,可惜c++没有。具体思路是先将c,m,e,a 分别存入数组中并且降序排序,查找时用二分找到每门课分数的排名(可能可以暴力,应该不会超时),在比较出排名最小的学科。可以用哈希表来存储ID,便于后续查找。

代码(c++)

#include <bits/stdc++.h>
#include <unordered_map>

using namespace std;

const int N = 2010;

int n, k;
int c[N], m[N], e[N], a[N], cpyc[N], cpym[N],cpye[N], cpya[N];
string id[N];

bool compareDescending(int a, int b) {                      // 降序排序
    return a > b; 
} 

int binarySearch(int arr[], int x){
    int l = 0, r = n - 1;
    while(l < r){
        int mid = (l + r) >> 1;
        if(arr[mid] <= x) r = mid;
        else l = mid + 1;
    }
    return l + 1;                                           // 一定能找到,不用判断。加1因为下标是从0开始而名次从1开始
}

int main(){
    cin >> n >> k;
    unordered_map<string, int> my_map;                      // 哈希表储存ID和对应下标
    
    for(int i = 0; i < n; i++) {
        cin >> id[i] >> c[i] >> m[i] >> e[i];
        a[i] = round((double)(c[i] + m[i] + e[i]) / 3);
        my_map[id[i]] = i + 1;                              // 加1使得所有存入ID的下标大于0
    }
    
    memcpy(cpyc, c, n * sizeof(int));
    memcpy(cpym, m, n * sizeof(int));
    memcpy(cpye, e, n * sizeof(int));
    memcpy(cpya, a, n * sizeof(int));
    sort(cpyc, cpyc + n, compareDescending);
    sort(cpym, cpym + n, compareDescending);
    sort(cpye, cpye + n, compareDescending);
    sort(cpya, cpya + n, compareDescending);
    
    while(k--){
        string testId;
        cin >> testId;
        int i = my_map[testId];
        if(i == 0) cout << "N/A" << endl;
        else{
            i--;
            int rankc = binarySearch(cpyc, c[i]);
            int rankm = binarySearch(cpym, m[i]);
            int ranke = binarySearch(cpye, e[i]);
            int ranka = binarySearch(cpya, a[i]);
            
            int rank1 = 101, rank2 = 101, rank = 101;
            char subject1, subject2, subject;
            if(ranka <= rankc) rank1 = ranka, subject1 = 'A';  // 找到名次最小的科目
            else rank1 = rankc, subject1 = 'C';
            
            if(rankm <= ranke) rank2 = rankm, subject2 = 'M';
            else rank2 = ranke, subject2 = 'E';
            
            if(rank1 <= rank2) rank = rank1, subject = subject1;
            else rank = rank2, subject = subject2;
            
            cout << rank << " " << subject << endl;
        }
    }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值