PAT 1055. The World's Richest (25)

//1055. The World's Richest (25)
//这道题主要方法很简单,但是很容易超时,所以需要适当的剪枝。
//剪枝的方法是: 另外对age开辟一个数组,由于每次只需要输出给定范围内的最多100个结果,所以对相同age的100个以后的数据不做考虑,建立age在整个数组中的索引。
//acc
#include <iostream>
#include <algorithm>
#include <string.h>
using namespace std;

typedef struct
{
	char name[9];
	int age;
	int worth;
}Student;

bool cmp( Student s1, Student s2)
{
	if (s1.worth == s2.worth)
	{
		if (s1.age == s2.age)
		{
			return (strcmp(s1.name, s2.name)<0); 
		}
		return (s1.age < s2.age);
	}
	return s1.worth > s2.worth;
}
	
Student stu[100001];

int main()
{
	int N, K;
	cin >> N >> K;

	int i;
	for (i = 0;i<N; i++)
	{
		scanf("%s %d %d", &stu[i].name, &stu[i].age, &stu[i].worth);
	}

	sort(stu, stu+N, cmp);
	
	int num = 0;
	int age[201] = {0};
	int filter_num[100001];

	//剪枝部分begin
	for (i = 0;i<N;i++)
	{
		if (age[stu[i].age] < 100)
		{
			filter_num[num++] = i;
			age[ stu[i].age ]++;
		}
	}
	//剪枝部分end

	int j;
	int maxperson;
	int minage, maxage;
	int currperson = 0;
	int index = 0;
	for ( i = 1;i<=K;i++)
	{
		scanf("%d %d %d", &maxperson, &minage, &maxage);
		printf("Case #%d:\n", i);

		currperson = 0;
		for (j = 0;j<num;j++)
		{
			index = filter_num[j];//剪枝尤为注意这个的赋值,相当于建立索引
			if (currperson == maxperson)
			{
				break;
			}
			if (stu[index].age >= minage && stu[index].age <= maxage)
			{
				printf("%s %d %d\n", stu[index].name, stu[index].age, stu[index].worth);
				currperson++;
			}
		}

		if (currperson == 0 && maxperson!=0)
		{
			//cout << "None\n";
			printf("None\n");
		}
	}
	return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值