PAT乙级1095 解码PAT准考证 (25 分)

 测试点1、4错误的话  检查你输出是不是3位或者6位 //printf("Case %d: %d %06d\n",i+1,zl,data);

#include <bits/stdc++.h>
using namespace std;
const int INF=100000000;
const int maxn=1111111;
struct st{
	string all;
	char rank;  //级别
	int room;  //考场日
	int data;  //日期
	string stuNo; //考生号
	int score; //分数
}stu[11111];
struct third{     //指令3专用结构体
	int room;   //考场号
	int sum;	//个数
	third(int r,int s):room(r),sum(s){}
};
bool cmp(st s1,st s2){
	if(s1.score!=s2.score){
		return s1.score>s2.score;
	}else return s1.all<s2.all;
}
bool cmp2(third m1,third m2){
	if(m1.sum!=m2.sum){
		return m1.sum>m2.sum;
	}else return m1.room<m2.room;
}
int a[1000];
int num[1000];
int main() {
	int i,n,m;
	scanf("%d%d",&n,&m);
	string s;
	int score;
	vector<st> v1,v2,v3;   //分别存顶级,甲级,乙级
	for(i=0;i<n;i++){
		cin>>s>>score;
		stu[i].all=s;
		stu[i].rank=s[0];
		stu[i].room=stoi(s.substr(1,3));
		stu[i].data=stoi(s.substr(4,6));
		stu[i].stuNo=s.substr(10,3);
		stu[i].score=score;
		if(stu[i].rank=='T'){
			v1.push_back(stu[i]);   
		}else if(stu[i].rank=='A'){
			v2.push_back(stu[i]);
		}else if(stu[i].rank=='B'){
			v3.push_back(stu[i]);
		}
		a[stu[i].room]+=stu[i].score;  //使用数组a和数组num,直接存入指令2所需要的数据
		num[stu[i].room]++;
 
	}
	sort(v1.begin(),v1.end(),cmp);
	sort(v2.begin(),v2.end(),cmp);
	sort(v3.begin(),v3.end(),cmp);
 
	int zl,room,j;
	char rank;
	int data;
	for(i=0;i<m;i++){
		scanf("%d",&zl);
		if(zl==1){
			cin>>rank;
			printf("Case %d: %d %c\n",i+1,zl,rank);
			if(rank=='T'){
				if(v1.empty()){printf("NA\n");continue;} 
				for(j=0;j<v1.size();j++){
					printf("%s %d\n",v1[j].all.c_str(),v1[j].score);
				}
			}else if(rank=='A'){
				if(v2.empty()){
					printf("NA\n");continue;
				}
				for(j=0;j<v2.size();j++){
					printf("%s %d\n",v2[j].all.c_str(),v2[j].score);
				}
			}else if(rank=='B'){
				if(v3.empty()){printf("NA\n");continue;}
				for(j=0;j<v3.size();j++){
					printf("%s %d\n",v3[j].all.c_str(),v3[j].score);
				}
			}
		}else if(zl==2){
			scanf("%d",&room);
			printf("Case %d: %d %03d\n",i+1,zl,room);
			if(num[room]==0){printf("NA\n");continue;}
			printf("%d %d\n",num[room],a[room]);
		}else if(zl==3){
			map<int,int> mp;
			vector<third> vm;
			scanf("%d",&data);
			printf("Case %d: %d %06d\n",i+1,zl,data);
			int flag=1;
			for(j=0;j<n;j++){
				if(stu[j].data==data){
					flag=0;
					mp[stu[j].room]++;
				}
			}
			if(flag) {printf("NA\n");continue;}
			for(auto i:mp)
				vm.push_back(third(i.first,i.second));
			sort(vm.begin(),vm.end(),cmp2);
			for(j=0;j<vm.size();j++){
				printf("%d %d\n",vm[j].room,vm[j].sum);
			}
		}
	}
	return 0;
}

 

### PAT 1095 解码准考证 测试点4 超时优化 针对PAT乙级真题中的 `1095 解码PAT准考证` 的测试点4超时问题,可以通过以下策略来优化算法性能。 #### 数据存储与处理优化 为了减少不必要的重复计算并提高效率,在数据读取阶段可以预先解析准考证号的信息,并将其类存储到合适的数据结构中。例如,对于不同类型的查询需求(按等级、考场号或日期),可别构建对应的哈希表或其他索引结构以便快速查找[^1]。 ```python from collections import defaultdict def preprocess(records): level_map = defaultdict(list) room_map = defaultdict(lambda: {'count': 0, 'total_score': 0}) date_map = defaultdict(dict) for record in records: id_, score = record.split() level, room_id, exam_date, _ = decode_pat_id(id_) # 构建level_map用于查询一 level_map[level].append((id_, int(score))) # 更room_map用于查询二 room_key = f"{level}-{room_id}" if room_key not in room_map: room_map[room_key]['students'] = [] room_map[room_key]['students'].append(int(score)) room_map[room_key]['count'] += 1 room_map[room_key]['total_score'] += int(score) # 构建date_map用于查询三 if exam_date not in date_map[level]: date_map[level][exam_date] = {} if room_id not in date_map[level][exam_date]: date_map[level][exam_date][room_id] = { 'count': 0, 'total_score': 0 } date_map[level][exam_date][room_id]['count'] += 1 date_map[level][exam_date][room_id]['total_score'] += int(score) return level_map, room_map, date_map ``` 上述代码展示了如何预处理输入记录以支持高效的多维度查询操作[^2]。 #### 查询逻辑调整 在实现具体查询功能时,应充利用已建立的映射关系而非重遍历原始数据集。这样能够显著降低时间复杂度至接近O(1)级别[^3]。 ##### 查询一:按等级筛选考生 当接收到指定考试级别的请求时,只需访问对应键值下的列表即可完成排序输出任务。 ```python def query_level(level_map, target_level): candidates = sorted( [(sid, sc) for sid, sc in level_map[target_level]], key=lambda x: (-x[1], x[0]) ) result = "\n".join([f"{cand[0]} {cand[1]}" for cand in candidates]) or "NA" return result ``` ##### 查询二:统计特定考场信息 利用事先准备好的房间字典可以直接获取所需统计数据。 ```python def query_room(room_map, target_room): prefix = "-".join(target_room[:2]) stats = room_map.get(prefix, {}) count = sum(len(stats['students'])) total_scores = sum(stats['students']) avg_score = round(total_scores / max(count, 1)) if count != 0 else 0 output_lines = [ str(count), str(total_scores), *[str(scr) for scr in sorted(stats['students'])], str(avg_score) ] return '\n'.join(output_lines) if count > 0 else "NA" ``` ##### 查询三:汇总某日各场次情况 依据先前整理过的日期关联资料执行相应运算流程。 ```python def query_date(date_map, target_date_info): lvl, dt = target_date_info rooms_data = list(date_map[lvl][dt].items()) ordered_rooms = sorted( rooms_data, key=lambda item: (-item[1]['count'], item[0]) ) lines = [" ".join([ rm_no, str(info['count']), str(info['total_score']) ]) for rm_no, info in ordered_rooms] final_output = "\n".join(lines) if lines else "NA" return final_output ``` 以上函数均基于前期精心设计的数据组织形式运作,从而有效规避了因逐条扫描带来的额外开销问题^.
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值