1153 Decode Registration Card of PAT (25 分)

排序问题
坑点如下
1,比较函数的传参尽量用引用传参。
2,单作为映射可以用unordered_map,内部元素是无序的。
代码如下

#include<cstdio>
#include<cstring>
#include<iostream>
#include<unordered_map>
#include<vector>
#include<algorithm>
using namespace std;
struct Node{
    string id;
    int score,site;
}node;
struct Node1{
    int si,num;
    Node1(int a,int b){
        si=a;
        num=b;
    }
};
unordered_map<string,vector<Node>>m1,m2,m3;
bool cmp1(const Node &a,const Node &b){
  return   a.site<b.site;
}
bool cmp3(const Node &a,const Node &b){
  return   a.score!=b.score?a.score>b.score:a.id<b.id;
}
bool cmp4(const Node1 &a,const Node1 &b){
  return   a.num!=b.num?a.num>b.num:a.si<b.si;
}
int main(){
    int n,m,t,total;
    string l;
    cin>>n>>m;
    for(int i=0;i<n;i++){
       cin>>node.id;
        scanf("%d",&node.score);
       node.site=stoi(node.id.substr(1,3));
       m1[node.id.substr(0,1)].push_back(node);
       m2[node.id.substr(1,3)].push_back(node);
       m3[node.id.substr(4,6)].push_back(node);
    }
    for(int i=1;i<=m;i++){
     cin>>t>>l;
     printf("Case %d: %d %s\n",i,t,l.c_str());
     if(t==1){
         if(m1[l].size()==0)printf("NA\n");
         else{
         sort(m1[l].begin(),m1[l].end(),cmp3);
         for(int b=0;b<m1[l].size();b++)
             printf("%s %d\n",m1[l][b].id.c_str(),m1[l][b].score); 
    }
     }
    else if(t==2){
        if(m2[l].size()==0)printf("NA\n");
         else{total=0;
           for(int b=0;b<m2[l].size();b++)
           total+=m2[l][b].score;
           printf("%d %d\n",int(m2[l].size()),total);
    }}
    else if(t==3){
   if(m3[l].size()==0)printf("NA\n");
    else {vector<Node1>k;
          int num=1,now;
          sort(m3[l].begin(),m3[l].end(),cmp1);
          now=m3[l][0].site;
          for(int b=1;b<m3[l].size();b++){
             if(m3[l][b].site==now)num++;
             else{ k.push_back(Node1(now,num));
                  num=1;
                  now=m3[l][b].site;}
          }
          k.push_back(Node1(now,num));
          sort(k.begin(),k.end(),cmp4);
          for(int b=0;b<k.size();b++)
              printf("%d %d\n",k[b].si,k[b].num);
    }
    }
        
}
    return 0;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值