PAT甲 1095 解码PAT准考证/1153 Decode Registration Card of PAT(优化技巧)
PAT 准考证号由 4 部分组成:
- 第 1 位是级别,即
T代表顶级;A代表甲级;B代表乙级; - 第 2~4 位是考场编号,范围从 101 到 999;
- 第 5~10 位是考试日期,格式为年、月、日顺次各占 2 位;
- 最后 11~13 位是考生编号,范围从 000 到 999。
现给定一系列考生的准考证号和他们的成绩,请你按照要求输出各种统计信息。
输入格式:
输入首先在一行中给出两个正整数 N(≤104)和 M(≤100),分别为考生人数和统计要求的个数。
接下来 N 行,每行给出一个考生的准考证号和其分数(在区间 [0,100] 内的整数),其间以空格分隔。
考生信息之后,再给出 M 行,每行给出一个统计要求,格式为:类型 指令,其中
类型为 1 表示要求按分数非升序输出某个指定级别的考生的成绩,对应的指令则给出代表指定级别的字母;类型为 2 表示要求将某指定考场的考生人数和总分统计输出,对应的指令则给出指定考场的编号;类型为 3 表示要求将某指定日期的考生人数分考场统计输出,对应的指令则给出指定日期,格式与准考证上日期相同。
输出格式:
对每项统计要求,首先在一行中输出 Case #: 要求,其中 # 是该项要求的编号,从 1 开始;要求 即复制输入给出的要求。随后输出相应的统计结果:
类型为 1 的指令,输出格式与输入的考生信息格式相同,即准考证号 成绩。对于分数并列的考生,按其准考证号的字典序递增输出(题目保证无重复准考证号);类型为 2 的指令,按人数 总分的格式输出;类型为 3 的指令,输出按人数非递增顺序,格式为考场编号 总人数。若人数并列则按考场编号递增顺序输出。
如果查询结果为空,则输出 NA。
输入样例:
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
输出样例:
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需要以下几个优化小技巧。
1.自定义排序引用传参较快
2.string使用printf输出,string.c_str()
3.使用unordered_map(key未排序)优于map,较低版本编译器可能会出现编译错误(c++11)
ps:也可以使用数组变量记录,map虽然只会遍历大于0的key,但效率远不及数组变量
#include<bits/stdc++.h>
using namespace std;
typedef long long ll; struct Node{
string num;
int sco;
}a[],b[]; bool cmp(const Node &a,const Node &b){ //引用传参
if(a.sco==b.sco) return a.num<b.num;
return a.sco>b.sco;
}
int main()
{
int n,m,i,j;
scanf("%d%d",&n,&m);
for(i=;i<=n;i++){
cin>>a[i].num>>a[i].sco;
}
int tt=;
int x;
string y;
while(m--){
tt++;
cin>>x>>y;
printf("Case %d: %d %s\n",tt,x,y.c_str()); //printf输出string
int c=,ans=;
unordered_map<string,int> bb; //c++11版,优于map
for(i=;i<=n;i++){
if(x==){
if(a[i].num[]==y[]){
c++;
b[c].num=a[i].num;
b[c].sco=a[i].sco;
}
}
else if(x==){
if(a[i].num.substr(,)==y){
c++;
ans+=a[i].sco;
}
}
else{
if(a[i].num.substr(,)!=y) continue;
bb[a[i].num.substr(,)]++;
}
}
if(x==){
if(c==) printf("NA\n");
else{
sort(b+,b+c+,cmp);
for(i=;i<=c;i++){
printf("%s %d\n",b[i].num.c_str(),b[i].sco); //
}
}
}
else if(x==){
if(c==) printf("NA\n");
else printf("%d %d\n",c,ans);
}
else{
unordered_map<string,int>::iterator it;
for(it=bb.begin();it!=bb.end();it++){ //只遍历value不为0的key
c++;
b[c].num=it->first;
b[c].sco=it->second;
}
if(c==) printf("NA\n");
else{
sort(b+,b+c+,cmp);
for(i=;i<=c;i++){
printf("%s %d\n",b[i].num.c_str(),b[i].sco); //
}
}
}
}
return ;
}
PAT甲 1095 解码PAT准考证/1153 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, nam ...
- 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, nam ...
- 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, nam ...
- 1153 Decode Registration Card of PAT
A registration card number of PAT consists of 4 parts: the 1st letter represents the test level, nam ...
- PAT_A1153#Decode Registration Card of PAT
Source: PAT A1153 Decode Registration Card of PAT (25 分) Description: A registration card number of ...
- PAT-1153(Decode Registration Card of PAT)+unordered_map的使用+vector的使用+sort条件排序的使用
Decode Registration Card of PAT PAT-1153 这里需要注意题目的规模,并不需要一开始就存储好所有的满足题意的信息 这里必须使用unordered_map否则会超时 ...
- 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, nam ...
- PAT Basic 1095 解码PAT准考证 (25 分)
PAT 准考证号由 4 部分组成: 第 1 位是级别,即 T 代表顶级:A 代表甲级:B 代表乙级: 第 2~4 位是考场编号,范围从 101 到 999: 第 5~10 位是考试日期,格式为年.月. ...
- PAT甲级1095. Cars on Campus
PAT甲级1095. Cars on Campus 题意: 浙江大学有6个校区和很多门.从每个门口,我们可以收集穿过大门的汽车的进/出时间和车牌号码.现在有了所有的信息,你应该在任何特定的时间点告诉在 ...
随机推荐
- 大鱼吃小鱼(运用stack的模拟)
个人心得:这一题在暑假集训的周测里做到过,当时就死模拟,然后卡了很久很久才做对.现在发现运用stack其实非常方便, 将向左向右游动的鱼分开,则往后走只要往右移动的就放入stack,往左的时候就与st ...
- 【Linux网络编程】基于TCP流 I/O多路转接(poll) 的高性能http服务器
服务器比较简陋,为了学习poll的使用,只向客户端回写一条html语句.启动服务器后,浏览器发起请求,服务端向浏览器写回html,响应字符串,然后可以看到,浏览器解析并显示 Hello Poll!. ...
- L2-014. 列车调度(set的使用,最长递增子序列)
L2-014. 列车调度 时间限制 300 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 火车站的列车调度铁轨的结构如下图所示. Figure ...
- Java-API-Package:org.springframework.beans.factory.annotation
ylbtech-Java-API-Package:org.springframework.beans.factory.annotation 1.返回顶部 1. @NonNullApi @NonNull ...
- c# 遍历目录
public static List<string> TraverseDirector(string dir, bool isTraveSubDirFlag, bool isFilterS ...
- 【OpenCV】图像代数运算:平均值去噪,减去背景
代数运算,就是对两幅图像的点之间进行加.减.乘.除的运算.四种运算相应的公式为: 代数运算中比较常用的是图像相加和相减.图像相加常用来求平均值去除addtive噪声或者实现二次曝光(double-ex ...
- rails表单控件helper
1.form加入HTML属性 <%= form_for(@device, :html => {:method=>"post", :id=>"for ...
- Python函数(二)-参数传递
位置参数 根据位置顺序来传递参数 # -*- coding:utf-8 -*- __author__ = "MuT6 Sch01aR" def test(a,b): #a和b为形参 ...
- JavaScript基本概念B - 关于方法
方法也是对象 这个事需要反复强调.方法是 类型 Function 的对象,和其他对象一样,它也有方法. function gen() { return function ans(factor) { r ...
- 深入理解Java虚拟机—JVM内存结构
1.概述 jvm内存分为线程共享区和线程独占区,线程独占区主要包括虚拟机栈.本地方法栈.程序计数器:线程共享区包括堆和方法区 2.线程独占区 虚拟机栈 虚拟机栈描述的是java方法执行的动态内存模型, ...