PAT A1153 Decode Registration Card of PAT [硬核模拟]

题目描述

链接
给出一组学生的准考证号和成绩,准考证号包含了等级(乙甲顶),考场号,日期,和个人编号信息,并有三种查询方式
查询一:给出考试等级,找出该等级的考生,按照成绩降序,准考证升序排序
查询二:给出考场号,统计该考场的考生数量和总得分
查询三:给出考试日期,查询改日期下所有考场的考试人数,按照人数降序,考场号升序排序

分析

  • 查询一和查询二都是可以直接遍历做到的!!!直接遍历,不用存中间状态!!!否则用map超时
  • 查询三用unordered_map
  • 注意!!不需要提前把所有都提取出来,变成string-->map<>这样,反而更慢,直接来一个查询,保存一个结果,再查多好!!!
  • 当然非要用这个时,注意先建个tmp,再把值赋给最外层的map
  • 可以用string.c_str()将string变成char*,可以用printf,不用cout,从而更快
#include<bits/stdc++.h>
using namespace std;

unordered_map<string, unordered_map<int, int> > t3;

struct node{
    string s;
    int num;
};

int n,m;
vector<node> a;

bool cmp1(node &x, node &y){
    if(x.num == y.num) return x.s<y.s;
    return x.num > y.num;
}

bool cmp2(pair<int,int> &x, pair<int,int> &y){
    if(x.second == y.second) return x.first < y.first;
    return x.first > y.first;
}

void solve(){
    for(int j=0;j<n;j++){
        string date = a[j].s.substr(4,6);
        string site = a[j].s.substr(1,3);
        if(t3.find(date) == t3.end()){
            unordered_map<int,int> tmp; //建一个临时的!!!
            t3[date] = tmp;
        }
        t3[date][stoi(site)]++;
    }
}

int main(){

    scanf("%d%d",&n,&m);
    a.resize(n);
    for(int i=0;i<n;i++){
        cin>>a[i].s>>a[i].num;
    }
    sort(a.begin(),a.end(),cmp1);
    solve();
    for(int i=1;i<=m;i++){
        int type;
        string s;
        cin>>type>>s;
        cout<<"Case "<<i<<": "<<type<<" "<<s<<endl;
        if(type == 1){
            bool flag = 0;
            for(int j=0;j<n;j++){
                if(a[j].s.substr(0,1) == s){
                    cout<<a[j].s<<" "<<a[j].num<<endl;
                    flag = 1;
                }
            }
            if(!flag) printf("NA\n");
        }else if(type == 2){
            bool flag = 0;
            int cnt = 0, sum = 0;
            for(int j=0;j<n;j++){
                if(a[j].s.substr(1,3) == s){
                    cnt++;
                    sum += a[j].num;
                    flag = 1;
                }
            }
            if(flag) printf("%d %d\n", cnt, sum);
            else printf("NA\n");
        }else if(type == 3){
            if(t3.find(s) == t3.end()){
                cout<<"NA"<<endl;
                continue;
            }
            //unordered_map 更快!
            unordered_map<int, int> tmp = t3[s];
            vector<pair<int,int> > ans;
            for(auto it=tmp.begin(); it!=tmp.end();it++){
                ans.push_back(*it);
            }
            sort(ans.begin(),ans.end(),cmp2);
            for(int j=0;j<ans.size();j++){
                //使用c_str()将字符串转为char*,避免使用cout超时
                cout<<ans[j].first<<" "<<ans[j].second<<endl;
            }
        }
    }
}

好代码

#include <iostream>
#include <vector>
#include <unordered_map>
#include <algorithm>
using namespace std;
struct node {
    string t;
    int value;
};
bool cmp(const node &a, const node &b) {
    return a.value != b.value ? a.value > b.value : a.t < b.t;
}
int main() {
    int n, k, num;
    string s;
    cin >> n >> k;
    vector<node> v(n);
    for (int i = 0; i < n; i++)
        cin >> v[i].t >> v[i].value;
    for (int i = 1; i <= k; i++) {
        cin >> num >> s;
        printf("Case %d: %d %s\n", i, num, s.c_str());
        vector<node> ans;
        int cnt = 0, sum = 0;
        if (num == 1) {
            for (int j = 0; j < n; j++)
                if (v[j].t[0] == s[0]) ans.push_back(v[j]);
        } else if (num == 2) {
            for (int j = 0; j < n; j++) {
                if (v[j].t.substr(1, 3) == s) {
                    cnt++;
                    sum += v[j].value;
                }
            }
            if (cnt != 0) printf("%d %d\n", cnt, sum);
        } else if (num == 3) {
            unordered_map<string, int> m;
            for (int j = 0; j < n; j++)
                if (v[j].t.substr(4, 6) == s) m[v[j].t.substr(1, 3)]++;
            for (auto it : m) ans.push_back({it.first, it.second});
        }
        sort(ans.begin(), ans.end(),cmp);
        for (int j = 0; j < ans.size(); j++)
            printf("%s %d\n", ans[j].t.c_str(), ans[j].value);
        if (((num == 1 || num == 3) && ans.size() == 0) || (num == 2 && cnt == 0)) printf("NA\n");
    }
    return 0;
}

