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

题目链接

坑点(测试点4超时)

超时的话试一试将cin、cout换成scanf,printf
map换成unordered_map

实现

#include <iostream>
#include <string>
#include <algorithm>
#include <cstdio>
#include <vector>
#include <map>
#include <unordered_map>
using namespace std;
struct student {
	string id;
	int score;
};
bool cmp(student a, student b)
{
	return a.score != b.score ? a.score >= b.score : a.id<b.id;
}
bool cmp1(const pair<string, int> &a, const pair<string, int> &b)
{
	return a.second != b.second?a.second > b.second:a.first<b.first;
}
void outputCase1(vector<student> vec)
{
	if (vec.size() == 0)
		printf("NA\n");
	else
		for (vector<student>::iterator it = vec.begin(); it != vec.end(); it++)
			printf("%s %d\n", it->id.c_str(), it->score);
}
int main()
{
	int N, M, i;
	string stuId;
	int stuScore;
	vector<student> B, A, T;	//记录不同等级的学生
	map<string, int> roomNum, totalScore;	//记录考场对应的考生数 总分
	unordered_map<string, unordered_map<string, int>> dateRoomNum;	//日期对应的某考场的考生人数
	scanf("%d %d", &N, &M);
	for (i = 0; i < N; i++)
	{
		cin >> stuId;
		scanf("%d", &stuScore);
		string date = stuId.substr(4, 6);
		string roomid = stuId.substr(1, 3);
		if (stuId[0] == 'B')
			B.push_back(student{ stuId,stuScore });
		else if (stuId[0] == 'A')
			A.push_back(student{ stuId,stuScore });
		else
			T.push_back(student{ stuId,stuScore });
		roomNum[roomid]++;
		totalScore[roomid] += stuScore;
		dateRoomNum[date][roomid]++;
	}
	sort(A.begin(), A.end(), cmp);
	sort(B.begin(), B.end(), cmp);
	sort(T.begin(), T.end(), cmp);
	int temp1;
	string temp2;
	for (i = 0; i < M; i++)
	{
		scanf("%d", &temp1);
		cin >> temp2;
		printf("Case %d: %d %s\n", i + 1, temp1, temp2.c_str());
		if (temp1 == 1)
		{
			if (temp2 == "B")
				outputCase1(B);
			else if (temp2 == "A")
				outputCase1(A);
			else
				outputCase1(T);
		}
		else if (temp1 == 2)
		{
			if (roomNum[temp2] == 0)
				printf("NA\n");
			else
				printf("%d %d\n", roomNum[temp2], totalScore[temp2]);
		}
		else
		{
			unordered_map<string, int> curDateRoom = dateRoomNum[temp2];
			if (curDateRoom.size() == 0)
				printf("NA\n");
			else
			{
				vector<pair<string, int>> drNvec(curDateRoom.begin(), curDateRoom.end());	//对map不能直接使用sort,要转存到vector中进行排序
				sort(drNvec.begin(), drNvec.end(), cmp1);
				for (vector<pair<string, int>>::iterator it = drNvec.begin(); it != drNvec.end(); it++)
				{
					printf("%s %d\n", it->first.c_str(), it->second);
				}
			}
		
		}
	
	}
}



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值