思路:
c++/STL,利用map保存输入,然后计算ScoreX,然后输出到vector,最后sort排序,输出排序结果。
1141 PAT Ranking of Institutions (25 point(s))
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 (≤105), 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.
Example:
#include<iostream>
#include<vector>
#include<map>
#include<algorithm>
#include<cstring>
using namespace std;
struct Score {
string School;
int A;
int B;
int T;
int Total;
int Cnt;
bool operator <(const Score& other) const {
return Total==other.Total?(Cnt==other.Cnt?(School<other.School):Cnt<other.Cnt):(Total>other.Total);
}
};
int main()
{
int N;
cin >> N;
map<string, Score> index;
for(int i = 0; i < N; i++) {
string ID, school;
int score;
cin >> ID >> score >> school;
transform(school.begin(), school.end(), school.begin(), ::tolower);
index[school].Cnt++;
index[school].School = school;
if(ID[0] == 'A') index[school].A += score;
else if(ID[0] == 'B') index[school].B += score;
else if(ID[0] == 'T') index[school].T += score;
}
vector<Score> S;
for(auto x = index.begin(); x != index.end(); x++) {
x->second.Total = x->second.B/1.5 + x->second.A + x->second.T*1.5;
S.push_back(x->second);
}
sort(S.begin(), S.end());
int last = 0;
int rank = 1;
int size = S.size();
cout << size << endl;
for(int i = 0; i < size; i++) {
if(S[i].Total != last) {
rank = i + 1;
last = S[i].Total;
}
printf("%d %s %d %d\n", rank, S[i].School.c_str(), S[i].Total, S[i].Cnt);
}
}