1141 PAT Ranking of Institutions (25 分)

1141 PAT Ranking of Institutions (25 分)

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

题目大意:
给定n个学生的id、score、school,学校名称不区分大小写。要求输出每个学校的排名、加权总分和学生人数

算法分析:
设置结构体存储每个学生的信息,将出现的学校名称单独放入一个set中(去重)。输入完毕后,遍历set,计算每个学校的总分和学生人数,另设结构体压入vector中,再对vector进行排序

错误代码(20分,最后两个点运行超时)

#include<bits/stdc++.h>
using namespace std;
struct testee{
	string id;
	int score;
	string school;
};
struct testee2{
	string school;
	int totalScore;
	int cnt;
};
int cmp(testee2 a,testee2 b){
	if(a.totalScore!=b.totalScore)return a.totalScore>b.totalScore;
	if(a.cnt!=b.cnt)return a.cnt<b.cnt;
	return a.school<b.school;
}
int main()
{
	int num;
	cin>>num;
	struct testee list[num];
	set<string> s;
	vector<testee2> v;
	for(int i=0;i<num;i++){
		getchar();
		cin>>list[i].id>>list[i].score>>list[i].school;
		for(int j=0;j<list[i].school.size();j++){
			if(list[i].school[j]>='A'&&list[i].school[j]<='Z')
			list[i].school[j]=tolower(list[i].school[j]);
		}
		s.insert(list[i].school);
	}
	for(auto it=s.begin();it!=s.end();it++){
		int total=0,cnt=0;
		for(int j=0;j<num;j++){
			if(list[j].school==*it){
				cnt++;
				if(list[j].id[0]=='B')
				total+=list[j].score/1.5;
				else if(list[j].id[0]=='A')
				total+=list[j].score;
				else
				total+=list[j].score*1.5;
			}
		}
		testee2 t={*it,total,cnt};
		v.push_back(t);
	}
	sort(v.begin(),v.end(),cmp);
	int cnt=1,front=v[0].totalScore;
	cout<<v.size()<<"\n";
	cout<<cnt<<" "<<v[0].school<<" "<<v[0].totalScore<<" "<<v[0].cnt<<"\n"; 
	for(int i=1;i<v.size();i++){
		if(v[i].totalScore==front)
		//cnt中存储的是输出的前一个学校的排名,如果二者总分相同则排名一样 
		cout<<cnt<<" "<<v[i].school<<" "<<v[i].totalScore<<" "<<v[i].cnt<<"\n"; 
		else{
			cout<<i+1<<" "<<v[i].school<<" "<<v[i].totalScore<<" "<<v[i].cnt<<"\n"; 
			cnt=i+1;
		}
		front=v[i].totalScore;
	}
}

后参考柳神代码,设置两个map,cnt用来计算学校的学生人数,sum用来计算总分。在输入时直接完成对学生人数和总分的计算。输入完毕后,遍历任意map,将学校名称、分数、学生人数信息放入结点中,再压入vector,最后对vector进行排序

注意:
对于输出的处理:设立front设立上一个学校的总分,当当前学校的总分和front相同时,二者排名相同;若不同,排名即为遍历的顺序

读入的score要用double存储,不能直接用int

AC代码:

#include<bits/stdc++.h>
using namespace std;
struct testee2{
	string school;
	int totalScore;
	int cnt;
};
int cmp(testee2 a,testee2 b){
	if(a.totalScore!=b.totalScore)return a.totalScore>b.totalScore;
	if(a.cnt!=b.cnt)return a.cnt<b.cnt;
	return a.school<b.school;
}
int main()
{
	int num;
	double score;
	string id,school;
	cin>>num;
	vector<testee2> v;
	map<string,int> cnt;
	map<string,double> sum;
	for(int i=0;i<num;i++){
		getchar();//cin会将回车放入缓冲区中,记得读掉 
		cin>>id>>score>>school;
		for(int j=0;j<school.size();j++){
			if(school[j]>='A'&&school[j]<='Z')
			school[j]=tolower(school[j]);
		}
		if(id[0]=='B')
		score=score/1.5;
		//这里的score一定要用double存,不能每次计算完后直接转成int,不然最后一个点过不了 
		else if(id[0]=='T')
		score=score*1.5;
		sum[school]+=score;
		cnt[school]++;
	}
	for(auto it=sum.begin();it!=sum.end();it++){
		v.push_back(testee2{it->first,(int)it->second,cnt[it->first]});
	}
	sort(v.begin(),v.end(),cmp);
	int cnt2=1,front=v[0].totalScore;
	cout<<v.size()<<"\n";
	cout<<cnt2<<" "<<v[0].school<<" "<<v[0].totalScore<<" "<<v[0].cnt<<"\n"; 
	for(int i=1;i<v.size();i++){
		if(v[i].totalScore==front)
		//cnt2中存储的是输出的前一个学校的排名,如果二者总分相同则排名一样 
		cout<<cnt2<<" "<<v[i].school<<" "<<v[i].totalScore<<" "<<v[i].cnt<<"\n"; 
		else{
			cout<<i+1<<" "<<v[i].school<<" "<<v[i].totalScore<<" "<<v[i].cnt<<"\n"; 
			cnt2=i+1;
		}
		front=v[i].totalScore;
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值