1141. PAT Ranking of Institutions

PAT Ranking of Institutions

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行列表,每一行为"编号,成绩,学校",编号第一位代表该成绩属于"A,B,T",要求统计信息后,按照优先级输出所有学校的"排名,学校名字,学校总加权分数,学校参考人数",其中要求学校名字统一为全小写。

总加权分数计算公式为:ScoreB/1.5 + ScoreA + ScoreT*1.5,结果取整数部分。

优先级依次为:一,总加权分数降序;二,参考人数升序;三,学校名字升序。

思路

直接按照题意处理,先用map记录对应学校的各项信息,再将数据转存到vector中,按照优先级排序后输出。

由于样例数据较大,要注意代码中下面的两个点会消耗大量时间:一,输出使用cout可能会超时;二,用map可能会超时,所以代码中使用unordered_map,并将按学校名字排序整合到sort中。


代码实现

#include <iostream>
#include <vector>
#include <unordered_map>
#include <string>
#include <algorithm>
#include <cctype>
using namespace std;

struct School
{
    string name;            // 学校名字
    int tws;                // 总加权分数
    int ns;                 // 参考人数
    int A, B, T;            // 分别记录三种考试的总成绩
    School(): A(0), B(0), T(0), tws(0), ns(0) {}        // 构造函数初始化
};

bool cmp(School a, School b)        // 按照优先级排序
{
    if (a.tws == b.tws && a.ns == b.ns)
        return a.name < b.name;
    else if (a.tws == b.tws)
        return a.ns < b.ns;
    else
        return a.tws > b.tws;
}

int main()
{
    int n;
    string id, school;
    int score;
    vector<School> ans;
    unordered_map<string, School> sch;

    cin >> n;
    for (int i = 0; i < n; i++)
    {
        cin >> id >> score >> school;

        for (int j = 0; j < school.length(); j++)   // 转换成全小写
            school[j] = tolower(school[j]);

        if (id[0] == 'A')
            sch[school].A += score;
        else if (id[0] == 'B')
            sch[school].B += score;
        else if (id[0] == 'T')
            sch[school].T += score;

        sch[school].ns++;
        sch[school].name = school;
    }

    unordered_map<string, School>::iterator it;
    for (it = sch.begin(); it != sch.end(); it++)       // 转存到vector中
        ans.push_back(it->second);

    for (int i = 0; i < ans.size(); i++)                // 计算出总加权分数
        ans[i].tws = (int) (1.0 * ans[i].A + 1.0 * ans[i].B / 1.5 + 1.0 * ans[i].T * 1.5);

    sort(ans.begin(), ans.end(), cmp);                  // 按照优先级排序

    cout << ans.size() << endl;

    int rank = 1, prev = ans[0].tws;
    for (int i = 0; i < ans.size(); i++)
    {
        // tws小于前个学校,更新排名,否则排名与前个学校相同
        if (ans[i].tws < prev)
        {
            prev = ans[i].tws;
            rank = i + 1;
        }
        cout << rank << " " << ans[i].name << " " << ans[i].tws << " " << ans[i].ns << endl;
    }

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值