爆刷PAT(甲级)——之【1153】 Decode Registration Card of PAT (25 分)——模拟细节题(如何解决超时问题!)

数字IC后端设计实现之Innovus POD V2 Flow性能优化全解析 本文深入解析了数字IC后端设计中的Innovus POD V2 Flow性能优化方案。该流程通过整合place_opt_design与clock_opt_design命令,实现了布局与时钟树的协同优化,有效提升了时序收敛效率、改善了设计密度与拥塞,并显著减少了迭代次数,是应对先进工艺复杂设计的关键利器。 阅读详情

此题是去年12月份甲级PAT没有拿到全分的题目。当时最后两个点没有过,所以最可能拿满分的一次机会白白流失掉了。

当初最后两个点是因为超时,一时半会没想到如何去优化。回来以后也没有花时间再去解决这个TLE问题。

昨天晚上突然想到这题,抽时间写了一下,花了2个小时还是最后两个点没有过,百思不得其解。

今天解决了这个问题,觉得有毒。

网上各大博客都没有着重点出如何解决这个TLE(超时)问题,也很有可能是因为我太久没有做题忘记了这些做题常识等等原因吧。遂在此记录一下此题之外,兴许能够帮到同在苦恼第三个点总是过不了的同学人们吧。

 

题意:N个卡号(N最大10e4)M次询问(M最大10e2)。对于每个卡号,由级别(首字母)、地区号(三位数)、日期(六位数)、用户id(3位数)(没用到)的信息组成。然后给出M个询问,询问分成三种,第一种就是输出所有人的卡号和成绩(降序);第二种是输出指定地点的人数情况与总分;第三种是给出某日期,输出此日下,各个地点的考试人数(降序)。

 

思路:这么一看其实很明显,这题在考场上就是看你的细致模拟、编码速度。这些相信都不是问题。而解决问题,不管是那种询问,都遵循的是先遍历一遍题目给出的卡号清单,更具要求筛选出我们需要的卡号,将这些筛选出的人根据特定的排序要求进行排序并输出结果即可。

其中要注意的是,第一种询问和第三种询问都是需要按照一定优先级进行排序操作的——数据量是1W而查询量是100,讲道理肯定是不会超时的。但是在提交的时候却发生了超时情况是什么原因呢?

 

难点:

  1. 第一种询问和第二种询问撰写都比较简单,复杂度分别是O(NlogN)以及O(N)也不会有太大问题。但第三种询问,需要将所有卡号根据查询给出的日期,将此日卡号挑出并根据对应的地址进行归类!如此一来,多数人会想到用map来建立“地址映射”,但这样就会TLE。仔细看,题目给出的程序运行时间限制为200ms,而一般的题目是1000ms。map的底层是由红黑树实现的,红黑树作为一棵树的查询复杂度就是(logN),以及要具备红黑树的特性其时空会带来额外的消耗。如果用map进行映射,整道题的复杂度虽然是O(N+M(NlogN+NlogN+N))(读取数据、M次查询(地址查询、排序、输出))但是却会超时。所以,第一点就是要用unordered_map来代替map。unordered_map也是用于映射,但底层由哈希实现,单次查询速度为O(1),可以将时间降低为O(N+M(1+NlogN+N)).
  2. 然鹅我本来就是用unordered_map也超时了?,第三个数据点死活过不了!第四个数据点要靠运气,有时候150ms有时候197ms有时候过不了,但是柳神等人的代码第四个数据点是50ms,比我快了三倍!!!看了诸多博客,完全get不到自己为什么会超时。第一点就是大部分博客都会提到的地方。一度怀疑是因为vector的size、string流、cin、局部变量回收过程、switch比不过if等奇奇怪怪的效率问题,已经神经质了......突然瞄到柳神以及其他人AC的代码中,在最后的O(N)的输出环节中,用的并不是cout而是printf!!!!!搞了半天不是cin的速度慢是cout的输出慢,猪脑子居然没想到!!!改了这个地方直接就A了。。。记住了,cout居然比printf输出慢了三倍!

代码上嘛我写的一般般,没有柳神那么优秀。。。在准备PAT的时候,一些基本的库函数比如stoi(string转int)、string的成员函数c_str(string转char数组)、unordered_map(常用的map的替代者)都要去看,都是基本操作,别忘了。

