题意:测试者的id分为四个部分;
我们给出三种类型:第一种:按照分数从大到小排,当分数相同,按照id从小到大排;
第二种:求出符合类型二的id数量和分数总和;
第三种:求出符合类型三的id,和数量,按照数量从大到小排,数量相同,按照id从小到大排;
这里排序,最好用unorder_map,输出用printf,否则会超时;
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <unordered_map>
using namespace std;
struct testees{
string id;
int score;
}t[10001];
int N,M,type;
bool cmp(testees a,testees b){
if(a.score == b.score) return a.id < b.id;
else return a.score > b.score;
}
bool cmp_1(pair<string,int> a,pair<string,int>b){
if(a.second == b.second) return a.first < b.first;
else return a.second > b.second;
}
void deal(string s){
unordered_map<string,int> mp;
bool flag = false;
if(type == 1){
sort(t,t+N,cmp);
for(int i = 0; i < N; i++){
if(s == t[i].id.substr(0,1)){
printf("%s %d\n",t[i].id.c_str(),t[i].score);
flag = true;
}
}
}
else if(type == 2){
int sum = 0, count = 0;
for(int i = 0; i < N; i++){
if(s == t[i].id.substr(1,3)){
sum += t[i].score;
flag = true;
count++;
}
}
if(flag) printf("%d %d\n",count,sum);
}
else if(type == 3){
for(int i = 0; i < N; i++){
if(s == t[i].id.substr(4,6)){
mp[t[i].id.substr(1,3)]++;
flag = true;
}
}
vector<pair<string,int>>vec_mp(mp.begin(),mp.end());
sort(vec_mp.begin(),vec_mp.end(),cmp_1);
for(int i = 0; i <vec_mp.size(); i++){
printf("%s %d\n",vec_mp[i].first.c_str(),vec_mp[i].second);
}
}
if(!flag) printf("NA\n");
}
int main(){
string s;
cin >> N >> M;
for(int i = 0;i < N; i++){
cin >> t[i].id >> t[i].score;
}
for(int i = 1; i <= M; i++){
cin >> type >> s;
printf("Case %d: %d %s\n",i,type,s.c_str());
deal(s);
}
}在这里插入代码片