1055 The World's Richest (25 point(s))

本文详细解析了C++中复杂数据结构的排序算法,并针对特定条件进行了优化处理。通过案例分析,阐述了如何在数据量较大时使用空间换时间策略,对数据进行预处理,以提高排序效率。同时,介绍了如何筛选并展示排序后的数据。

Matter:

1.搞懂了cmp的完全排序问题。

2.根据输入的范围考虑问题。M的数值 < 100,这个地方如果不对100个人进行单独操作会超时。出现数据太多的时候就用空间换时间,将数据带出来,单独处理。

Code:

#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;

struct node{
	char name[10];
	int age , worth;
}Node[100005] , Use[100005];

bool cmp(node a , node b){
	if(a.worth != b.worth){
		return a.worth > b.worth;
	}
	else if(a.age != b.age){
		return a.age < b.age;
	}
	else{
		return strcmp(a.name , b.name) < 0;	//	对于char类型的操作 
	}
}

int main(){
	int n , k;
	
	//input
	scanf("%d %d" , &n , &k);
	for(int i = 0 ; i < n ; i ++){
		scanf("%s %d %d" , Node[i].name , &Node[i].age , &Node[i].worth);	
	}
	
	//sort the structure
	sort(Node , Node + n , cmp);
	
	//Because M <= 100,so we just use the top 100's data in [amin , amax]
	int Age[205] = { 0 } , Use_count = 0;
	for(int i = 0 ; i < n ; i ++){
		if(Age[Node[i].age] < 100){
			Age[Node[i].age] ++;
			Use[Use_count ++] = Node[i];
		}
	}
	
	//output
	int range , amin , amax; 
	for(int i = 1 ; i <= k ; i ++){
		int print_num = 0;
		scanf("%d %d %d" , &range , &amin , &amax);
		printf("Case #%d:\n" , i);
		
		for(int j = 0 ; j < Use_count ; j ++){
			if(Use[j].age >= amin && Use[j].age <= amax && print_num < range){
				printf("%s %d %d\n" , Use[j].name , Use[j].age , Use[j].worth);
				print_num ++;				
			}	
		}
		
		//	if print_num is 0,it means there are no things in it.
		if(print_num == 0){	
			printf("None\n");
		}
	} 
	return 0;
} 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值