Code:

#include<bits/stdc++.h>
#include<unordered_map>
using namespace std;
#define inf 10009
#define INF 0x3f3f3f3f
#define loop(x,y,z) for(x=y;x<z;x++)

int n,m;
struct Info
{
    string card;
    int value;
    string level;
    string site;
    string date;
    void set(string s,int v)
    {
        value=v;
        card=s;
        level=s.substr(0,1);
        site=s.substr(1,3);
        date=s.substr(4,6);
    }
}info[inf];

void readData()
{
    int i,j;
    int value;
    string s;
    loop(i,0,n)
    {
        cin>>s>>value;
        info[i].set(s,value);
    }
}

bool cmp1(const int& i,const int &j)//ÌáÎÊ1µÄÅÅÐò
{
    if(info[i].value==info[j].value)return info[i].card<info[j].card;
    return info[i].value>info[j].value;
}

struct Type3
{
    string site;
    int num;
    Type3(string s)
    {
        site=s;
        num=1;
    }
    bool operator<(const Type3&t)const
    {
        if(num==t.num)return site<t.site;
        return num>t.num;
    }
};
int main()
{
    int i,j,k;
    cin>>n>>m;
    readData();//¶ÁÈ¡

    loop(k,1,m+1)
    {
        int type;
        cin>>type;
        switch(type)
        {
        case 1:{
            string level;
            cin>>level;
            vector<int>cache;
            cache.clear();
            loop(i,0,n)
                if(info[i].level==level)cache.push_back(i);
            sort(cache.begin(),cache.end(),cmp1);
            //print
            cout<<"Case "<<k<<": "<<type<<' '<<level<<endl;
            if(!cache.size())cout<<"NA"<<endl;
            else{
                int len=cache.size();
                loop(i,0,len)
                     printf("%s %d\n",info[cache[i]].card.c_str(),info[cache[i]].value);//改成这个才不会超市
                    //cout<<info[cache[i]].card<<' '<<info[cache[i]].value<<endl;
            }
            break;
            }
        case 2:{
            string site;
            cin>>site;
            int num=0;
            int sum=0;
            loop(i,0,n)
                if(info[i].site==site)
                {
                    num++;
                    sum+=info[i].value;
                }
            //print
            cout<<"Case "<<k<<": "<<type<<' '<<site<<endl;
            if(num)
                cout<<num<<' '<<sum<<endl;
            else
                cout<<"NA"<<endl;
            break;
            }
        case 3:{
            string date;
            cin>>date;
            vector<Type3>cache;
            unordered_map<string,int>mp;
            loop(i,0,n)
                if(info[i].date==date)
                {
                    string &site=info[i].site;
                    if(mp.count(site))cache[mp[site]].num++;
                    else {
                        mp[site]=cache.size();
                        Type3 t(site);
                        cache.push_back(t);
                    }
                }
            sort(cache.begin(),cache.end());
            //print
            cout<<"Case "<<k<<": "<<type<<' '<<date<<endl;
            if(!cache.size())cout<<"NA"<<endl;
            else{
                int len=cache.size();
                loop(i,0,len)
                    printf("%s %d\n",cache[i].site.c_str(),cache[i].num);//改成这个才不会超市
                    //cout<<cache[i].site<<' '<<cache[i].num<<endl;
            }
            break;
        }
        default:break;
        }
    }
    return 0;
}

 

信贷系统学习总结(6)—— 贷前、贷中、贷后全流程核心模型解析 本文系统解析了金融风控模型的技术演进与应用实践。从贷前、贷中到贷后全流程,详细介绍了A卡、B卡、C卡三类核心评分模型,以及反欺诈、动态监控、催收优化等关键技术的实现路径。重点阐述了特征工程设计、模型验证指标、联邦学习等创新应用,并提供了多个实战案例数据。同时提出了算法选择的"黄金三角"原则和分阶段技术演进路线,强调数据治理、模型全生命周期管理和复合型团队建设的重要性。文章指出未来风控将向决策智能化、数据无感化和风险预知化方向发展,需要平衡技术创新与合规要求。 阅读详情

相关推荐

基于YOLOv8和PaddlepaddleOCR的微信截图聊天记录提取