原文地址:https://www.cnblogs.com/doragd/p/11460794.html

时间: 2024-07-30 15:29:20

PAT A1153 Decode Registration Card of 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, 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

PAT Advanced 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, 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

1153.Decode Registration Card of PAT(unordered_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 gi

1153 Decode Registration Card of PAT

这道题一开始一直超时,后来看了大神的发现要用unordered_map,然后还是不行,把cout改成printf,居然过了...时间真的掐得紧 #include <iostream> #include <stdlib.h> #include <string> #include<algorithm> #include<vector> #include<cmath> #include<map> #include <uno

PAT A1105 Spiral Matrix [硬核模拟]

题目描述 链接 将给定的N个正整数按非递增的顺序,填入"螺旋矩阵"~所谓"螺旋矩阵",是指从左上角第1个格子开始,按顺时针螺旋方向填充~要求矩阵的规模为m行n列,满足条件:m*n等于N:m>=n:且m-n取所有可能值中的最小值 分析 方法1:就是按照题意模拟,我自己想到一个切换方向的方法,但是必须初始化的时候要注意! 方法2:while里面套while.内层while有4个,组成了一个整体,即走了一圈,外层while表示不断走圈,直到走完 一直最后一个样例错!

1093. Count PAT&#39;s (25)【计数】——PAT (Advanced Level) Practise

题目信息 1093. Count PAT's (25) 时间限制120 ms 内存限制65536 kB 代码长度限制16000 B The string APPAPT contains two PAT's as substrings. The first one is formed by the 2nd, the 4th, and the 6th characters, and the second one is formed by the 3rd, the 4th, and the 6th c

嵌入ARM硬核的FPGA

目前,在FPGA上嵌入ARM硬核的包括Xilinx的zynq系列以及Intel 的CYCLONEV系列. Zynq出来有一定市场,但是这个市场不是传统FPGA的主流市场,而是为了和微处理抢一些控制领域的市场.但是目前在反响上,听说,不如预期,首先对小公司来说,同时熟悉ARM和FPGA的人不多,在大公司来说,由于他们面向的市场对专业分工要求更高,一般都使用FPGA+CPU的方式,对这类Zynq这种新鲜事物还是持保留态度的,观望的比较多. 但随着技术及工具的发展,zynq的使用越来越受到许多公司青睐

如何成为云中硬核“牧羊人”?云堡垒机服务高效运维,让云主机不再成为落单的小羊!

企业运维场景难点,自检你中招了哪些?? 企业运维账号众多企业运维的服务器数量众多,而维护人员数量有限,一个运维人员维护多台主机.多个系统的现象普遍存在.因此,运维人员不仅管理的机器账号密码多种多样,而且需要同时在多套主机系统之间切换.这种情况大大增加运维人员工作量,导致运维效率低下.易出错.影响IT系统正常运行. ? 权限分配粗放,缺乏细粒度企业运维授权一般是采用操作系统自身的授权系统,授权系统功能分散在各个设备和系统中,导致缺乏统一的运维操作授权策略:授权颗粒度粗,无法基于最小权限分配原则管理

[转帖]程序员需要了解的硬核知识之磁盘

https://www.cnblogs.com/cxuanBlog/p/11776310.html 此篇文章是 <程序员需要了解的硬核知识>系列第四篇,历史文章请戳 程序员需要了解的硬核知识之内存 程序员需要了解的硬核知识之CPU 程序员需要了解的硬核知识之二进制 我们大家知道,计算机的五大基础部件是 存储器.控制器.运算器.输入和输出设备,其中从存储功能的角度来看,可以把存储器分为内存和 磁盘,内存我们上面的文章已经介绍过了,那么此篇文章我们来介绍一下磁盘以及内存和磁盘的关系. 认识磁盘 首