甲级 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 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

 题目大意:

第一排给出学生总数-n,查询数-m

接下来n排给出每个学生的成绩,顺序是编号id,C语言成绩-C,数学成绩-M,英语成绩-E,剩余一项平均分需要根据三门学科成绩自己计算

我们需要根据分数,在每一门成绩里排名,要小心分数一样导致的并列排名,并记录下来

最后输出查询编号所对应的学生最好的排名以及这门学科名字

我的思路:

1、灵活运用map键值对,可用于筛除不存在的编号,数字对应的学科,还有快速查询编号对应的位置(在我的代码中分别对应existed,match,mp)

2、sort每排序一轮,记录一次学科排名,要注意处理并列排名

下面是我的代码~

#include <bits/stdc++.h>
using namespace std;
int n,m;
map<string, int> mp;            //用于查询编号对应的位置,方便使用位置调出其他数据
map<string, bool> existed;        //标记存在的编号,不存在的直接输出“N/A”
map<int, char> match;            //0123分别匹配"A","C","M","E",这样方便在最后比大小时直接改动,不需要再用多个if-else来达到学科变动
struct Stu {
    string id;
    int mark[4];        //结构体打包学生成绩,分数和排名用数组表示,方便遍历比大小
    int rank[4];        //0123分别对应"A","C","M","E",这个顺序由题目中所说的优先级决定
}stu[2020];

bool cmp0(Stu a, Stu b) {return a.mark[0] > b.mark[0];}        //4个排序函数
bool cmp1(Stu a, Stu b) {return a.mark[1] > b.mark[1];}
bool cmp2(Stu a, Stu b) {return a.mark[2] > b.mark[2];}
bool cmp3(Stu a, Stu b) {return a.mark[3] > b.mark[3];}

int main() {
    cin >> n >> m;
    for (int i = 0; i < n; i++) {
        cin >> stu[i].id >> stu[i].mark[1] >> stu[i].mark[2] >> stu[i].mark[3];
        stu[i].mark[0] = (stu[i].mark[1]+stu[i].mark[2]+stu[i].mark[3])/3;    //计算平均成绩
        existed[stu[i].id] = true;
    }
    
    sort(stu, stu+n, cmp0);
    for (int i = 0; i < n; i++) {
        if (i == 0 || stu[i].mark[0] != stu[i-1].mark[0]) stu[i].rank[0] = i+1;
        else stu[i].rank[0] = stu[i-1].rank[0];        //分两类,对于和前一个人的成绩相同的人要特殊处理
    }

    sort(stu, stu+n, cmp1);
    for (int i = 0; i < n; i++) {
        if (i == 0 || stu[i].mark[1] != stu[i-1].mark[1]) stu[i].rank[1] = i+1;
        else stu[i].rank[1] = stu[i-1].rank[1];
    }

    sort(stu, stu+n, cmp2);
    for (int i = 0; i < n; i++) {
        if (i == 0 || stu[i].mark[2] != stu[i-1].mark[2]) stu[i].rank[2] = i+1;
        else stu[i].rank[2] = stu[i-1].rank[2];
    }

    sort(stu, stu+n, cmp3);
    for (int i = 0; i < n; i++) {
        if (i == 0 || stu[i].mark[3] != stu[i-1].mark[3]) stu[i].rank[3] = i+1;
        else stu[i].rank[3] = stu[i-1].rank[3];
    }

    for (int i = 0; i < n; i++) {
        mp[stu[i].id] = i;
    }

    match[0] = 'A';    match[1] = 'C';
    match[2] = 'M';    match[3] = 'E';

    while(m--) {
        string temp;
        cin >> temp;
        if (existed[temp] == false) cout << "N/A" << endl;        //不存在直接输出"N/A"
        else {
            int pos = mp[temp];            //将编号temp信息直接转换成pos索引,后续要什么数据直接使用pos,不再需要temp了
            int ans_rank = 0x3f3f3f;        //这两排是初始化,不能忘
            char ans_subj = match[0];
            for (int i = 0; i < 4; i++) {
                if (stu[pos].rank[i] < ans_rank) {
                    ans_rank = stu[pos].rank[i];        //遇到更好的排名,直接更新ans
                    ans_subj = match[i];            //这边就是使用match更新学科,不需要再if
                }
            }
            cout << ans_rank << " " << ans_subj << endl;
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值