题目链接
坑点(测试点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());
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);
}
}
}
}
}