书强调:不能先排序然后根据每一个条件再新建一个数组、对新数组排序的方法,这样测试会超时。因为n和m的悬殊太大,n有10的5次方,m却只有100个,但是为什么自己这样写就可以运行呢?
#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;
struct person
{
char name[10];
int age,worth;
};
bool cmp(person a,person 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;
scanf("%d%d\n",&n,&k);
vector<person> pe(n);
for(int i=0;i<n;i++)
{
scanf("%s%d%d",pe[i].name,&pe[i].age,&pe[i].worth);
}
sort(pe.begin(),pe.end(),cmp);
int ant,minage,maxage;
for(int i=1;i<=k;i++)
{
scanf("%d%d%d",&ant,&minage,&maxage);
vector<person> ans;
printf("Case#%d\n",i);
int num=0;
for(int j=0;j<n;j++)
{
if(pe[j].age>=minage&&pe[j].age<=maxage)
{
ans.push_back(pe[j]);
num++;
}
}
if(num==0) printf("None\n");
else
{
for(int z=0;z<ant&&z<num;z++)
{
printf("%s %d %d\n",ans[z].name,ans[z].age,ans[z].worth);
}
}
}
return 0;
}