国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

1055. The World's Richest (25)

2019-11-14 09:50:12
字體:
來源:轉載
供稿:網友

1055. The World's Richest (25)

時間限制 400 ms內存限制 128000 kB代碼長度限制 16000 B判題程序 Standard 作者 CHEN, Yue

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 4Zoe_Bill 35 2333Bob_Volk 24 5888Anny_Cin 95 999999Williams 30 -22Cindy 76 76000Alice 18 88888Joe_Mike 32 3222Michael 5 300000Rosemary 40 5888Dobby 24 5888Billy 24 5888Nobody 5 04 15 454 30 354 5 951 45 50

Sample Output:

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

#include<iostream>#include<string>#include<vector>#include<algorithm>#include<string.h>using namespace std;struct person{		char name[10];		int age;		int worth;};bool compare1(const person &a,const person &b){	return a.age<b.age;}bool compare2(const person &a,const person &b){	if(a.worth!=b.worth)	return a.worth>b.worth;	else if(a.age!=b.age)	return a.age<b.age;	if(strcmp(b.name,a.name)>0)	return true;	return false;}vector<person> v;vector<person> a;void qury(int b,int min,int max,int N){	int i;	v.clear();/*	int start=0,end=a.size()-1;	while(start<end-1){		if(a[(start+end)/2].age>min){			end=(start+end)/2;		}		else if(a[(start+end)/2].age<min){			start=(start+end)/2;		}		else{			start=(start+end)/2;			end=start;		}	}	if(start-3<0)	start=0;	else	start=start-3;*/	for(i=0;i<N;i++){		/*if(a[i].age>max)		break;*/		if(a[i].age>=min&&a[i].age<=max)		v.push_back(a[i]);		if(v.size()>=b)		break;	}	if(v.size()==0){	printf("None/n");	return ;	}	//sort(v.begin(),v.end(),compare2);	if(b>=v.size())	for(i=0;i<v.size();i++){	printf("%s %d %d/n",v[i].name,v[i].age,v[i].worth);	}	else	for(i=0;i<b;i++){	printf("%s %d %d/n",v[i].name,v[i].age,v[i].worth);	}}int main(){	int N,M,i,temp1,temp2,temp3;	person tt;	cin>>N>>M; 	for(i=0;i<N;i++){		scanf("%s %d %d",&tt.name,&tt.age,&tt.worth);		a.push_back(tt);	}	sort(a.begin(),a.end(),compare2);	for(i=0;i<M;i++){		scanf("%d %d %d",&temp1,&temp2,&temp3);		printf("Case #%d:/n",i+1);		qury(temp1,temp2,temp3,N);	}} 感想:這道題關鍵點是運行時間,測試點2和3表示數據量最大和查詢數據量最大,具體要求:1. 先按輸出要求用sort函數排序后,再取數據,這里當數據量到達了要求的量的時候要停止遍歷,這樣可以降低時間2. 不要用string類,不要用cout,用char* 和printf3. 使用vector也可以加快速度4. 注掉的部分是我之前的想法(先按時間排序,再用二分查找法找到要查找的子字符串的起始位置,然后再對字符串排序,結果會略超時)
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 洛川县| 德庆县| 通海县| 台山市| 新竹县| 囊谦县| 太白县| 江油市| 瑞金市| 湛江市| 保山市| 涿州市| 云南省| 合江县| 灵璧县| 木兰县| 信宜市| 庄浪县| 上高县| 花垣县| 金山区| 庆城县| 乌鲁木齐县| 沧州市| 讷河市| 威宁| 贞丰县| 洛扎县| 凤台县| 临清市| 濮阳县| 博爱县| 甘洛县| 库伦旗| 锦屏县| 韶关市| 木兰县| 屯门区| 海林市| 北安市| 浦北县|