1153 Decode Registration Card of PAT (25分)

  • 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.

1、首先,PAT card字符串的一些信息用substr来截取

2、对于三种要求,就分三种情况处理。

type1要求由等级对应信息,输出先按分数再按考号排序。排序

type2要求由考场号统计出来考场人数和总分数。统计

type3要求由日期对应当天的考场,并按考场人数和考场号排序。排序加统计

我们既可以通过unordered_map<string,vector<Node>> t1,t2,t3;在一开始的时候就push_back,最后再排序和统计。

也可以按照type单独push到ans里。然后再做统一处理。

注:时间限制比较小,那么把map改成unordered_map,排序传参用引用传参

注:string只能用cin cout 但是因为输出时间可以用c_str()然后printf

 
#include<bits/stdc++.h>
using namespace std;
struct Node{
    string all;
    int score;
}temp;
unordered_map<string,vector<Node>> t1,t2,t3;
struct Site{
    string id;
    int num;
}site;
bool cmp1(Node a,Node b){
    if(a.score!=b.score) return a.score>b.score;
    else return a.all<b.all;
}
bool cmp2(Site a,Site b){
    if(a.num!=b.num) return a.num>b.num;
    else return a.id<b.id;
}
int main(){
    int n,m;
    scanf("%d %d",&n,&m);
    for(int i=0;i<n;i++){
        cin>>temp.all>>temp.score;
        t1[temp.all.substr(0,1)].push_back(temp);
        t2[temp.all.substr(1,3)].push_back(temp);
        t3[temp.all.substr(4,6)].push_back(temp);
    }
    for(int i=1;i<=m;i++){
        int x;string a;
        cin>>x>>a;
        printf("Case %d: %d %s\n",i,x,a.c_str());
        if(x==1){
            if(t1[a].size()==0) printf("NA\n");
            else{
                sort(t1[a].begin(),t1[a].end(),cmp1);
                for(int j=0;j<t1[a].size();j++){
                    printf("%s %d\n",t1[a][j].all.c_str(),t1[a][j].score);
                }
            }
        }else if(x==2){
            if(t2[a].size()==0) printf("NA\n");
            else{
                int sum=0;
                for(int j=0;j<t2[a].size();j++){
                    sum+=t2[a][j].score;
                }
                printf("%d %d\n",t2[a].size(),sum);
            }
        }else{
            if(t3[a].size()==0) printf("NA\n");
            else{
                vector<Site> v;
                unordered_map<string,int> mp;
                for(int j=0;j<t3[a].size();j++){
                    string b=t3[a][j].all.substr(1,3);
                    mp[b]++;
                }
                for(auto m:mp){
                    site.id=m.first;
                    site.num=m.second;
                    v.push_back(site);
                }
                sort(v.begin(),v.end(),cmp2);
                for(int j=0;j<v.size();j++){
                    printf("%s %d\n",v[j].id.c_str(),v[j].num);
                }
            }
        }
    }
    return 0;
}
#include<bits/stdc++.h>
using namespace std;
struct node{
    string all;
    int val;
}a[10010];
bool cmp(node &a,node &b){
    if(a.val!=b.val) return a.val>b.val;
    else return a.all<b.all;
}
vector<node> ans;
int main(){
    int n,m;
    scanf("%d %d",&n,&m);
    for(int i=0;i<n;i++){
		cin>>a[i].all>>a[i].val; 
	} 
    for(int j=1;j<=m;j++){
        int type;string s;
        ans.clear();
        cin>>type>>s;
        printf("Case %d: %d %s\n",j,type,s.c_str());
		int cnt=0; 
        if(type==1){
            for(int i=0;i<n;i++){
                if(a[i].all.substr(0,1)==s) ans.push_back(a[i]);
            }
        }else if(type==2){
            int sum=0;
            for(int i=0;i<n;i++){
                if(a[i].all.substr(1,3)==s){
                    cnt++;sum+=a[i].val;
                }
            }
            if(cnt!=0) printf("%d %d\n",cnt,sum);
        }else{
            unordered_map<string,int> temp;
            for(int i=0;i<n;i++){
                if(a[i].all.substr(4,6)==s) temp[a[i].all.substr(1,3)]++;
            }
            for(auto mp:temp){
                ans.push_back(node{mp.first,mp.second});
            }
        }
        sort(ans.begin(),ans.end(),cmp);
        for(int i=0;i<ans.size();i++){
            printf("%s %d\n",ans[i].all.c_str(),ans[i].val);
        }
        if(type==2&&cnt==0||type!=2&&ans.size()==0) printf("NA\n");
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值