解题代码
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
struct person {
char name[ 9 ] ;
int age;
int worth;
} p[ 100000 ] , p1[ 100000 ] ;
typedef struct person per;
struct que {
int cnt;
int min, max;
} q[ 1000 ] ;
bool cmp ( per a, per b) {
if ( a. worth != b. worth) return a. worth > b. worth;
if ( a. age != b. age) return a. age < b. age;
return strcmp ( a. name, b. name) < 0 ;
}
int Age[ 201 ] = { 0 } ;
int main ( )
{
int N, K;
scanf ( "%d %d" , & N, & K) ;
for ( int i = 0 ; i < N; i++ )
scanf ( "%s %d %d" , p[ i] . name, & p[ i] . age, & p[ i] . worth) ;
for ( int i = 0 ; i < K; i++ )
scanf ( "%d %d %d" , & q[ i] . cnt, & q[ i] . min, & q[ i] . max) ;
sort ( p, p + N, cmp) ;
int pp = 0 ;
for ( int i = 0 ; i < N; i++ ) {
if ( Age[ p[ i] . age] < 100 ) {
Age[ p[ i] . age] ++ ;
p1[ pp++ ] = p[ i] ;
}
}
for ( int i = 0 ; i < K; i++ ) {
if ( i) printf ( "\n" ) ;
printf ( "Case #%d:" , i + 1 ) ;
int len = q[ i] . cnt, l, j;
for ( j = 0 , l = 0 ; j < pp && l < len; j++ ) {
if ( p1[ j] . age >= q[ i] . min && p1[ j] . age <= q[ i] . max) {
printf ( "\n%s %d %d" , p1[ j] . name, p1[ j] . age, p1[ j] . worth) ;
l++ ;
}
}
if ( ! l) printf ( "\nNone" ) ;
}
return 0 ;
}
测试结果
问题整理
1.排序算法。
2.该题通过题意分析,预处理后极大缩短了运行时间,避免了超时。
3.注意有两个条件判断的for循环。