PAT甲级A1141 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,成绩,所属学校,让你按要求对这些学生所属的学校进行排名 。排序规则如下:

①学生学号第一位若为B则为乙级成绩,若为A则为甲级成绩,若为T则为顶级成绩。学校的最终成绩为ScoreB/1.5 + ScoreA + ScoreT*1.5, 其中scoreX表示学校对应级别的总分。

②按加权分降序排列,分数相同则排名相同,但排序时,如果分数相同则按照学校参赛人数升序排列,如果还相同,则按学校名称字母顺序排列。其中学校的名称同一输出小写格式。

思路:为了方便排序,使用vector存放学校的id,分数等信息,结构体如下:

struct node{
	string school;
	int cnt,Bscore,Ascore,Tscore,score;    //分别对应学生人数,乙级总分,甲级总分,顶级总分和最终加权分
};

使用map<string,int>来存放学校在vector中的下标idx,使用函数getIdx()来获取下标,分数输入完毕后,遍历vector求加权分,然后按要求排序,输出。

参考代码:

#include<cstdio>
#include<vector>
#include<cstring>
#include<string>
#include<iostream>
#include<algorithm>
#include<unordered_map>
using namespace std;
unordered_map<string,int> mp;
struct node{
	string school;
	int cnt,Bscore,Ascore,Tscore,score;
};
node a[100010];
int n,idx=0;
int getIdx(string school){
	if(mp.find(school)==mp.end()){
		mp[school]=idx;
		return idx++;
	}else return mp[school];
}
bool cmp(node a,node b){
	if(a.score!=b.score)
		return a.score>b.score;
	else if(a.cnt!=b.cnt)
		return a.cnt<b.cnt;
	else return a.school<b.school;
}
int main()
{
	scanf("%d",&n);
	int score;
	string stu_id,school;
	for(int i=0;i<n;i++){
		cin>>stu_id>>score>>school;
		for(int i=0;i<school.size();i++){
			if(school[i]>='A'&&school[i]<='Z')	//统一校名为小写字母
				school[i]+=32;
		}
		int id=getIdx(school);			//转换校名为对应idx
		if(stu_id[0]=='B') a[id].Bscore+=score;
		if(stu_id[0]=='T') a[id].Tscore+=score;
		if(stu_id[0]=='A') a[id].Ascore+=score;
		a[id].school=school;
		a[id].cnt++;
	}
	for(int i=0;i<idx;i++)					//求总加权分
		a[i].score=(int)(a[i].Bscore/1.5+a[i].Ascore+a[i].Tscore*1.5);
	sort(a,a+idx,cmp);
	cout<<idx<<endl;
	int Rank=1;		//接下来输出排名
	cout<<"1 "<<a[0].school<<" "<<a[0].score<<" "<<a[0].cnt<<endl;
	for(int i=1;i<idx;i++){
		if(a[i].score!=a[i-1].score) 
			Rank=i+1;		
		cout<<Rank<<" "<<a[i].school<<" "<<(int)a[i].score<<" "<<a[i].cnt<<endl;	//注意分数要输出整型
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/vir_lee/article/details/88424087