1055. The World's Richest (25)

这题要剪支,每个年龄段最多输出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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值