1141 PAT Ranking of Institutions (25分)
题目大意:给定考生ID,分数,学校,要求对各个学校总分进行排名。
tips:
- 学校code需要全部转换为小写
- 按照分数排名,如果分数相同,按照考生人数从小到大排列,如果考生人数相同,则按照字母表顺序排列。
- 分数需要到最后再转化为int(测试点5)
- 如果用map超时,可以考虑使用unordered_map(不过我用map并没有超时)
#include<iostream> //输入输出流头文件
#include<algorithm> //C++标准模板库的函数
#include<map> //map映射容器
#include<vector> //变长数组容器
#include<string> //C++string类
using namespace std; //标准命名空间
int n;
struct sc{
int ns;
double totalscore;
};
bool cmp(pair<string,sc> a,pair<string,sc> b){
return (int)a.second.totalscore!=(int)b.second.totalscore?a.second.totalscore>b.second.totalscore:(a.second.ns!=b.second.ns?a.second.ns<b.second.ns:a.first<b.first);
}
int main(){ //主函数
#ifdef ONLINE_JUDGE //如果有oj系统(在线判定),则忽略文件读入,否则使用文件作为标准输入
#else
freopen("1.txt", "r", stdin); //从1.txt输入数据
#endif
cin>>n;
map<string ,sc> m;
for(int i=0;i<n;i++){
string id,school;
double score;
cin>>id>>score>>school;
for(int j=0;j<school.size();j++){
if(school[j]<'a') school[j]=school[j]+32;//变为小写字母
}
string level=id.substr(0,1);//等级
if(level=="B")score/=1.5;
else if(level=="T")score*=1.5;
m[school].ns++;
m[school].totalscore+=score;
}
vector<pair<string,sc>> v(m.begin(), m.end());//存到vector中,按分数排序
cout<<m.size()<<endl;
sort(v.begin(),v.end(),cmp);
int i=1, lastscore=v[0].second.totalscore;//i为排名
cout<<i<<' '<<v[0].first<<' '<<(int)v[0].second.totalscore<<' '<<v[0].second.ns<<endl;
for(int j=1;j<v.size();j++){
if((int)v[j].second.totalscore==lastscore) cout<<i;
else {cout<<j+1;i=j+1;}
cout<<' '<<v[j].first<<' '<<(int)v[j].second.totalscore<<' '<<v[j].second.ns<<endl;
lastscore=v[j].second.totalscore;
}
return 0; //返回0,如果不返回0,PAT会报错
}