每次 PAT 考试结束后,考试中心都会发布一个考生单位排行榜。本题就请你实现这个功能。 输入格式:输入第一行给出一个正整数 N(≤105),即考生人数。随后 N 行,每行按下列格式给出一个考生的信息: 其中 输出格式:首先在一行中输出单位个数。随后按以下格式非降序输出单位的排行榜: 其中 学校首先按加权总分排行。如有并列,则应对应相同的排名,并按考生人数升序输出。如果仍然并列,则按单位码的字典序输出。 |
输入样例:
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
输出样例:
5
1 cmu 192 2
1 au 192 3
3 pku 100 1
4 hypu 81 2
4 lanx 81 2
考察 : 字符串处理与排序 (卡了好一会儿) |
注意 : 总分只比较整数部分(91.1=91.7),分数相同排名相同 |
思路 : 直接map<string,xxx>会超时,那就先用map<string,int>将学校转化为对应唯一下标,然后用数据结构数组存入 |
C/C++
#include<bits/stdc++.h>
using namespace std;
struct School{
string name;
long scoreB=0,scoreA=0,scoreT=0,num=0; // BAT三种分数,num为该校学生人数
}school[100001];
bool cmp(const School& x, const School& y){
if(x.scoreA!=y.scoreA) return x.scoreA>y.scoreA;
if(x.num!=y.num) return x.num < y.num;
return x.name < y.name;
}
int main()
{
map<string,long> id;
string s1,s2;
long N,num=1,n,f; // num : 为学校个数-1
scanf("%ld",&N);
while (N--){
cin >> s1 >> n >> s2;
for(char &x:s2) if(x >= 'A' && x <= 'Z') x+=32; // 学校名字统一转换为小写
f = id[s2]; // f:获取该学校对应的整数下标
if(f==0) // 为0说明第一次出现那么其下标为num,同时num+=1
{
f = id[s2] = num++;
school[f].name = s2;
}
school[f].num++; // 该校学生+1
if(s1[0]=='B') school[f].scoreB += n;
else if(s1[0]=='A') school[f].scoreA += n;
else school[f].scoreT += n;
}
for(long z=1;z<num;z++) school[z].scoreA += (long)(school[z].scoreB/1.5 + school[z].scoreT*1.5); // 将总分存入scoreA
sort(school+1,school+num,cmp);
// 答案输出
cout << num-1 << endl;
for(long z=1;z<num;z+=n) { // 注意同分排名并列
n = 1;
while ((z+n)<num && school[z].scoreA==school[z+n].scoreA) n++;
for(long z1=z;z1<z+n;z1++)
cout << z << " " << school[z1].name << " " << school[z1].scoreA << " " << school[z1].num << endl;
}
return 0;
}
// #include<bits/stdc++.h>
// using namespace std;
// map<string,long> num; // 该学校参赛人数
// map<string,long> score; // 该学校总得分
// map<string,bool> appear; // 避免学校姓名重复存入
// vector<string> school; // 该学校名字存入
// bool cmp(string x,string y){
// if(score[x]!=score[y]) return score[x]>score[y];
// if(num[x]!=num[y]) return num[x]<num[y];
// return x<y;
// }
// int main()
// {
// // string id,name;
// char id[7],name[7];
// long N,n;
// scanf("%ld",&N);
// while (N--){
// scanf("%s %ld %s",&id,&n,&name);
// for(char &x:name) if(x >= 'A' && x <= 'Z') x+=32;
// // _strlwr(name); // 等价上述循环
// // num[name]++;
// if(id[0]=='B') score[name] += n/1.5;
// else if(id[0]=='A') score[name] += n;
// else score[name] += n*1.5;
// if(0==num[name]++) school.push_back(name);
// // appear[name] = true;
// }
// // for(string x:school) score[x] = (long)score[x];
// sort(school.begin(),school.end(),cmp);
// long len = school.size();
// printf("%ld\n",len);
// for(long z=0;z<len;z+=n){
// n = 1;
// while (z<len-1 && score[school[z]]==score[school[z+n]]) n++;
// for(long z1=0;z1<n;z1++)
// {
// string s = school[z+z1];
// printf("%ld %s %ld %ld\n",z+1,s.c_str(),score[s],num[s]);
// }
// // cout << z+1 << " " << school[z+z1] << " " << score[school[z+z1]] << " " << num[school[z+z1]] << endl;
// }
// return 0;
// }