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 (≤), 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
题意:
给出一组PAT考生的信息,通过分析这些信息输出考生所在学校的排名。
思路:
构造一个结构体,然后输入,排序,输出。(详细过程键代码)最后一组数据卡到了,看了大佬的代码,发现TWS取整的时候是最后取整,如果在过程中取整的话最后一组数据不能通过。
code:
1 #include <algorithm> 2 #include <iostream> 3 #include <map> 4 #include <string> 5 #include <vector> 6 7 using namespace std; 8 9 struct School { 10 int Rank; 11 string school; 12 double TWS; 13 int Ns; 14 }; 15 16 bool cmp(School s1, School s2) { 17 if (s1.TWS != s2.TWS) 18 return s1.TWS > s2.TWS; 19 else if (s1.Ns != s2.Ns) 20 return s1.school < s2.school; 21 else 22 return s1.Ns < s2.Ns; 23 } 24 25 string toLower(string str) { 26 for (int i = 0; i < str.length(); ++i) str[i] = tolower(str[i]); 27 return str; 28 } 29 30 int main() { 31 int n; 32 cin >> n; 33 getchar(); 34 string inf; 35 map<string, School> m; 36 map<char, double> s; 37 s['B'] = 0.66666667; 38 s['A'] = 1.0; 39 s['T'] = 1.5; 40 for (int i = 0; i < n; ++i) { 41 getline(cin, inf); 42 string ID = inf.substr(0, 6); 43 int pos = 7; 44 while (inf[pos] != ' ') pos++; 45 int score = stoi(inf.substr(7, pos - 7)); 46 string school = toLower(inf.substr(pos + 1)); 47 m[school].school = school; 48 m[school].TWS += score * s[ID[0]]; 49 m[school].Ns += 1; 50 } 51 vector<School> v; 52 for (auto it : m) { 53 it.second.TWS = (int)it.second.TWS; 54 v.push_back(it.second); 55 } 56 sort(v.begin(), v.end(), cmp); 57 int rank = 1; 58 v[0].Rank = rank; 59 for (int i = 1; i < v.size(); ++i) { 60 rank++; 61 if (v[i].TWS == v[i - 1].TWS) 62 v[i].Rank = v[i - 1].Rank; 63 else 64 v[i].Rank = rank; 65 } 66 cout << v.size() << endl; 67 for (int i = 0; i < v.size(); ++i) { 68 cout << v[i].Rank << " " << v[i].school << " " << v[i].TWS << " " 69 << v[i].Ns << endl; 70 } 71 return 0; 72 }
优化了一下cmp函数的写法。
PAT应该是加强了测试点的精确度,上一次AC的代码这次值得了5分
重新写了一下代码最后一组数据还是被卡到了。
1 #include <bits/stdc++.h> 2 3 using namespace std; 4 5 struct Institution { 6 string school; 7 double TWS; 8 int Ns; 9 }; 10 11 string _tolower(string s) { 12 for (int i = 0; i < s.length(); ++i) { 13 s[i] = tolower(s[i]); 14 } 15 return s; 16 } 17 18 bool cmp(Institution a, Institution b) { 19 if (a.TWS != b.TWS) { 20 return a.TWS > b.TWS; 21 } else if (a.Ns != b.Ns) { 22 return a.Ns < b.Ns; 23 } else { 24 return a.school < b.school; 25 } 26 } 27 28 int main() { 29 int n; 30 cin >> n; 31 map<string, Institution> mp; 32 string id, school; 33 double score; 34 for (int i = 0; i < n; ++i) { 35 cin >> id >> score >> school; 36 school = _tolower(school); 37 mp[school].Ns++; 38 if (id[0] == 'B') { 39 mp[school].TWS += score * 0.66666667; 40 } else if (id[0] == 'A') { 41 mp[school].TWS += score; 42 } else { 43 mp[school].TWS += score * 1.5; 44 } 45 } 46 vector<Institution> v; 47 for (auto it : mp) { 48 it.second.school = it.first; 49 v.push_back(it.second); 50 } 51 sort(v.begin(), v.end(), cmp); 52 cout << v.size() << endl; 53 int rank = 1; 54 cout << rank << " " << v[0].school << " " << (int)v[0].TWS << " " << v[0].Ns 55 << endl; 56 for (int i = 1; i < v.size(); ++i) { 57 if ((int)v[i].TWS == (int)v[i - 1].TWS) { 58 cout << rank << " " << v[i].school << " " << (int)v[i].TWS << " " 59 << v[i].Ns << endl; 60 } else { 61 rank = i + 1; 62 cout << rank << " " << v[i].school << " " << (int)v[i].TWS << " " 63 << v[i].Ns << endl; 64 } 65 } 66 return 0; 67 }
排序前需要对TWS进行取整不然最后一组数据不能通过。