1141. PAT Ranking of Institutions (25)

1141. PAT Ranking of Institutions (25)
时间限制 500 ms 内存限制 65536 kB 代码长度限制 16000 B
判题程序 Standard 作者 CHEN, Yue

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

mmp,在今年考pat的时候,这份代码一个测试点超时,,,,现在重新写了一遍可以过了!!!不过用c的输入输出确实快了不少
可能坑点:计算分数的时候,累加需要用double浮点数,用int的话,最后一个测试点不过,当然,用了double之后打印的时候,强转为int,不然最后一个测试点还是不过

#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <iostream>
#include <string>
#include <map>
const int Maxn = 100010;
using namespace std;

struct info {
    string institute;
    double scoreSum;
    int testees;
}Info[Maxn]; int n = 0;
map<string, info> Map;

void addTestee(info &data) { //引用传参减少时间
    auto it = Map.find(data.institute);
    if (it != Map.end()) {
        it->second.scoreSum += data.scoreSum;
        ++it->second.testees;
    }
    else {
        Map[data.institute] = data;
    }
}

bool cmp(info a, info b) {
    if (a.scoreSum != b.scoreSum) return a.scoreSum > b.scoreSum;
    if (a.testees != b.testees) return a.testees < b.testees;
    return a.institute < b.institute;
}

int main() {
#ifdef _DEBUG
    freopen("data.txt", "r+", stdin);
#endif // _DEBUG

    std::ios::sync_with_stdio(false);
    info tmp; int m; string id;
    tmp.testees = 1;
    //scanf("%d", &m);
    cin >> m;
    while (m--) {
        cin >> id >> tmp.scoreSum >> tmp.institute;
        if (id[0] == 'T') tmp.scoreSum *= 1.5;
        else if (id[0] == 'B')tmp.scoreSum /= 1.5;
        for (size_t i = 0; i < tmp.institute.size(); ++i) {
            if ('A' <= tmp.institute[i] && tmp.institute[i] <= 'Z') tmp.institute[i] += 32;
        }
        addTestee(tmp);
    }

    for (auto it = Map.begin(); it != Map.end(); ++it) {
        Info[n++] = it->second;
    }

    for (int i = 0; i < n; ++i)Info[i].scoreSum = (int)Info[i].scoreSum;
    sort(Info, Info + n, cmp);

    /*printf("%d\n", n);
    if (n > 0)printf("1 %s %d %d\n", Info[0].institute.c_str(), (int)Info[0].scoreSum, Info[0].testees);
    for (int rank = 1, i = 1; i < n; ++i) {
        if (Info[i].scoreSum != Info[i - 1].scoreSum) rank = i + 1;
        printf("%d %s %d %d\n", rank, Info[i].institute.c_str(), (int)Info[i].scoreSum, Info[i].testees);
    }*/
    cout << n << endl;
    if (n > 0)cout << 1 << " " << Info[0].institute << " " << (int)Info[0].scoreSum << " " << Info[0].testees << endl;
    for (int rank = 1, i = 1; i < n; ++i) {
        if (Info[i].scoreSum != Info[i - 1].scoreSum) rank = i + 1;
        cout << rank << " " << Info[i].institute << " " << (int)Info[i].scoreSum << " " << Info[i].testees << endl;
    }

    return 0;
}  

上述代码运行时间:
这里写图片描述
可以看到接近时间限制了,如果只用c++输入string的,其他的用C输入输出,快了很多:
C输出:

#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <iostream>
#include <string>
#include <map>
const int Maxn = 100010;
using namespace std;

struct info {
    string institute;
    double scoreSum;
    int testees;
}Info[Maxn]; int n = 0;
map<string, info> Map;

void addTestee(info &data) { //引用传参减少时间
    auto it = Map.find(data.institute);
    if (it != Map.end()) {
        it->second.scoreSum += data.scoreSum;
        ++it->second.testees;
    }
    else {
        Map[data.institute] = data;
    }
}

bool cmp(info a, info b) {
    if (a.scoreSum != b.scoreSum) return a.scoreSum > b.scoreSum;
    if (a.testees != b.testees) return a.testees < b.testees;
    return a.institute < b.institute;
}

int main() {
#ifdef _DEBUG
    freopen("data.txt", "r+", stdin);
#endif // _DEBUG

    info tmp; int m; string id;
    tmp.testees = 1;
    scanf("%d", &m);
    while (m--) {
        cin >> id >> tmp.scoreSum >> tmp.institute;
        if (id[0] == 'T') tmp.scoreSum *= 1.5;
        else if (id[0] == 'B')tmp.scoreSum /= 1.5;
        for (size_t i = 0; i < tmp.institute.size(); ++i) {
            if ('A' <= tmp.institute[i] && tmp.institute[i] <= 'Z') tmp.institute[i] += 32;
        }
        addTestee(tmp);
    }

    for (auto it = Map.begin(); it != Map.end(); ++it) {
        Info[n++] = it->second;
    }

    for (int i = 0; i < n; ++i)Info[i].scoreSum = (int)Info[i].scoreSum;
    sort(Info, Info + n, cmp);

    printf("%d\n", n);
    if (n > 0)printf("1 %s %d %d\n", Info[0].institute.c_str(), (int)Info[0].scoreSum, Info[0].testees);
    for (int rank = 1, i = 1; i < n; ++i) {
        if (Info[i].scoreSum != Info[i - 1].scoreSum) rank = i + 1;
        printf("%d %s %d %d\n", rank, Info[i].institute.c_str(), (int)Info[i].scoreSum, Info[i].testees);
    }

    return 0;
}  

执行时间:
这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值