从头到尾遍历 万幸没有超时。晴神书里的写法是有优化
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
struct man{
char name[10];
int age;
int worth;
}men[100005];
int cmp(man a,man b){
if(a.worth!=b.worth) return a.worth>b.worth;
else if(a.age!=b.age) return a.age<b.age;
else return strcmp(a.name,b.name)<0;
}
int main(){
int n,k,i,num,lage,hage,j=0,tag=0;
scanf("%d%d",&n,&k);
for(i=0;i<n;i++){
scanf("%s%d%d",men[i].name,&men[i].age,&men[i].worth);
}
sort(men,men+n,cmp);
for(i=0;i<k;i++){
tag=0;
scanf("%d%d%d",&num,&lage,&hage);
printf("Case #%d:\n",i+1);
for(j=0;j<n && num>0;j++){
if(men[j].age<=hage && men[j].age>=lage){
printf("%s %d %d\n",men[j].name,men[j].age,men[j].worth);
tag=1;
num--;
}
}
if(tag==0) printf("None\n");
}
return 0;
}
PAT A1055 the world's richest(25)
最新推荐文章于 2022-04-20 21:29:21 发布