版本1
这个题,怎么说呢?巨TM恶心不过分吧。
排序尽量用结构体吧,vector靠不住。今天不是type==3的时候,我偏要用vector保存pair类型来排序,不然早就过了。
还有就是字符转数字的时候没有减去字符0,导致直接算的ASCII码值。
#include <cstdio>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
#include <cstring>
#include <algorithm>
#include <unordered_map>
#include <map>
using namespace std;
const int N = 1e4+5;
typedef pair<int, int> PII;
unordered_map<string, PII> mp;
struct node{
string pat;
int score;
}E[N], A[N], B[N], T[N];
struct stu{
int site, num;
}X[N];
bool cmp(node &a, node &b){
if(a.score != b.score) return a.score > b.score;
else return a.pat < b.pat;
}
bool cmp1(stu a, stu b){
if(a.num != b.num) return a.num > b.num;
return a.site < b.site;
}
int main(){
int n, m, cnta=0, cntb=0, cntt=0;
string temp, str;
scanf("%d%d", &n, &m);
for(int i = 0; i < n; i++){
cin>>E[i].pat>>E[i].score;
temp = E[i].pat.substr(1,3);
mp[temp].second += E[i].score;
mp[temp].first++;
if(E[i].pat[0]=='A') A[cnta].pat = E[i].pat, A[cnta].score = E[i].score, cnta++;
else if(E[i].pat[0]=='B') B[cntb].pat = E[i].pat, B[cntb].score = E[i].score, cntb++;
else T[cntt].pat = E[i].pat, T[cntt].score = E[i].score, cntt++;
}
int type;
char C;
string site, date;
for(int i = 1; i <= m; i++){
scanf("%d", &type);
getchar();
if(type == 1){
cin>>C;
printf("Case %d: %d %c\n",i,type, C);
if(C=='A') {
if(cnta == 0){
puts("NA");
continue;
}
sort(A, A + cnta, cmp);
for(int j = 0; j < cnta; j++){
cout<<A[j].pat<<" "<<A[j].score<<endl;
}
}else if(C == 'B'){
if(cntb == 0){
puts("NA");
continue;
}
sort(B,B+cntb, cmp);
for(int j = 0; j < cntb; j++){
cout<<B[j].pat<<" "<<B[j].score<<endl;
}
}else{
if(cntt == 0){
puts("NA");
continue;
}
sort(T,T+cntt, cmp);
for(int j = 0; j < cntt; j++){
cout<<T[j].pat<<" "<<T[j].score<<endl;
}
}
}else if(type == 2){
cin>>site;
printf("Case %d: %d %s\n",i,type, site.c_str());
if(mp.count(site)) printf("%d %d\n", mp[site].first, mp[site].second);
else puts("NA");
}else{
cin>>date;
printf("Case %d: %d %s\n",i,type, date.c_str());
unordered_map<int, int> has;
for(int v = 0; v < n; v++){
temp = E[v].pat.substr(4, 6);
if(temp == date){
int t = (E[v].pat[1] - '0')*100+(E[v].pat[2] - '0')*10+E[v].pat[3]-'0';
has[t]++;
}
}
if(!has.size()) puts("NA");
else{
int id = 0;
for(auto it: has){
X[id].site = it.first;
X[id].num = it.second;
id++;
}
sort(X, X+id,cmp1);
for(int z = 0; z < id; z++){
cout<<X[z].site<<" "<< X[z].num<<endl;
}
}
}
}
return 0;
}