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

2211

被折叠的 条评论
为什么被折叠?