本文提出了一种基于YOLOv8和PaddleOCR的微信聊天记录提取方法。首先使用YOLOv8n模型分割出聊天气泡(针对深浅色模式分别训练),通过合成数据集增强模型鲁棒性,召回率达0.995。随后采用PP-OCRv5进行文本识别,利用坐标匹配将气泡内文本送入识别队列,短长文本平均置信度分别>0.98和>0.95。文中特别强调了PaddleOCR不同版本的输出格式差异及适配方案,建议使用3.0+版本。该方法有效解决了直接OCR识别易受干扰的问题,通过气泡分割+文本定位的两阶段策略显著提升了识别准确

2401_87383958的博客 2354

「1153」Decode Registration Card of PAT

A registration card number of PAT consists of 4 parts: the 1st letter represents the test level, namely,Tfor the top level,Afor advance andBfor basic; the 2nd - 4th digits are the test...

weixin_52435933的博客 361

Vue3多级路由缓存失效的深度解析与实战解决方案

本文深度解析了Vue3项目中多级路由下keep-alive缓存失效的根本原因,并提供了三种实战解决方案:通过路由扁平化根治问题、利用状态管理精细模拟缓存效果,以及借助keep-alive-vue3插件快速修复。文章旨在帮助开发者彻底理解Vue Router与keep-alive的协作机制,从而根据项目情况选择最佳策略,有效解决页面状态丢失的难题。

weixin_29015801的博客 285

PAT-A 1153 Decode Registration Card of PAT

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

JoJo's blog 486

FUXA实战:5步搞定基于Web的SCADA系统搭建(含Node.js版本避坑指南)

本文提供了一份基于开源工具FUXA搭建Web版SCADA系统的实战指南。文章重点解析了从环境准备到生产部署的五个核心步骤,并特别强调了Node.js版本选择对西门子S7 PLC通信兼容性的关键影响,帮助开发者避开常见陷阱,快速构建工业监控界面。

weixin_29172963的博客 140

oVirt 4.4.10超详细安装指南:从零搭建企业级虚拟化环境(附常见错误排查)

本文提供了oVirt 4.4.10企业级虚拟化平台的超详细安装指南,涵盖环境规划、系统准备、Engine安装配置、计算节点部署、存储系统集成及高级运维技巧。通过实战操作手册和常见错误排查方法,帮助用户从零搭建稳定高效的虚拟化环境,特别适合企业IT管理员参考实施。

wasm7browser的博客 351

1153 Decode Registration Card of PAT (PAT甲级)

这道题因为运行超时,卡了很久。

linh2006的博客 186

FPGA信号处理系列文章——分数倍插值抽取滤波器(一)

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档 FPGA信号处理系列文章——分数倍插值抽取滤波器(一)前言信号流matlab模型频谱示意图分析总结 前言 变采样率滤波器不仅仅存在整数倍插值和整数倍抽取的情况,还会存在分数倍插值抽取的情况。那这种情况下要怎么处理呢 信号流 分数倍插值抽取滤波器的信号流是什么?信号先进行插值,再进行FIR滤波,最后进行抽取 matlab模型 %signal 定点化数据输入 coeff_val_fir = importdata('fir.coe'); si

gzy0506的博客 1万+

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

柳婼 の blog 8455

★教程2:fpga学习教程入门100例目录【含程序操作讲解视频】

★基础入门 1.FPGA开发软件安装

FPGA/MATLAB学习教程/源码/项目合作开发 7万+

CasADi - 最优控制开源 Python/MATLAB 库

CasADi 是一款开源软件工具,用于数值优化,特别是最优控制(即涉及微分方程的优化)。该项目由 Joel Andersson 和 Joris Gillis 在鲁汶工程大学工程优化中心 (OPTEC) 在读博士生在 Moritz Diehl 的指导下发起。本文档旨在简要介绍 CasADi。

机器人控制与仿真应用 7932

【PAT】1153 Decode Registration Card of PAT (25 分)

题目大意:PAT考生号的第1个字母代表考生等级,第24个数字代表考试地点,第510个数字代表yymmdd格式的时间,第1113位表示考生数量(0999)。给出一些PAT考生号,根据查询输出信息: 按分数降序输出所给等级所有的考生分数,如果分数相同,按考生号以字母表顺序升序排序(即直接比较两个字符串) 输出所给地点所有的考生数量和他们的总分 输出所给日期里每个地点的所有考生数量,按考生数量降序排序,若考生数量相同,就按地点升序排序(即直接比较两个字符串) 思路: 用结构体数组(vector)记录..

