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

题目链接

坑点(测试点4超时)

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

实现

#include 
#include 
#include 
#include 
#include 
#include 
#include 
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);
				}
			}
		
		}
	
	}
}



你可能感兴趣的:(PAT乙级)