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

题意是先输入两个整数n和m。接下来就输入n行学生的学号 c语言成绩 数学成绩 英语成绩 再输入m个学号。要求输出这m个学号所代表学生的最好的一门成绩的排名 和那门成绩所代表的字母,如果同时有好几门成绩的排名都是最好的话,就按照平均成绩,C语言成绩,数学成绩,英语成绩的优先程度决定输出的科目。如果输入的学号查无此人的话就输出N/A。

#include<iostream>
#include<unordered_map>
#include<vector>
#include<math.h>
#include<algorithm>
using namespace std;

unordered_map <string, vector<int>> grades;
vector<int> q[4];            //q[0]代表A,q[1]代表C,q[2]代表M,q[3]代表E
int n,m;

int get_rank(vector<int>& a,int x)
{
    int l = 0,r = a.size() - 1;
    while(l < r)
    {
        int mid = l + r + 1 >> 1;
        if(a[mid] <= x) l = mid;
        else r = mid - 1;
    }
    return a.size()-r;
}

int main()
{
    cin>>n>>m;

    //输入数据,并将数据存入
    for(int i = 0; i < n; i ++)
    {
        string id;
        int t[4]={0};
        cin >> id;
        for(int j = 1; j <= 3; j ++)
        {
            cin >> t[j];
            t[0] += t[j];
        }
        t[0]=round(t[0] / 3);

        for(int j = 0; j <= 3; j ++)
        {
            grades[id].push_back(t[j]);
            q[j].push_back(t[j]);
        }
    }

    //对各门成绩进行排序
    for(int i = 0;i <= 3; i ++)
        sort(q[i].begin(),q[i].end());

    //输入要查询的id,并寻找其最高排名并输出
    char cls[] = "ACME";
    for(int i = 0; i < m; i ++)
    {
        string id;
        cin >> id;
        if(grades.count(id) == 0)
            cout<<"N/A"<<endl;
        else
        {
            int s = n + 1;
            char c;
            for(int j = 0; j < 4; j ++ )
            {
                int rank = get_rank(q[j],grades[id][j]);
                if(rank < s)
                {
                    s = rank;
                    c = cls[j];
                }
            }
            cout<<s<<" "<<c<<endl;
        }
    }
    return 0;
}

中间有涉及到二分查找,我用了y总的第一个模板,WA了,后来仔细一研究发现第一个模板在查找的范围内有多个相同元素时,指向的是第一个,而第二个模板在查找的范围内有多个相同元素时,指向的是最后一个。二分模板与总结链接→https://www.acwing.com/blog/content/91/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值