SSibyl的博客 169

一张图看懂 ABAP CDS 两大子语言:CDS DDL 与 CDS DCL 以及缺席的 DML

ABAP CDS 的核心设计思想围绕语义数据模型(DDL)与数据访问控制(DCL)展开,而非单纯作为SQL查询工具。其DDL通过声明式语法定义业务导向的数据视图,强调模型稳定性与复用性;DCL则将访问控制逻辑下沉到模型层,实现行级权限过滤。ABAP CDS刻意剥离DML功能,交由ABAP SQL处理数据变更,形成清晰的分层架构。这种设计使数据定义、访问控制与应用逻辑解耦,为现代ABAP应用提供更安全、可维护的数据治理方案。

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 975

18冬第二题 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 andBfor basic; the 2nd - 4th digits are the test sit...

qiiingc的解题记录 3万+

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

pscad IEEE标准系统搭建说明

pscad IEEE标准系统搭建说明,包含典型的IEEE 9节点、14节点、30节点、39节点和118节点系统。内含标准测试系统的原始数据和潮流结果。

PAT 甲级 1153  Decode Registration Card of PAT

1153Decode Registration Card of PAT(25 point(s)) A registration card number of PAT consists of 4 parts: the 1st letter represents the test level, namely,Tfor the top level,Afor advance andBf...

漫浸天空的雨色的备忘录~ 1018

温度PID控制PLC程序

温度PID稳定控制程序,适用于欧姆龙PLC,对其他PLC也具有很大参考价值

PAT 甲级 1153 Decode Registration Card of PAT

PAT 甲级 1153 Decode Registration Card of PAT思路:注意点:代码: 思路: 1.暴力破解,直接进行遍历排序即可; 2.第三个type使用unordered_map比较方便; 注意点: 1.个别数据转换成int输出时要注意形式; 2.避免超时问题; 代码: #include<iostream> #include<algorithm> #...

Yuhan的博客 385

WINCC7.3破解文件

WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!

PAT 甲级 1153 Decode Registration Card of PAT (25分)

1153 Decode Registration Card of PAT

Droke_Zhou的博客 657

1153 Decode Registration Card of PAT——PAT甲级

TAByymmdd。

flyidj的博客 861

PAT甲级1153 Decode Registration Card of PAT (25 分)

1153Decode 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 andBfor basi...

Dexter 298

PAT甲级 1153 Decode Registration Card of PAT (25分) map/C++

1153 Decode Registration Card of PAT (25分) 题目大意:给定准考证号和成绩,按照下列查询方式查询: 给定level,找到该等级考试的所有学生,按照分数降序排序,相等则按准考证号升序排序; 给定site,找到site的所有成绩之和,并输出人数; 给定date,找到每个site的人数,按人数降序输出,相等则按site升序输出。 输入输出用scanf、printf,并且使用unordered_map代替map,否则会超时 自己写完以后,再看柳神的代码,还是一如既往的强

qq_43955154的博客 297

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

沧夜的成长日记 701

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

一只奋斗中的本科生 348

【PAT甲级A1153】Decode Registration Card of PAT (25分)(c++)

1153 Decode Registration Card of PAT (25分) 作者:CHEN, Yue 单位:浙江大学 代码长度限制:16 KB 时间限制:400 ms 内存限制:64 MB 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;

小庄同学的博客 491

PAT甲级A1153 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 nu...

vir_lee的博客 344

PAT 甲级1153 Decode Registration Card of PAT

1153 Decode Registration Card of PAT 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 2...

cyberickk blog 223

1153 Decode Registration Card of PAT (25point(s)) - C语言 PAT 甲级

* 排好序按要求输出

有三只土豆 691
上一篇: 爆刷PAT(甲级)——之【1151】 LCA in a Binary Tree (30 分)——先序中序建树+LCA问题
下一篇: 爆刷PAT(甲级)——之【1123】 Is It a Complete AVL Tree (30 分)——AVL树建树+层次遍历+完全二叉树的判断
仰天倀笑
博客等级 码龄10年 313粉丝 124原创
评论 4
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值