1055.theworld'srichest(25)

gaoren gaoren     2022-09-29     330

关键词:

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

解题思路:字符串比较的使用char*来存储,而不能用string,不然此题会超时。
#include<iostream>
#include<cstdio>
#include<vector>
#include<cstring>
#include<algorithm>
using namespace std;
struct Node{
	char* name;
	int age;
	int worth;
};
vector<Node>v_p;
bool cmp(Node a,Node b){
	if(a.worth>b.worth){
		return 1;
	}else if(a.worth==b.worth&&a.age<b.age){
		return 1;
	}else if(a.worth==b.worth&&a.age==b.age){
		for(int i=0;i<9;i++){
			if(a.name[i]<b.name[i])return 1;
			else if(a.name[i]>b.name[i])return 0;
		}
	}else {
		return 0;
	}
}
int main(){
	int n,m,k;
	scanf("%d%d",&n,&k);
	int i,j;
	int age;
	int worth;
	Node node;
	for(i=0;i<n;i++){
		char* name= new char[9];
		scanf("%s%d%d",name,&age,&worth);
		node.name=name;
		node.age=age;
		node.worth=worth;
		v_p.push_back(node);
	}
	sort(v_p.begin(),v_p.end(),cmp);
	for(j=0;j<k;j++){
		int num,age_s,age_e;
		int flag=0;
		scanf("%d%d%d",&num,&age_s,&age_e);
		printf("Case #%d:
",j+1);
		for(i=0;i<n;i++){
			int tmp=v_p[i].age;
			if(tmp<=age_e&&tmp>=age_s){
				printf("%s %d %d
",v_p[i].name,v_p[i].age,v_p[i].worth);
				num--;
				flag=1;
			}
			if(num==0){
				break;
			}
		}
		if(!flag){
			printf("None
");
		}
	}
	return 0;
}

  

1055theworld'srichest(25分)(代码片段)

注意到(N(leq10^5)),(M(leq100)),那么对于数据集可以进行如下处理:按题目要求对所有数据排序。建立标记数组c[AGE],在(c_i)中记录年龄为(i)的人数,其中constintAGE=205。再建立新数组s[N],记录有效数据。遍历已有序的数据。若对于... 查看详情

patadvanced1055theworld'srichest(25分)(代码片段)

...besmagazinepublisheseveryyearitslistofbillionairesbasedontheannualrankingoftheworld‘swealthiestpeople.Nowyouaresupposedtosimulatethisjob,butconcentrateonlyonthepeopleinacertainrangeofages.Thatis,giventhenetworthsof N people,youmustfindthe M richestpeopleinagivenrangeoftheirages... 查看详情

1055theworld'srichest(25分)(代码片段)

1.题目2.思路常规题3.注意点注意超时4.tip善于使用题目的条件来减少数据量fill(begin,end,value)注意[begin,end)2.代码#include<cstdio>#include<algorithm>#include<string>#include<vector>#include<iostream>//14:57-usingnamespacestd;structpeoplestr... 查看详情

1055theworld‘srichest(代码片段)

开始的做法是,对于每一个case,都在整个person数组内进行遍历,把所有年龄符合要求的都放进一个临时数组,然后对临时数组进行排序,再根据要求的数目输出。但是这么做会有一个测试用例因为超时而通不... 查看详情

1055theworld‘srichest(25分)难度:一般/知识点:多关键字排序(代码片段)

https://pintia.cn/problem-sets/994805342720868352/problems/994805421066272768这里:注意用scanf,printf。有一个小细节就是string如何用scanf读入呢?我们可以用char[]来读入,最后用char[]给string赋值即可。#include<bits/stdc++.h&g 查看详情

脏字过滤

//脏字过滤functiondeny_bad_words($words) $badwords=array('艹','草','操','你妈','逼','鸡巴','靠','傻','煞笔','草泥马','蠢货','猪','白痴','日你','你妹'... 查看详情

二维数组迷宫趣题

...cclassTwoDimensionalMappublicstaticvoidmain(String[]args)char[][]map='*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*'... 查看详情

微信表情代码怎么用

参考技术A微信表情代码如下:varqqfaceArr=[['[微笑]','0'],['[撇嘴]','1'],['[色]','2'],['[发呆]','3'],['[得意]','4'],['[流泪]','5'],['[害羞]','6'],['[闭嘴]'... 查看详情

随机生成18位密码

varisShow=false;$scope.genPass=function()//varchars=['0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F','G','H','I','J&... 查看详情

总结vbscript中,过程以函数的格式及调用方法,总结二者区别

...tion和subfunction:称作函数可以有返回值也可以没有返回值'''''''''''''''''''''例:有返回值的''''''''''''''''''''&... 查看详情

json格式多维数组

"data":['number':'1','text1':'3','info1':'4','special1':'5','number':'1','text1':'2','info1':'5','special1':'5'],"columModle":['header':'序... 查看详情

json数组的删除。

比如说varrol=['id':'1','name':'Mark','description':'Mark','id':'2','name':'Jon','description':'Jon','id':'3','rolename':'Kent','roledescription':'Kent',&... 查看详情

luabase64自定义编解码(代码片段)

...实现1、base64编码--自定义编码表localencodeTable=[0]='A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S',&... 查看详情

js多个数组合并每一个值对应一个数组

vara=['北京','天津','河北','上海','广东'];varb=['海淀','滨海新区','石家庄','浦东','深圳'];varc=['黄庄','海港','新华区','陆家嘴','福田区'];... 查看详情

python列表操作

定义一个function输入一个类似a_list=[['a','b','c'],['d','e','f'],['g','h','i'],['j','k','l']]的列表。然后想要生成一个b_list=['a','b','c','d','e'... 查看详情

c#winform程序如何判断用户输入的验证码是不是正确?

...分你看一下吧,对你可能有点帮助privatestaticchar[]constant='0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f','g','h','i'... 查看详情

matlab怎么建立结构体数组?

...的每个元素是一个结构体变量。像这样:a=(1,2)a(1)=struct('name',0,'num',0)a(1)=struct('name',0,'num',0)参考技术As(1,1)=struct('number','9901','name','Xiaoli','score','90','grade','A');... 查看详情

python两个list合并成字典

第一个list:['PID','USERNAME','THR','PRI','NICE','SIZE','RES','STATE','TIME','WCPU','COMMAND']第二个list:['32','root','1','-36','-155','0K','16K',&... 查看详情