• 【PAT甲级】1141 PAT Ranking of Institutions


    ✍个人博客:https://blog.csdn.net/Newin2020?spm=1011.2415.3001.5343
    📚专栏地址:PAT题解集合
    📝原题地址:题目详情 - 1141 PAT Ranking of Institutions (pintia.cn)
    🔑中文翻译:PAT单位排行
    📣专栏定位:为想考甲级PAT的小伙伴整理常考算法题解,祝大家都能取得满分!
    ❤️如果有收获的话,欢迎点赞👍收藏📁,您的支持就是我创作的最大动力💪

    1141 PAT Ranking of Institutions

    After each PAT, the PAT Center will announce the ranking of institutions based on their students’ performances. Now you are asked to generate the ranklist.

    Input Specification:

    Each input file contains one test case. For each case, the first line gives a positive integer N (≤105), which is the number of testees. Then N lines follow, each gives the information of a testee in the following format:

    ID Score School
    
    • 1

    where ID is a string of 6 characters with the first one representing the test level: B stands for the basic level, A the advanced level and T the top level; Score is an integer in [0, 100]; and School is the institution code which is a string of no more than 6 English letters (case insensitive). Note: it is guaranteed that ID is unique for each testee.

    Output Specification:

    For each case, first print in a line the total number of institutions. Then output the ranklist of institutions in nondecreasing order of their ranks in the following format:

    Rank School TWS Ns
    
    • 1

    where Rank is the rank (start from 1) of the institution; School is the institution code (all in lower case); TWS is the total weighted score which is defined to be the integer part of ScoreB/1.5 + ScoreA + ScoreT*1.5, where ScoreX is the total score of the testees belong to this institution on level X; and Ns is the total number of testees who belong to this institution.

    The institutions are ranked according to their TWS. If there is a tie, the institutions are supposed to have the same rank, and they shall be printed in ascending order of Ns. If there is still a tie, they shall be printed in alphabetical order of their codes.

    Sample Input:

    10
    A57908 85 Au
    B57908 54 LanX
    A37487 60 au
    T28374 67 CMU
    T32486 24 hypu
    A66734 92 cmu
    B76378 71 AU
    A47780 45 lanx
    A72809 100 pku
    A03274 45 hypu
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    Sample Output:

    5
    1 cmu 192 2
    1 au 192 3
    3 pku 100 1
    4 hypu 81 2
    4 lanx 81 2
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    思路

    这道题需要对所有单位进行排序,输出的第一个值是排位;第二个值是单位名称;第三个值是该单位中所有人的分数权值之和,取整数;第四个值输出该单位的人数总和。

    1. 先将所有信息读入哈希表中,哈希表中的主键维护的是小写的字符串,所以要将大写字母全部转换成小写字母再存入哈希表。另外,哈希表中存的是计算后总分数,所以要对读取的分数进行换算,根据 id 中第一个字母进行改变,如果是 B 需要除以 1.5 ,如果是 A 直接加上就好,如果是 T 需要乘以 1.5
    2. 我们用一个 vector 容器存放哈希表中的所有单位信息,因为 vector 可以利用 sort 函数进行排序。当然这里的 sort 需要自定义排序规则,按照单位总分数的大小进行降序排序,如果分数相等则按照每个单位的总人数进行升序排序,如果单位人数也相等则按照单位名字的字典序从小到大进行排序。
    3. 排完序之后先输出总单位数,然后依次输出每个单位的排位信息,注意排名只根据分数进行划分,存在出现同排名的情况。
    代码
    #include
    using namespace std;
    
    struct School {
        string name;
        int cnt;
        double sum;
    
        School() :cnt(0), sum(0) {}
    
        bool operator <(const School& s)const
        {
            if (sum != s.sum)  return sum > s.sum;   //按分数降序
            if (cnt != s.cnt)  return cnt < s.cnt;   //按人数升序
            return name < s.name; //按字典序升序
        }
    };
    
    int main()
    {
        int n;
        cin >> n;
    
        unordered_map<string, School> hash;
        for (int i = 0; i < n; i++)
        {
            string id, name;
            double score;
            cin >> id >> score >> name;
    
            //将大写转换成小写
            for (auto& c : name)   c = tolower(c);
    
            //根据等级改变分数
            if (id[0] == 'B')  score /= 1.5;
            else if (id[0] == 'T') score *= 1.5;
    
            //更新信息
            hash[name].sum += score;
            hash[name].cnt++;
            hash[name].name = name;
        }
    
        //将所有单位存到容器中去,方便排序
        vector<School> ans;
        for (auto x : hash)
        {
            x.second.sum = (int)(x.second.sum + 1e-8); //防止出现精度问题
            ans.push_back(x.second);
        }
        sort(ans.begin(), ans.end());    //排序
    
        cout << ans.size() << endl; //输出有多少个单位
    
        //输出每所单位信息
        int rank = 1;
        for (int i = 0; i < ans.size(); i++)
        {
            auto x = ans[i];
            if (i > 0 && x.sum < ans[i - 1].sum)    rank = i + 1;
            printf("%d %s %d %d\n", rank, x.name.c_str(), (int)x.sum, x.cnt);
        }
    
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
  • 相关阅读:
    基于TOTP算法的Github两步验证2FA(双因子)机制Python3.10实现
    C++ 字符串编码转换封装函数,UTF-8编码与本地编码互转
    【机器学习】使用scikitLearn进行决策树分类与回归:DecisionTreeClassifier及DecisionTreeRegressor
    使用CompletionService进行多个文件打包为zip下载
    leetcode 49. 字母异位词分组
    “蔚来杯“2022牛客暑期多校训练营4-N、K、D(待补)、H
    前端面试第一周快速复盘,不标准的面试经验分享 (一)
    2023年【起重信号司索工(建筑特殊工种)】试题及解析及起重信号司索工(建筑特殊工种)操作证考试
    机器人操作系统ROS(19) 雷达和摄像头融合的资料
    无版权 NFT 会为市场带来改变么?
  • 原文地址:https://blog.csdn.net/Newin2020/article/details/126681299