p1:一开始写的是cout,发现最后两个测试超时,最后换成printf就好了
p2:map是自动排序的,耗时多,unordered_map是不排序的
/*
类型1:输出指定等级的考生,按分数从大到小,相同则字母序。
类型2:输出指定考场的人数和总分。
类型3:输出指定日期的所有考场编号和人数,按照人数递减,人数同则按照考场编号递增
*/
#include<stdio.h>
#include<string>
#include<iostream>
#include<algorithm>
#include<unordered_map>
#include<vector>
using namespace std;
struct node{
string s;
int value;
};
bool cmp(node a, node b){
if(a.value != b.value)
return a.value > b.value;
else
return a.s < b.s;
}
int main() {
int n, m, type;
string str;
cin >> n >> m;
vector<node> v(n);
for(int i=0; i<n; ++i)
cin >> v[i].s >> v[i].value;
for(int i=1; i<=m; ++i) {
cin >> type >> str;
printf("Case %d: %d %s\n", i, type, str.c_str());
vector<node> ans;
int cnt = 0, sum = 0;
if(type == 1){
for(int j=0; j<n; ++j)
if(v[j].s[0] == str[0])
ans.push_back(v[j]);
} else if(type == 2){
for(int j=0; j<n; ++j){
if(v[j].s.substr(1, 3) == str) {
cnt++;
sum += v[j].value;
}
}
if(cnt != 0)
printf("%d %d\n", cnt, sum);
} else if(type == 3) {
unordered_map<string, int> mp;
for(int j=0; j<n; ++j) {
if(v[j].s.substr(4, 6) == str) {
mp[v[j].s.substr(1, 3)]++;
}
}
for(auto it : mp)
ans.push_back({it.first, it.second});
}
if(ans.size() > 0) {
sort(ans.begin(), ans.end(), cmp);
for(int j=0; j<ans.size(); ++j)
printf("%s %d\n", ans[j].s.c_str(), ans[j].value);
}
if((type == 1 || type == 3) && ans.size() == 0 || type == 2 && cnt == 0 )
printf("NA\n");
}
return 0;
}