1153 Decode Registration Card of PAT(map排序 + 解决超时问题)

A registration card number of PAT consists of 4 parts:

  • the 1st letter represents the test level, namely, T for the top level, A for advance and B for basic;
  • the 2nd - 4th digits are the test site number, ranged from 101 to 999;
  • the 5th - 10th digits give the test date, in the form of yymmdd;
  • finally the 11th - 13th digits are the testee's number, ranged from 000 to 999.

Now given a set of registration card numbers and the scores of the card owners, you are supposed to output the various statistics according to the given queries.

Input Specification:

Each input file contains one test case. For each case, the first line gives two positive integers N (104) and M (100), the numbers of cards and the queries, respectively.

Then N lines follow, each gives a card number and the owner's score (integer in [0,100]), separated by a space.

After the info of testees, there are M lines, each gives a query in the format Type Term, where

  • Type being 1 means to output all the testees on a given level, in non-increasing order of their scores. The corresponding Term will be the letter which specifies the level;
  • Type being 2 means to output the total number of testees together with their total scores in a given site. The corresponding Term will then be the site number;
  • Type being 3 means to output the total number of testees of every site for a given test date. The corresponding Term will then be the date, given in the same format as in the registration card.

Output Specification:

For each query, first print in a line Case #: input, where # is the index of the query case, starting from 1; and input is a copy of the corresponding input query. Then output as requested:

  • for a type 1 query, the output format is the same as in input, that is, CardNumber Score. If there is a tie of the scores, output in increasing alphabetical order of their card numbers (uniqueness of the card numbers is guaranteed);
  • for a type 2 query, output in the format Nt Ns where Nt is the total number of testees and Ns is their total score;
  • for a type 3 query, output in the format Site Nt where Site is the site number and Nt is the total number of testees at Site. The output must be in non-increasing order of Nt's, or in increasing order of site numbers if there is a tie of Nt.

If the result of a query is empty, simply print NA.

Sample Input:

1
2
3
4
5
6
7
8
9
10
11
12
13
8 4
B123180908127 99
B102180908003 86
A112180318002 98
T107150310127 62
A107180908108 100
T123180908010 78
B112160918035 88
A107180908021 98
1 A
2 107
3 180908
2 999

 

Sample Output:

1
2
3
4
5
6
7
8
9
10
11
12
Case 1: 1 A
A107180908108 100
A107180908021 98
A112180318002 98
Case 2: 2 107
3 260
Case 3: 3 180908
107 2
123 2
102 1
Case 4: 2 999
NA

 

思路:

1、map排序问题:

对unordered_map同时根据key和value的值来排序,需要引入pair,然后将map中的值复制到vector,对vector进行排序

具体使用方法如下:

bool cmp1(const pair<string, int> &p1, const pair<string, int>&p2){
    if(p1.second != p2.second){
        return p1.second > p2.second;
    }
    if(p1.first != p2.first){
        return p1.first <= p2.first;
    }
}

 

复制代码
unordered_map<string, int>mp;
vector<pair<string, int>> vec;
for (auto it = mp.begin(); it != mp.end(); it++){
    vec.push_back(make_pair(it->first, it->second));
}
sort(vec.begin(), vec.end(), cmp1)
 for(auto it = vec.begin(); it != vec.end(); it++){
   cout<<it->first;
   printf(" %d\n", it->second);
}
复制代码

 

2、代码优化(解决超时问题)

1)排序传参尽量用引用(&),会快一点

2)尽量使用scanf/printf来输入输出,避免使用cin/cout

 

使用cin/cout

 

使用scanf/printf

 

 

代码:

 

复制代码
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<vector>
#include<unordered_map>
using namespace std;
struct testee{
    string num;
    int score;
}a[10005];
bool cmp(testee &t1, testee &t2){
    if(t1.score != t2.score){
        return t1.score > t2.score;
    }
    if(t1.num != t2.num){
        return t1.num < t2.num;
    }
}
bool cmp1(const pair<string, int> &p1, const pair<string, int>&p2){
    if(p1.second != p2.second){
        return p1.second > p2.second;
    }
    if(p1.first != p2.first){
        return p1.first <= p2.first;
    }
}
int main(){
    int n,m,score,cnt = 0;
    string num;
    scanf("%d%d",&n, &m);
    for(int i=0; i<n; i++){
        cin>>a[i].num;
        scanf("%d",&a[i].score);
    }
    sort(a, a+n, cmp);
    int type;
    string term;
    for(int i=0; i<m; i++){
        bool flag = false;
        scanf("%d",&type);
        cin>>term;
        if(type == 1){
            int c = 0;
            for(int j=0; j<n; j++){
                if(a[j].num[0] == term[0]){
                    flag = true;
                    if(c == 0){
                        printf("Case %d: %d ",++cnt, type);
                        cout<<term<<endl;
                        c++;
                    }
                    cout<<a[j].num;
                    printf(" %d\n",a[j].score);
                }
            }
        }else if(type == 2){
            int num = 0, total = 0;
            for(int j=0; j<n; j++){
                if(a[j].num.substr(1,3) == term){
                    total += a[j].score;
                    num++;
                }
            }
            if(num > 0){
                printf("Case %d: %d ",++cnt, type);
                cout<<term<<endl;
                printf("%d %d\n",num, total);
                flag = true;
            }
        }else if(type == 3){
            unordered_map<string, int>mp;
            for(int j=0; j<n; j++){
                if(a[j].num.substr(4,6) == term){
                    mp[a[j].num.substr(1,3)] ++;
                    flag = true;
                }
            }
            vector<pair<string, int>> vec;
            for (auto it = mp.begin(); it != mp.end(); it++){
                vec.push_back(make_pair(it->first, it->second));
            }
            sort(vec.begin(), vec.end(), cmp1);
            if(flag == true){
                printf("Case %d: %d ",++cnt, type);
                cout<<term<<endl;
                for(auto it = vec.begin(); it != vec.end(); it++){
                    cout<<it->first;
                    printf(" %d\n", it->second);
                    
                }
            }
        }
        if(flag == false){
            printf("Case %d: %d ",++cnt, type);
            cout<<term<<endl;
            printf("NA\n");
        }  
    }
}
复制代码

 

  

posted @   Yohoc  阅读(21)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 使用C#创建一个MCP客户端
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示