PAT甲级——A1141 PATRankingofInstitution

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

Solution:
  排序而已,善于使用unordermap!
 1 #include <iostream>
 2 #include <vector>
 3 #include <string>
 4 #include <unordered_map>
 5 #include <algorithm>
 6 using namespace std;
 7 struct Node
 8 {
 9     string id, school;
10     double tws;//切记,一定是double,每次PAT就是在这里下套
11     int score, nums, rank;
12 };
13 int main()
14 {
15     int n;
16     cin >> n;
17     vector<Node>v(n);
18     unordered_map<string, vector<int>>map;//记录相同学校的是哪些人
19     for (int i = 0; i < n; ++i)
20     {
21         string name, school;
22         int score;
23         cin >> name >> score >> school;
24         for (int j = 0; j < school.size(); ++j)
25             school[j] = tolower(school[j]);
26         v[i] = { name,school,0.0,score,1,0 };
27         map[school].push_back(i);
28     }
29     vector<Node>res;
30     for (auto ptr = map.begin(); ptr != map.end(); ++ptr)
31     {
32         vector<int>p = ptr->second;
33         res.push_back({ "", v[p[0]].school, 0.0, 0, (int)p.size(), 1 });//将相同学校的分数相加
34         for (auto a : p)
35         {
36             if (v[a].id[0] == 'A')
37                 res.back().tws += v[a].score;
38             else if (v[a].id[0] == 'B')
39                 res.back().tws += v[a].score / 1.5;
40             else
41                 res.back().tws += v[a].score * 1.5;
42         }
43     }
44     sort(res.begin(), res.end(), [](Node a, Node b) {//排名
45         if ((int(a.tws)) == (int(b.tws)) && a.nums == b.nums)
46             return a.school < b.school;
47         else if ((int(a.tws)) == (int(b.tws)))
48             return a.nums < b.nums;
49         else
50             return a.tws > b.tws;
51         });
52     cout << res.size() << endl;
53     for (int i = 0; i < res.size(); ++i)
54     {
55         if (i > 0 && ((int)res[i].tws) == ((int)res[i-1].tws))
56             res[i].rank = res[i - 1].rank;
57         else
58             res[i].rank = i + 1;
59         cout << res[i].rank << " " << res[i].school << " " << (int)res[i].tws << " " << res[i].nums << endl;
60     }
61     return 0;
62 }
原文地址:https://www.cnblogs.com/zzw1024/p/11893109.html