https://pintia.cn/problem-sets/994805260223102976/problems/1071786104348536832
主要是情况3容易超时,把cout改成printf效率会高一些。
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
struct stu{// 学生
string num;
int score;
};
struct kao{// 考场
int num=0;
int student=0;
};
bool cmp1(stu a, stu b){
if(a.score!=b.score)
return a.score > b.score;
else
return a.num < b.num;
}
bool cmp2(kao a, kao b){
if(a.student!=b.student)
return a.student > b.student;
else
return a.num < b.num;
}
int main() {
string str;
int n, m, t, flag;
cin >> n >> m;
stu stus[n+1];
for(int i=1; i<=n; i++){
cin >> stus[i].num >> stus[i].score;
}
for(int i=0; i<m; i++){
cin >> t >> str;
printf("Case %d: %d %s\n", i+1, t, str.c_str());
flag = 0;
// 查开头
if(t==1){
int k=1;
stu rank[n+1];
for(int j=1; j<=n; j++){
if(stus[j].num[0]==str[0]){
rank[k].num = stus[j].num;
rank[k++].score = stus[j].score;
}
}
sort(rank+1, rank+k, cmp1);
for(int j=1; j<k; j++){
flag = t;
printf("%s %d\n", rank[j].num.c_str(), rank[j].score);
}
// 查考场人数和总分数
}else if(t==2){
int count=0, total=0;
for(int j=1; j<=n; j++){
if(stus[j].num.substr(1, 3)==str){
count ++;
total += stus[j].score;
}
}
if(count!=0){
printf("%d %d\n", count, total);
flag = t;
}
// 查指定日期的每个考场的考场号和人数
}else if(t==3){
int temp, l=0, max=-1;
kao kaos[1000];
for(int j=1; j<=n; j++){
if(stus[j].num.substr(4, 6)==str){
// 考场号101~999
temp = atoi(stus[j].num.substr(1, 3).c_str());
if(kaos[temp-101].num==0){
l ++;
kaos[temp-101].num = temp;
if(max<temp-101)
max = temp-101;
}
kaos[temp-101].student ++;
}
}
sort(kaos, kaos+max+1, cmp2);
for(int j=0; j<l; j++){
printf("%d %d\n", kaos[j].num, kaos[j].student);
flag = t;
}
}
if(flag == 0){
cout << "NA" << endl;
}
}
return 0;
}