PAT A 1055. The World's Richest (25)

题目

Forbes magazine publishes every year its list of billionaires based on the annual ranking of the world's wealthiest people.  Now you are supposed to simulate this job, but concentrate only on the people in a certain range of ages.  That is, given the net worths of N people, you must find the M richest people in a given range of their ages.

Input Specification:

Each input file contains one test case.  For each case, the first line contains 2 positive integers: N (<=105) - the total number of people, and K (<=103) - the number of queries.  Then N lines follow, each contains the name (string of no more than 8 characters without space), age (integer in (0, 200]), and the net worth (integer in [-106, 106]) of a person.  Finally there are K lines of queries, each contains three positive integers: M (<= 100) - the maximum number of outputs, and [Amin, Amax] which are the range of ages.  All the numbers in a line are separated by a space.

Output Specification:

For each query, first print in a line "Case #X:" where X is the query number starting from 1.  Then output the M richest people with their ages in the range [Amin, Amax].  Each person's information occupies a line, in the format

Name Age Net_Worth

The outputs must be in non-increasing order of the net worths.  In case there are equal worths, it must be in non-decreasing order of the ages.  If both worths and ages are the same, then the output must be in non-decreasing alphabetical order of the names.  It is guaranteed that there is no two persons share all the same of the three pieces of information.  In case no one is found, output "None".

Sample Input:

12 4
Zoe_Bill 35 2333
Bob_Volk 24 5888
Anny_Cin 95 999999
Williams 30 -22
Cindy 76 76000
Alice 18 88888
Joe_Mike 32 3222
Michael 5 300000
Rosemary 40 5888
Dobby 24 5888
Billy 24 5888
Nobody 5 0
4 15 45
4 30 35
4 5 95
1 45 50

Sample Output:

Case #1:
Alice 18 88888
Billy 24 5888
Bob_Volk 24 5888
Dobby 24 5888
Case #2:
Joe_Mike 32 3222
Zoe_Bill 35 2333
Williams 30 -22
Case #3:
Anny_Cin 95 999999
Michael 5 300000
Alice 18 88888
Cindy 76 76000
Case #4:
None

 

暴力方法:输入,排序,k个循环从头到尾扫描m个符合条件的数,输出。输出扫描的代价偏大,会超时。可以先扫描一次,把各个年龄段100名以外的先剔除(因为肯定用不到),此后每次仅扫描剔除那些数据后的,速度会快一些。

所用代码的思路,按各个年龄将数据分开,排序(排序代价会略微小一些)。每次输入扫描年龄范围段尚未输出的最大值,输出这个对应的数据,直到输出完毕或者没有数据了。

 

代码:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
using namespace std;

int MAX=0x3fffffff;

struct person	//个人信息
{
	char name[10];
	int age;
	int worth;
};

bool cm(const person &p1,const person &p2);	//排序

int main()
{
	int n,k;	//输入数据
	cin>>n>>k;

	person temp_p;	//临时的人
	vector<person> people[201];	//各个年龄的人

	int i,j,l;
	for(i=0;i<n;i++)	//输入人的信息
	{
		scanf("%s %d %d",temp_p.name,&temp_p.age,&temp_p.worth);
		people[temp_p.age].push_back(temp_p);
	}

	for(i=1;i<=200;i++)	//对各个年龄中的人排序
		sort(people[i].begin(),people[i].end(),cm);
	
	int m,age1,age2;	//查询条件
	int worthiest;	//最大的价值
	int worthiest_age;	//最大价值对应的年龄
	int age_flag[201];	//每个年龄已经输出到的位置
	for(i=1;i<=k;i++)
	{
		scanf("%d %d %d",&m,&age1,&age2);	//输入一次查询
		printf("Case #%d:\n",i);
		if(age1>200||age2<1||age1>age2)	//年龄范围超限
		{
			printf("None\n");
			continue;
		}
		for(j=1;j<=200;j++)	//情况标志
			age_flag[j]=0;
		for(j=0;j<m;j++)	//循环输出m个
		{
			worthiest=-MAX;
			for(l=age1;l<=age2;l++)	//扫描剩下数据中财富最多,且年龄最小的
			{
				if(age_flag[l]<people[l].size()&&people[l][age_flag[l]].worth>worthiest)
				{
					worthiest_age=l;
					worthiest=people[l][age_flag[l]].worth;
				}
			}
			if(worthiest!=-MAX)	//还能找到符合条件的数据,输出
			{
				printf("%s %d %d\n",people[worthiest_age][age_flag[worthiest_age]].name,
					people[worthiest_age][age_flag[worthiest_age]].age,
					people[worthiest_age][age_flag[worthiest_age]].worth);
				age_flag[worthiest_age]++;
			}
			else	//已经没有找到符合条件的数据,跳出
				break;
		}
		if(j==0)	//没有任何符合条件的输出
			printf("None\n");
	}

	return 0;
}

bool cm(const person &p1,const person &p2)
{
	if(p1.worth>p2.worth)
		return true;
	else if(p1.worth<p2.worth)
		return false;
	else
		return strcmp(p1.name,p2.name)<0;
}


 

 

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值