哈希表
#include <iostream>
#include <unordered_map>
#include <algorithm>
#include <vector>
using namespace std;
const int maxn=1e4+10;
struct person{
string id;
int score;
int cnt;
vector<int> s;
int rank;
bool operator < (const person& a) const{
if(score==a.score&&cnt==a.cnt)
return id<a.id;
else if(score==a.score)
return cnt>a.cnt;
else return score>a.score;
}
};
unordered_map<string,vector<int>> mp;
vector<person> v;
int q[10];
int main(){
int n,k,m;
scanf("%d%d%d",&n,&k,&m);
for(int i=0;i<k;i++) scanf("%d",&q[i]);
char str[10];
int num,score;
while(m--){
scanf("%s%d%d",str,&num,&score);
num--;
string id(str);
if(!mp.count(id)) mp.insert({id,vector<int>(10,-2)});
auto &a=mp[id];
a[num]=max(score,a[num]);
}
for(auto &it:mp){
auto& a=it.second;
int cnt=0,sum=0;
int flag=0;
for(int i=0;i<a.size();i++){
if(a[i]!=-1&&a[i]!=-2) sum+=a[i];
if(a[i]==q[i]) cnt++;
if(a[i]>=0) flag=1;
}
if(flag) v.push_back({it.first,sum,cnt,a});
}
sort(v.begin(),v.end());
for(int i=0;i<v.size();i++){
if(!i||v[i].score!=v[i-1].score) v[i].rank=i+1;
else v[i].rank=v[i-1].rank;
}
for(auto &x:v){
printf("%d %s %d",x.rank,x.id.c_str(),x.score);
for(int i=0;i<k;i++){
if(x.s[i]==-2) printf(" -");
else if(x.s[i]==-1) printf(" 0");
else printf(" %d",x.s[i]);
}
puts("");
}
return 0;
}
TLE了哥们
写得好