PAT A1012 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

题意

每名学生有 C M E 三门成绩以及平均成绩 A 求在所有学生中某一项排名最靠前的


思路1

  1. 将每名学生存储成一个 map 映射,为 id 对应其四门成绩 vector<int>类型的
  2. vector<int> grades[4] 则存储四门课程所有的学生的单门课程成绩并排序
  3. 对于每一个查询,先看 map 中是否存在该名学生,如存在分别计算出每门课程的成绩,取排名最高的一个

注意点:

  1. 四舍五入使用 round 浮点数
  2. 临时的 temp 处理输入需要进行初始化 0
  3. 默认升序,排名 = i+1

代码1

#include <iostream>
#include <vector>
#include <unordered_map>
#include <algorithm>
#include <cmath>
using namespace std;

unordered_map<string, vector<int>> mp;  // 学生--成绩
vector<int> grades[4];   // 四门课程所有成绩
char names[] = "ACME";

int get_rank(const vector<int> &v, int x)
{
    for(int i = 0; i < v.size(); i++)
    {
        if(v[i] == x)
            return i + 1;
    }
    return -1;
}

bool cmp(int a, int b)
{
    return a > b;
}

int main()
{
    int n, m;
    cin >> n >> m;
    // 输入
    for(int i = 0; i < n; i++)
    {
        string id;
        int temp[4] = {0}; // ~~初始化
        cin >> id;
        for(int i = 1; i <= 3; i++)
        {
            cin >> temp[i];
            temp[0] += temp[i];
        }
        temp[0] = round(temp[0]/3.0);  // 四舍五入取成绩

        for(int i = 0; i < 4; i++)
        {
            mp[id].push_back(temp[i]);  // 学生的四门成绩映射
            grades[i].push_back(temp[i]);  // 每一门课程
        }
    }

    // 排序
    for(int i = 0; i < 4; i++)
        sort(grades[i].begin(), grades[i].end(), cmp);

    // 查询
    while(m--)
    {
        string id;
        cin >> id;
        if(mp.find(id) == mp.end())
        {
            cout << "N/A" << endl;
        }
        else
        {
            // 找最高排名
            int ans = n + 1; // max
            char c;
            for(int i = 0; i < 4; i++)  // 四门课程
            {
                int r = get_rank(grades[i], mp[id][i]);
                if(r < ans)
                {
                    ans = r;
                    c = names[i];
                }
            }
            cout << ans << " " << c << endl;
        }
    }
    return 0;
}


思路2

整体思路大致一样,但是由于 grades[i] 是有序的,因此可以使用 二分 来查找

代码2

#include <iostream>
#include <vector>
#include <unordered_map>
#include <algorithm>
#include <cmath>
using namespace std;

unordered_map<string, vector<int>> mp;  // 学生--成绩
vector<int> grades[4];   // 四门课程所有成绩
char names[] = "ACME";

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

int main()
{
    int n, m;
    cin >> n >> m;
    // 输入
    for(int i = 0; i < n; i++)
    {
        string id;
        int temp[4] = {0}; // ~~初始化
        cin >> id;
        for(int i = 1; i <= 3; i++)
        {
            cin >> temp[i];
            temp[0] += temp[i];
        }
        temp[0] = round(temp[0]/3.0);  // 四舍五入取成绩
        
        for(int i = 0; i < 4; i++)
        {
            mp[id].push_back(temp[i]);  // 学生的四门成绩映射
            grades[i].push_back(temp[i]);  // 每一门课程
        }
    }
    
    // 排序
    for(int i = 0; i < 4; i++)
        sort(grades[i].begin(), grades[i].end());
    
    // for(int i = 0; i < 4; i++)
    //     for(int j = 0; j < grades[i].size(); j++)
    //         cout << grades[i][j] << " ";
    //     cout << endl;
    
    
    // 查询
    while(m--)
    {
        string id;
        cin >> id;
        if(mp.find(id) == mp.end())
        {
            cout << "N/A" << endl;
        }
        else
        {
            // 找最高排名
            int ans = n + 1; // max
            char c;
            for(int i = 0; i < 4; i++)  // 四门课程
            {
                int r = get_rank(grades[i], mp[id][i]);
                if(r < ans)
                {
                    ans = r;
                    c = names[i];
                }
            }
            cout << ans << " " << c << endl;
        }
    }
    return 0;
}
发布了166 篇原创文章 · 获赞 27 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/HdUIprince/article/details/105474304
今日推荐