17冬第二题 PAT甲级 1141 Ranking of Institutions (25分) 有点鸡贼

博客解析了PAT甲级1141题的学校排名问题,涉及加权成绩计算和非降序排列。介绍了使用unordered_map处理数据、自定义排序规则以及学校名称转小写的方法,强调了数据类型选择和避免超时的优化策略。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目

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 (≤10​5​​), which is the number of testees. Then N lines follow, each gives the information of a testee in the following format:

ID Score School

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

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

Sample Output:

5
1 cmu 192 2
1 au 192 3
3 pku 100 1
4 hypu 81 2
4 lanx 81 2

题意

致谢:https://blog.youkuaiyun.com/liuchuo/article/details/79618766

给出每个学生的id、分数、学校,学校名称不区分大小写,输出学校排名、学校名称、总加权成绩、学校参赛人数。学校名称输出时候以小写方式输出。

分析

致谢:https://blog.youkuaiyun.com/liuchuo/article/details/79618766

1.两个unordered_map,一个用来存储某学校名称对应的参赛人数,另一个计算某学校名称对应的总加权成绩

2.对于排名的处理:设立pres表示前一个学校的加权总分,如果pres和当前学校的加权总分不同,说明rank等于数组下标+1,否则rank不变

陷阱

1.结果是截取的整数
计数的时候存浮点数,存到结果的时候就要截取成整数,输出的时候也是整数。这三个地方反过来一个都不行。

2.map会超时
1153同款操作,只能用unordered_map

3.排名
分、人数、代号都要参加排序,自己写排序函数

4.学校名要存小写
没有现成的转换库函数,要自己写

int j=0;
while(j<sn.size()){
    sn[j]=tolower(sn[j]);
     j++;
}


满分代码一

#include<iostream>
#include<vector>
#include<unordered_map>
#include<cctype>
using namespace std;
int n,m;
string si,sn;
struct school{
    string id;
    int score,number;
};
bool cmp(school &a,school &b){
    if(a.score!=b.score)return a.score>b.score;
    else if(a.number!=b.number) return a.number<b.number;
    else return a.id<b.id;
}
int main(){
    scanf("%d",&n);
    unordered_map<string,double> m1;
    unordered_map<string,int> m2;
    for(int i=0;i<n;i++){
        cin>>si>>m>>sn;
        int j=0;
        while(j<sn.size()){
            sn[j]=tolower(sn[j]);
            j++;
        }
        if(si[0]=='A')m1[sn]+=m;
        else if(si[0]=='B')m1[sn]+=m/1.5;
        else if(si[0]=='T')m1[sn]+=m*1.5;
        m2[sn]++;
    }
    vector<school> v;
    for(auto it:m1){
        v.push_back({it.first,(int)it.second,m2[it.first]});
    }
    printf("%d\n",v.size());
    sort(v.begin(),v.end(),cmp);
    int rank=1,total=1,pre=0;
    for(auto it:v){
        if(total==1){
            printf("1 %s %d %d\n",it.id.c_str(),it.score,it.number);
        }else{
            if(it.score==pre){
                printf("%d %s %d %d\n",rank,it.id.c_str(),it.score,it.number);
            }else {
                printf("%d %s %d %d\n",total,it.id.c_str(),it.score,it.number);
                rank=total;
            }
        }
        pre=it.score;
        total++;
    }
    return 0;
}




满分代码二(也可以用set+运算符重载来代替向量的排序) 

#include<iostream>
#include<unordered_map>
#include<cctype>
#include<set>
using namespace std;
int n,m;
string si,sn;
struct school{
    string id;
    int score,number;
    bool operator < (const school &a) const{
        if(a.score!=score)return a.score<score;
        else if(a.number!=number) return a.number>number;
        else return a.id>id;
    }
};
set<school> s;
int main(){
    scanf("%d",&n);
    unordered_map<string,double> m1;
    unordered_map<string,int> m2;
    for(int i=0;i<n;i++){
        cin>>si>>m>>sn;
        for(int j=0;j<sn.size();j++) sn[j]=tolower(sn[j]);
        if(si[0]=='A')m1[sn]+=m;
        else if(si[0]=='B')m1[sn]+=m/1.5;
        else if(si[0]=='T')m1[sn]+=m*1.5;
        m2[sn]++;
    }
    for(auto it:m1){
        s.insert({it.first,(int)it.second,m2[it.first]});
    }
    printf("%d\n",s.size());
    int rank=1,total=1,pre=0;
    for(auto it:s){
        if(total==1){
            printf("1 %s %d %d\n",it.id.c_str(),it.score,it.number);
        }else{
            if(it.score==pre){
                printf("%d %s %d %d\n",rank,it.id.c_str(),it.score,it.number);
            }else {
                printf("%d %s %d %d\n",total,it.id.c_str(),it.score,it.number);
                rank=total;
            }
        }
        pre=it.score;
        total++;
    }
    return 0;
}

彩蛋

hpyu 哈佛大学、耶鲁大学和普林斯顿大学的合称

lanx 蓝翔

au 美利坚大学等好几所国外的大学/软件Au/金元素

pku 北京大学(陈越姥姥母校)

cmu 卡耐基梅隆大学

推荐阅读:2020年7月PAT甲级满分必备刷题技巧

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值