1095 解码PAT准考证/1153 Decode Registration Card of PAT(25 分)

PAT 准考证号由 4 部分组成:

  • 第 1 位是级别,即 T 代表顶级;A 代表甲级;B 代表乙级;
  • 第 2~4 位是考场编号,范围从 101 到 999;
  • 第 5~10 位是考试日期,格式为年、月、日顺次各占 2 位;
  • 最后 11~13 位是考生编号,范围从 000 到 999。

现给定一系列考生的准考证号和他们的成绩,请你按照要求输出各种统计信息。

输入格式:

输入首先在一行中给出两个正整数 N(≤10​4​​)和 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(优化技巧)的更多相关文章

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

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

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

  4. 1153 Decode Registration Card of PAT

    A registration card number of PAT consists of 4 parts: the 1st letter represents the test level, nam ...

  5. PAT_A1153#Decode Registration Card of PAT

    Source: PAT A1153 Decode Registration Card of PAT (25 分) Description: A registration card number of ...

  6. PAT-1153(Decode Registration Card of PAT)+unordered_map的使用+vector的使用+sort条件排序的使用

    Decode Registration Card of PAT PAT-1153 这里需要注意题目的规模,并不需要一开始就存储好所有的满足题意的信息 这里必须使用unordered_map否则会超时 ...

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

  8. PAT Basic 1095 解码PAT准考证 (25 分)

    PAT 准考证号由 4 部分组成: 第 1 位是级别,即 T 代表顶级:A 代表甲级:B 代表乙级: 第 2~4 位是考场编号,范围从 101 到 999: 第 5~10 位是考试日期,格式为年.月. ...

  9. PAT甲级1095. Cars on Campus

    PAT甲级1095. Cars on Campus 题意: 浙江大学有6个校区和很多门.从每个门口,我们可以收集穿过大门的汽车的进/出时间和车牌号码.现在有了所有的信息,你应该在任何特定的时间点告诉在 ...

随机推荐

  1. New Year and Counting Cards

    Your friend has n cards. You know that each card has a lowercase English letter on one side and a di ...

  2. 利用PIL添加水印

    转:http://www.jb51.net/article/66542.htm

  3. redis的no-appendfsync-on-rewrite参数

    redis提供了两种持久化机制,rdb和aof. 关于aof的原理,类似于预写日志,不再解释.其中几个选项如下: appendfsync always:总是写入aof文件,并完成磁盘同步appendf ...

  4. L2-023. 图着色问题(暴力)

    L2-023. 图着色问题 时间限制 300 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 图着色问题是一个著名的NP完全问题.给定无向图 G ...

  5. 设置windows10 背景颜色

    [Win + R ] regedit 打开注册表 HKEY_CURRENT_USER\Control Panel\Colors 1.[InfoWindow] 默认为(白色):255 255 255, ...

  6. rails登录后跳转到登录前的路径

    # 重定向到存储的地址或默认地址 def redirect_back_or(default) redirect_to(session[:forwarding_url] || default) sess ...

  7. Resque基本

    原文:http://www.cnblogs.com/rywx/archive/2012/05/26/2519615.html Resque resque是基于redis的后台任务组件,能把任何类或模块 ...

  8. [转]在 Windows 操作系统中的已知安全标识符(Sid security identifiers)

    安全标识符 (SID) 是用于标识安全主体或安全组在 Windows 操作系统中的可变长度的唯一值.常用 Sid 的 Sid 标识普通用户的一组或通用组.跨所有操作系统,它们的值保持不变. 此信息可用 ...

  9. “百度杯”CTF比赛 2017 二月场(Misc Web)

    爆破-1: 打开链接,是502 我直接在后面加个变量传参数:?a=1 出了一段代码 var_dump()函数中,用了$$a,可能用了超全局变量GLOBALS 给hello参数传个GLOBALS 得到f ...

  10. DAY3-python函数

    目录 一.了解函数 二. 函数定义 三.函数使用原则:先定义,后调用 四.定义函数的三种形式 五.函数的调用 六.函数的参数 七. 函数对象 八.函数嵌套 九.名称空间与作用域 十. 闭包函数 十一. ...