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 (≤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.
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/1.5 + A + T * 1.5, 根据id的首字母识别究竟是那种类型的测试得分(B、A或T)。排名时,按照总分非增序,如果总分一样,按照人数少的优先,若都一样,输出校名较小的那个。
思路:
- 用结构体node存储记录,先按照校名排好一遍,再令新结构体sch,存储校名、总分和总人数。
- 最后再将学校按要求排序就可以了。
- 注意:由于给出的学校名称有大小写情况,要统一成小写字母用transform函数。
- 最后一个测试点5不通过的原因是,需要用一个double型变量计算完总分再取整。
参考代码:
#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
using namespace std;
struct node{
string id, sch;
int v;
};
vector<node> list;
struct sch{
string id;
int w, num;
};
vector<sch> ans;
int n;
bool cmp1(node a, node b){return a.sch < b.sch;}
bool cmp2(sch a, sch b){
if(a.w != b.w) return a.w > b.w;
else if(a.num != b.num) return a.num < b.num;
else return a.id < b.id;
}
int main(){
scanf("%d", &n);
list.resize(n); ans.resize(n);
for(int i = 0; i < n; ++i){
cin >> list[i].id >> list[i].v >> list[i].sch;
transform(list[i].sch.begin(), list[i].sch.end(),list[i].sch.begin(), ::tolower);
}
sort(list.begin(), list.end(), cmp1);
int i = 0, idx = 0;
while(i < list.size()){
int next = i + 1;
while(next < list.size() && list[next].sch == list[i].sch) next++;
ans[idx].id = list[i].sch; ans[idx].num = next - i;
double total = 0.0;
for(int j = i; j < next; ++j){
if(list[j].id[0] == 'B') total += 1.0 * list[j].v / 1.5;
else if(list[j].id[0] == 'A') total += 1.0 * list[j].v;
else total += 1.5 * list[j].v;
}
ans[idx].w = total;
idx++;
i = next;
}
sort(ans.begin(), ans.begin() + idx, cmp2);
printf("%d\n", idx);
int r = 1;
printf("1 %s %d %d\n", ans[0].id.c_str(), ans[0].w, ans[0].num);
for(int i = 1; i < idx; ++i){
if(ans[i].w == ans[i - 1].w) printf("%d ", r);
else printf("%d ", i + 1), r = i + 1;
printf("%s %d %d\n", ans[i].id.c_str(), ans[i].w, ans[i].num);
}
return 0;
}