PAT 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 (<=10^5^), 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 "Score~B~/1.5 + Score~A~ + Score~T~*1.5", where "Score~X~" 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乙级最后一道题一样,不过当我将乙级的AC代码交上去时,发现最后一个测试点超时,后来看到别人说将Map换成unordered_map,试了一次,AC了,unordered_map不需要排序,所以效率会高点。涨姿势了。

代码:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <string>
#include <unordered_map>
using namespace std;
struct info
{
  string sch;
  int tot_score;
  int num;
  info(string sch = "", int tot_score = 0, int num = 0):sch(sch),tot_score(tot_score),num(num){}
};
bool cmp(info a,info b)
{
  if(a.tot_score != b.tot_score)
     return a.tot_score > b.tot_score;
  else if(a.num != b.num)
     return a.num < b.num;
  else
     return a.sch < b.sch;
}
int main()
{
  int N;
  cin>>N;
  unordered_map<string,int> SchToScoreA;
  unordered_map<string,int> SchToScoreB;
  unordered_map<string,int> SchToScoreT;
  unordered_map<string,int> SchToNum;
  while(N--)
  {
    string id;
    int score;
    string school;
    char ID[7];
    char s[20];
    scanf("%s %d %s",ID,&score,s);
    id = ID;
    school = s;
    for(int i = 0; i < school.size(); i++)
    {
      if(school[i] >= 'A' && school[i] <= 'Z')
         school[i] = school[i] - 'A' + 'a';
    }
    if(id[0] == 'A')
       SchToScoreA[school] += score;
    else if(id[0] == 'B')
       SchToScoreB[school] += score;
    else if(id[0] == 'T')
       SchToScoreT[school] += score;
    SchToNum[school]++;
  }
  vector<info> v;
  unordered_map<string,int>::iterator it = SchToNum.begin();
  for(; it != SchToNum.end(); it++)
  {
    string sch = it->first;
    int tot_score = SchToScoreB[it->first] / 1.5 + SchToScoreA[it->first] + SchToScoreT[it->first] * 1.5;
    v.push_back(info(sch,tot_score,it->second));
  }
  sort(v.begin(),v.end(),cmp);
  printf("%d\n",v.size());
  int rank = 1;
  printf("%d %s %d %d\n",rank,v[0].sch.c_str(),v[0].tot_score,v[0].num);
  for(int i = 1; i < v.size(); i++)
  {
    if(v[i].tot_score != v[i-1].tot_score)
        rank = i + 1;
    printf("%d %s %d %d\n",rank,v[i].sch.c_str(),v[i].tot_score,v[i].num);
  }
  return 0;
}

猜你喜欢

转载自blog.csdn.net/hickey_chen/article/details/80390966