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.
每次 PAT 考试结束后,考试中心都会发布一个考生单位排行榜。本题就请你实现这个功能。
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:
输入第一行给出一个正整数 N(≤10^5),即考生人数。随后 N 行,每行按下列格式给出一个考生的信息:
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.
其中准考证号是由 6 个字符组成的字符串,其首字母表示考试的级别:B代表乙级,A代表甲级,T代表顶级;得分是 [0, 100] 区间内的整数;学校是由不超过 6 个英文字母组成的单位码(大小写无关)。注意:题目保证每个考生的准考证号是不同的。
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.
其中排名是该单位的排名(从 1 开始);学校是全部按小写字母输出的单位码;加权总分定义为乙级总分/1.5 + 甲级总分 + 顶级总分*1.5的整数部分;考生人数是该属于单位的考生的总人数。
学校首先按加权总分排行。如有并列,则应对应相同的排名,并按考生人数升序输出。如果仍然并列,则按单位码的字典序输出。
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
解题思路
- 建立结构体保存学校数据,其中分数需要有double和int两种储存形式;
- 读入每组数据,并将其做预处理:学校名都改为小写字母;
- 使用map进行学校名到数字的转化;
- 处理成绩,并在数据录入完成后将其总分变为int形式;
- 排序并按照题目要求输出;
- 返回零值。
代码
#include<iostream>
#include<string>
#include<map>
#include<algorithm>
using namespace std;
#define maxn 100010
map<string,int> mp;
int num=0;
struct Node{
string name;
int TWS,Ns;
double T;
}S[maxn];
bool cmp(Node a,Node b){
if(a.TWS!=b.TWS){
return a.TWS>b.TWS;
}else if(a.Ns!=b.Ns){
return a.Ns<b.Ns;
}else{
return a.name<b.name;
}
}
int main(){
int N,G,t;
int i=0,now=-1,nowR=1;
string ID,school;
cin>>N;
while(N--){
cin>>ID>>G>>school;
for(i=0;i<school.length();i++){
if(school[i]>='A'&&school[i]<='Z'){
school[i]=school[i]-'A'+'a';
}
}
if(mp.find(school)==mp.end()){
t=num;
S[t].name=school;
mp[school]=num++;
}else{
t=mp[school];
}
if(ID[0]=='T'){
S[t].T+=1.5*G;
}else if(ID[0]=='B'){
S[t].T+=2.0*G/3.0;
}else{
S[t].T+=1.0*G;
}
S[t].Ns++;
}
for(i=0;i<num;i++){
S[i].TWS=(int)S[i].T;
}
sort(S,S+num,cmp);
cout<<num<<endl;
for(i=0;i<num;i++){
if(S[i].TWS==now){
cout<<nowR;
}else{
cout<<i+1;
now=S[i].TWS;
nowR=i+1;
}
cout<<" "<<S[i].name<<" "<<(int)S[i].TWS<<" "<<S[i].Ns<<endl;
}
return 0;
}
运行结果