Now that we prefer <iostream> to <stdio.h> ,why we use <stdio> in some acm questions?everyone knows the reason.well,i am not willing to do the same thing.and not only I think that way.Indeed,after searching online,I found there are three ways to improve the perfomance for <iostream>.
1.At the first line in main function,add :std::ios_base::sync_with_stdio(false).which cancel theSynchronization between <iostream> and <cstdio>;
2.At the second line in main function,add: std::cin.tie(0).which leads to that cin ties nothing.cin ties cout at first.
3.For all endl, use '\n' or"\n" instead.
Now let us get back pat1055,which we can change in three ways I mentiond above.and we get correct answer.
ps:cin>>string and cin>>char* are almost the same.if we do not filter the name that supass 100,the second case still cannot let us pass.
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
struct Person
{
char name[9];
int age;
int worth;
};
struct Comp
{
bool operator()(Person const &per_a,Person const &per_b)
{
if(per_a.worth==per_b.worth)
{
if(per_a.age==per_b.age)
{
return strcmp(per_a.name,per_b.name)<0;
}
return per_a.age<per_b.age;
}
return per_a.worth>per_b.worth;
}
};
int main()
{
std::ios::sync_with_stdio(false);
cin.tie(0);
int n,m;
cin>>n>>m;
Person arr[n];
for(int i=0;i<n;++i)
{
cin>>arr[i].name>>arr[i].age>>arr[i].worth;
}
sort(arr,arr+n,Comp());
bool mark[n];
memset(mark,0,sizeof(mark));
int cnt[201]={0};
int b[n];
int j=0;
for(int i=0;i<n;++i)
{
if(cnt[arr[i].age]<100)
{
b[j++]=i;
++cnt[arr[i].age];
}
}
for(int i=0;i<m;++i)
{
cout<<"Case #"<<i+1<<":\n";
int output_n,amin,amax;
cin>>output_n>>amin>>amax;
int tmp=output_n;
for(int ii=0;ii<j&&output_n!=0;++ii)
{
if(arr[b[ii]].age<=amax&&arr[b[ii]].age>=amin)
{
cout<<arr[b[ii]].name<<' '<<arr[b[ii]].age<<' '<<arr[b[ii]].worth<<'\n';
--output_n;
}
}
if(tmp==output_n) cout<<"None"<<'\n';
}
}