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 (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
 
 1 #include <stdio.h>
 2 #include <algorithm>
 3 #include <iostream>
 4 #include <map>
 5 #include <string>
 6 #include <vector>
 7 #include <set>
 8 #include <cctype>
 9 using namespace std;
10 const int maxn=10010;
11 int n,m,k;
12 struct node{
13     string id;
14     int score;
15     string ins;
16 };
17 struct inst{
18     string insti;
19     int tws;
20     int score[3]={0,0,0};
21     int ns;
22     bool operator < (const inst& a)const{
23         if(tws>a.tws)return true;
24         else if(tws==a.tws){
25             if(ns<a.ns) return true;
26             else if(ns==a.ns){
27                 return insti<a.insti?true:false;
28             }
29             else return false;
30         }
31         else return false;
32     }
33 };
34 int cnt;
35 map<string,inst> mp;
36 vector<inst> v;
37 int main(){
38     scanf("%d",&n);
39     for(int i=0;i<n;i++){
40         string id,ins;
41         int score,tw=0;
42         cin>>id>>score>>ins;
43         transform(ins.begin(), ins.end(), ins.begin(), ::tolower);
44         if(mp.find(ins)==mp.end()){
45             inst tmp;
46             tmp.insti=ins;
47             tmp.ns=1;
48             //tmp.tws=tw;
49             if(id[0]=='B'){
50                 tmp.score[0]=score;
51             }
52             else if(id[0]=='A'){
53                 tmp.score[1]=score;
54             }
55             else{
56                 tmp.score[2]=score;
57             }
58             mp[ins]=tmp;
59         }
60         else{
61             inst tmp=mp[ins];
62             tmp.ns++;
63             if(id[0]=='B'){
64                 tmp.score[0]+=score;
65             }
66             else if(id[0]=='A'){
67                 tmp.score[1]+=score;
68             }
69             else{
70                 tmp.score[2]+=score;
71             }
72             mp[ins]=tmp;
73         }
74     }
75     printf("%d\n",mp.size());
76     int rank=1;
77     for(auto it=mp.begin();it!=mp.end();it++){
78         inst tmp=it->second;
79         tmp.tws=tmp.score[0]/1.5+tmp.score[1]+tmp.score[2]*1.5;
80         v.push_back(tmp);
81     }
82     sort(v.begin(),v.end());
83     printf("%d %s %d %d\n",rank,v[0].insti.c_str(),v[0].tws,v[0].ns);
84     for(int i=1;i<v.size();i++){
85         if(v[i].tws!=v[i-1].tws) rank=i+1;
86         printf("%d %s %d %d\n",rank,v[i].insti.c_str(),v[i].tws,v[i].ns);
87     }
88 }
View Code

注意点:排序题有sort的帮助还是比较简单的,虽然还是花了很久,调试了很久。还用到了map和vector,最开始想直接map里面排序,还写了结构体重载函数,发现map是根据第一个键值来排序的,那只好再保存到vector里,反正也要遍历一遍算tws。重载了<号就不用写cmp函数了。因为set和map自动排序是根据小于号判断两个结构体的大小,小的放前面,sort 排序不写cmp函数也是自动根据小于号有小到大排列

第一个坑是最开始每读一个分数看他哪个等级就更新 tws,这样最后一个测试点答案错误,以为要用float,重新看了遍题,他是取整数部分,但不是每一个来取,而是对所有对应等级的总和取整,这就还需要保存一个总和值最后一起来算

第二个坑是结构体里面保存总和的数组没有显式初始化为0,算出来答案一直错误,需要手动初始化为0,就AC了

猜你喜欢

转载自www.cnblogs.com/tccbj/p/10419286.html