PAT 甲级 1141 PAT Ranking of Institutions (25 分)

思路:

1.以校名为索引存储相关信息;
2.定义符合题意的sort()排序;

代码:

#include<iostream>
#include<unordered_map>
#include<string>
#include<cctype>
#include<vector>
#include<algorithm>
using namespace std;
struct ins{
	string code;
	int tws,score[3]={0},ns;
};
bool cmp(const ins & a,const ins & b){
	return a.tws==b.tws?(a.ns==b.ns?a.code<b.code:a.ns<b.ns):a.tws>b.tws;
}
int main(){
	int n;
	cin>>n;
	unordered_map<string,ins> mp;
	for(int i=0;i<n;i++){
		string id,code;
		int score;
		cin>>id>>score>>code;
		for(int j=0;j<code.length();j++) code[j]=tolower(code[j]);
		mp[code].ns++;
		id[0]=='B'||id[0]=='A'?mp[code].score['B'-id[0]]+=score:mp[code].score[2]+=score;
	}
	vector<ins> v;
	for(auto e:mp)
	{
		ins i;
		i.code=e.first;
		i.tws=e.second.score[0]/1.5+e.second.score[1]+e.second.score[2]*1.5;
		i.ns=e.second.ns;
		v.push_back(i);
	}
	sort(v.begin(),v.end(),cmp);
	int rank=1,size=v.size();
	printf("%d\n",size);
	for(int i=0;i<size;i++)
		if(i==0||v[i].tws==v[i-1].tws) printf("%d %s %d %d\n",rank,v[i].code.c_str(),v[i].tws,v[i].ns);
		else{
			printf("%d %s %d %d\n",i+1,v[i].code.c_str(),v[i].tws,v[i].ns);
			rank=i+1;
		}
	return 0;
}
posted @ 2019-08-12 14:58  YuhanのBlog  阅读(68)  评论(0编辑  收藏  举报