您的位置:首页 > 其它

1055. The World's Richest (25)

2017-02-11 15:51 302 查看
这题要剪支,每个年龄段最多输出100人,所以每个年龄最多保留100人就可以了,不然第二点超时

不过我第一次用的方法第二点过了,第三点超时了。。。还多赚一分lol

#include<iostream>
#include<algorithm>
#include<vector>
#include<map>
#include<string>
#include<set>
#include<stack>
using namespace std;
struct rec{
string name;
int age;
int worth;
};
int n, k;
bool comp(rec a, rec b){
if(a.worth != b.worth) return a.worth > b.worth;
else if(a.age != b.age) return a.age < b.age;
else return a.name < b.name;
}
vector<rec> all_recs, keep_recs;
int allages[201] = {0};
int main(){
cin>>n>>k;
for(int i = 0; i < n; i++){
rec a;
cin>>a.name;
scanf("%d%d",&a.age,&a.worth);
all_recs.push_back(a);
}
sort(all_recs.begin(),all_recs.end(),comp);
for(int i = 0; i < n; i++){
int tempage = all_recs[i].age;
if(allages[tempage] < 100){
allages[tempage]++;
keep_recs.push_back(all_recs[i]);
}
}
for(int i = 1; i <= k; i++){
int num, low, high;
scanf("%d%d%d",&num,&low,&high);
printf("Case #%d:\n",i);
int count = 0;
for(int j = 0; j < keep_recs.size() && count < num; j++){
if(keep_recs[j].age >= low && keep_recs[j].age <= high){
printf("%s %d %d\n",keep_recs[j].name.c_str(),keep_recs[j].age,keep_recs[j].worth);
count++;
}
}
if(count == 0){
printf("None\n");
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  PAT