PAT甲级刷题记录——1153 Decode Registration Card of PAT (25分)

A registration card number of PAT consists of 4 parts:

  • the 1st letter represents the test level, namely, Tfor the top level, Afor advance and Bfor 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 (≤10​4​​ ) 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

  • Typebeing 1 means to output all the testees on a given level, in non-increasing order of their scores. The corresponding Termwill be the letter which specifies the level;
  • Typebeing 2 means to output the total number of testees together with their total scores in a given site. The corresponding Termwill then be the site number;
  • Typebeing 3 means to output the total number of testees of every site for a given test date. The corresponding Termwill 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 inputis 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 Nswhere Ntis the total number of testees and Nsis their total score;
  • for a type 3 query, output in the format Site Ntwhere Siteis the site number and Ntis 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:

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:

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

思路

这题其实算是一个比较常规的排序题,但是时间卡得很紧(两个排序函数写出来基本上这题就能AC了,但是超时问题解决起来很麻烦……)。

题目大意就是给N行信息,每行有这个学生的准考证号和成绩。然后接下来是M行查询信息,其中第一个数字是查询的类型(这种方式和【1022 Digital Library (30分)】有点像),然后后面跟的是查询的信息。

  • 如果查询的编号是1,那么后面跟的是PAT考试的等级,于是输出这个等级所有考生的信息(准考证号+成绩),其中,按成绩降序排列,如果成绩相同,按准考证号升序排列;
  • 如果查询的编号是2,那么后面跟的是考场号,于是输出这个考场里学生的总人数+成绩总和
  • 如果查询的编号是3,那么后面跟的是日期,于是输出这个日期里考场号+这个考场的学生人数,并且优先按学生人数降序排列,如果学生人数相同,那么按照考场号升序排列。

解决方法还是很常规的,就不多说了,这里说一下解决超时的问题:

  • 关闭cin和cout的同步:ios::sync_with_stdio(false);
  • 在排序函数cmp()中使用引用传参:bool cmp(const info &a, const info &b)这个习惯很重要,我就是在这一直卡在最后一个测试点超时,全部改成引用之后就AC了);
  • unordered_map代替map
  • scanfprintf代替cincout输出(但是有使用string的题目最好都用cin和cout,不然混合使用会显得很另类……);
  • ‘\n’代替endl

代码

#include<cstdio>
#include<stdlib.h>
#include<algorithm>
#include<vector>
#include<unordered_map>
#include<string>
#include<string.h>
#include<iostream>
using namespace std;
const int maxn = 10010;
struct info{
    string card_number;
    string level;
    string testSite_number;
    string date;
    string testee_number;
    int score;
    info(){
        score = 0;
    }
}m[maxn];
struct iinfo{
    int testSite_number;
    int Nt;
    iinfo(){
        Nt = 0;
    }
};
bool cmp(const info &a, const info &b){
    if(a.score!=b.score) return a.score>b.score;
    else return a.card_number<b.card_number;
}
bool cmp2(const iinfo &a, const iinfo &b){
    if(a.Nt!=b.Nt) return a.Nt>b.Nt;
    else return a.testSite_number<b.testSite_number;
}
int main()
{
    ios::sync_with_stdio(false);
    int N, M;
    cin>>N>>M;
    cin.ignore();
    for(int i=0;i<N;i++){
        string tmpID;
        int tmpScore;
        cin>>tmpID>>tmpScore;
        cin.ignore();
        char tmpLevel = tmpID[0];
        string tmpTestSite = tmpID.substr(1, 3);
        string tmpDate = tmpID.substr(4, 6);
        string tmpTesteeNumber = tmpID.substr(10, 3);
        m[i].card_number = tmpID;
        m[i].level = tmpLevel;
        m[i].testSite_number = tmpTestSite;
        m[i].date = tmpDate;
        m[i].testee_number = tmpTesteeNumber;
        m[i].score = tmpScore;
    }
    for(int i=1;i<=M;i++){
        string tmpQuery;
        getline(cin, tmpQuery);
        char type = tmpQuery[0];
        string tmpInfo = tmpQuery.substr(2);
        bool flag = false;
        cout<<"Case "<<i<<": "<<tmpQuery<<'\n';
        if(type=='1'){
            vector<info> List;
            for(int j=0;j<N;j++){
                if(m[j].level==tmpInfo){
                    List.push_back(m[j]);
                    flag = true;
                }
            }
            if(flag==true){
                sort(List.begin(), List.end(), cmp);
                for(int j=0;j<List.size();j++){
                    cout<<List[j].card_number<<" "<<List[j].score<<'\n';
                }
            }
            else cout<<"NA\n";
        }
        else if(type=='2'){
            int Nt = 0, Ns = 0;
            for(int j=0;j<N;j++){
                if(m[j].testSite_number==tmpInfo){
                    Nt++;
                    Ns += m[j].score;
                    flag = true;
                }
            }
            if(flag==true){
                cout<<Nt<<" "<<Ns<<'\n';
            }
            else cout<<"NA\n";
        }
        else if(type=='3'){
            unordered_map<int, int> site;
            vector<iinfo> List;
            for(int j=0;j<N;j++){
                if(m[j].date==tmpInfo){
                    site[stoi(m[j].testSite_number)]++;
                    flag = true;
                }
            }
            if(flag==true){
                for(unordered_map<int, int>::iterator itt=site.begin();itt!=site.end();itt++){
                    iinfo tmp;
                    tmp.testSite_number = itt->first;
                    tmp.Nt = itt->second;
                    List.push_back(tmp);
                }
                sort(List.begin(), List.end(), cmp2);
                for(int j=0;j<List.size();j++){
                    cout<<List[j].testSite_number<<" "<<List[j].Nt<<'\n';
                }
            }
            else cout<<"NA\n";
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值