PAT--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

解题思路: 这题用的是map,我一看到这题的思路就是怎么下标可以是字符串,然后看了别人的代码,才想起map可以有这个功能,这个人真的好牛,特别喜欢她的,因为她的代码简洁又可以看得懂,不过直接用map好像会超时,用的是unordered_map<string,int>,unordered_map<string,double>,vector<point>,vector这是我第一次保存数据结构类型,涨知识了,看她的代码可以学到很多东西。

#include<bits/stdc++.h>
using namespace std;
struct point{
	string s;
	int tws,ns;
}; 

bool cmp(point a,point 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.s<b.s;
}

int main(void)
{
	unordered_map<string,int>cnt; //同一个学校的人数
	unordered_map<string,double>sum;//学校的得分
	int n;
	scanf("%d",&n);
	for(int i=0;i<n;i++)
	{
		string id;
		cin>>id;
		double score;
		scanf("%lf",&score);
		string sc;
		cin>>sc;
		for(int j=0;j<sc.size();j++) sc[j]=tolower(sc[j]);
		if(id[0]=='A') score=score;
		else  if(id[0]=='B') score=score/1.5;
		else score=score*1.5;
		cnt[sc]++;
		sum[sc]+=score;
	}
	vector<point> p;//vector数据结构类型
	for(auto it=cnt.begin();it!=cnt.end();it++)
	{
		p.push_back(point{it->first,(int)sum[it->first],cnt[it->first]});//数据结构里面有三个元素,sum只要整数部分
	}
	sort(p.begin(),p.end(),cmp);
	printf("%d\n",cnt.size());
	int rank=0,pre=-1;
	for (int i = 0; i < p.size(); i++)//处理排名和输出
        {
            if (pre != p[i].tws) rank = i + 1;
            pre = p[i].tws;
            printf("%d ", rank);
            cout << p[i].s;
            printf(" %d %d\n", p[i].tws, p[i].ns);
        }
	return 0;
}

猜你喜欢

转载自blog.csdn.net/Imagirl1/article/details/82220138