【PAT甲级】1055 The World's Richest (25 分)

题意:

输入两个正整数N和K(N<=1e5,K<=1000),接着输入N行,每行包括一位老板的名字,年龄和财富。K次询问,每次输入三个正整数M,L,R(M<=100,L,R<=200),输出至多M位年龄区间在L,R之间的老板的名字年龄和财富,按照财富降序,年龄升序,姓名字典序输出。

代码:

#define HAVE_STRUCT_TIMESPEC
#include<bits/stdc++.h>
using namespace std;
string s;
typedef struct account{
string name;
int age,worth;
};
bool cmp(account a,account b){
if(a.worth!=b.worth)
return a.worth>b.worth;
if(a.age!=b.age)
return a.age<b.age;
return a.name<b.name;
}
vector<account>vv;
vector<account>v;
int sum[207];
int main(){
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n,k;
cin>>n>>k;
int age,w;
for(int i=1;i<=n;++i){
cin>>s>>age>>w;
account tamp;
tamp.name=s;
tamp.age=age;
tamp.worth=w;
vv.push_back(tamp);
}
int x,l,r;
sort(vv.begin(),vv.end(),cmp);
/*for(int i=0;i<vv.size();++i){
//cout<<vv[i].name<<" "<<vv[i].age<<" "<<vv[i].worth<<"\n";
if(sum[vv[i].age]<100){
v.push_back(vv[i]);
++sum[vv[i].age];
}
}*/
for(int i=1;i<=k;++i){
cin>>x>>l>>r;
cout<<"Case #"<<i<<":\n";
int flag=0;
for(int j=0;j<vv.size();++j){
if(vv[j].age>=l&&vv[j].age<=r){
cout<<vv[j].name<<" "<<vv[j].age<<" "<<vv[j].worth<<"\n";
--x;
flag=1;
}
if(!x)
break;
}
if(!flag)
cout<<"None\n";
}
return 0;
}

代码2(根据数据规模优化):

#define HAVE_STRUCT_TIMESPEC
#include<bits/stdc++.h>
using namespace std;
string s;
typedef struct account{
string name;
int age,worth;
};
bool cmp(account a,account b){
if(a.worth!=b.worth)
return a.worth>b.worth;
if(a.age!=b.age)
return a.age<b.age;
return a.name<b.name;
}
vector<account>vv;
vector<account>v;
int sum[207];
int main(){
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n,k;
cin>>n>>k;
int age,w;
for(int i=1;i<=n;++i){
cin>>s>>age>>w;
account tamp;
tamp.name=s;
tamp.age=age;
tamp.worth=w;
vv.push_back(tamp);
}
int x,l,r;
sort(vv.begin(),vv.end(),cmp);
for(int i=0;i<vv.size();++i){
//cout<<vv[i].name<<" "<<vv[i].age<<" "<<vv[i].worth<<"\n";
if(sum[vv[i].age]<100){
v.push_back(vv[i]);
++sum[vv[i].age];
}
}
for(int i=1;i<=k;++i){
cin>>x>>l>>r;
cout<<"Case #"<<i<<":\n";
int flag=0;
for(int j=0;j<v.size();++j){
if(v[j].age>=l&&v[j].age<=r){
cout<<v[j].name<<" "<<v[j].age<<" "<<v[j].worth<<"\n";
--x;
flag=1;
}
if(!x)
break;
}
if(!flag)
cout<<"None\n";
}
return 0;
}

猜你喜欢

转载自www.cnblogs.com/ldudxy/p/11639172.html