PAT甲级 1153 Decode Registration Card of PAT (25 分)

p1:一开始写的是cout,发现最后两个测试超时,最后换成printf就好了
p2:map是自动排序的,耗时多,unordered_map是不排序的


/*
类型1:输出指定等级的考生,按分数从大到小,相同则字母序。
类型2:输出指定考场的人数和总分。
类型3:输出指定日期的所有考场编号和人数,按照人数递减,人数同则按照考场编号递增
*/

#include<stdio.h>
#include<string>
#include<iostream>
#include<algorithm>
#include<unordered_map>
#include<vector>
using namespace std;

struct node{
    string s;
    int value;
};

bool cmp(node a, node b){
    if(a.value != b.value)
        return a.value > b.value;
    else
        return a.s < b.s;
}

int main() {
    int n, m, type;
    string str;
    cin >> n >> m;

    vector<node> v(n);
    for(int i=0; i<n; ++i)
        cin >> v[i].s >> v[i].value;

    for(int i=1; i<=m; ++i) {
        cin >> type >> str;
        printf("Case %d: %d %s\n", i, type, str.c_str());

        vector<node> ans;
        int cnt = 0, sum = 0;
        if(type == 1){
            for(int j=0; j<n; ++j)
                if(v[j].s[0] == str[0])
                    ans.push_back(v[j]);
        } else if(type == 2){
            for(int j=0; j<n; ++j){
                if(v[j].s.substr(1, 3) == str) {
                    cnt++;
                    sum += v[j].value;
                }
            }
            if(cnt != 0)
                printf("%d %d\n", cnt, sum);
        } else if(type == 3) {
            unordered_map<string, int> mp;
            for(int j=0; j<n; ++j) {
                if(v[j].s.substr(4, 6) == str) {
                    mp[v[j].s.substr(1, 3)]++;
                }
            }
            for(auto it : mp)
                ans.push_back({it.first, it.second});
        }

        if(ans.size() > 0) {
            sort(ans.begin(), ans.end(), cmp);
            for(int j=0; j<ans.size(); ++j)
                printf("%s %d\n", ans[j].s.c_str(), ans[j].value);
        }
        if((type == 1 || type == 3) && ans.size() == 0 || type == 2 && cnt == 0 )
            printf("NA\n");
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值