http://pat.zju.edu.cn/contests/pat-a-practise/1055
Forbes Magazine publishes every year it list of billionaires based on the annual ranking to the world ' s wealthiest people . Now for are supposed to simulate this job, but concentrate only on the people in a certain range of ages. That's, given the net worths of N people, you are must find the M richest people in a given range of their.
Input Specification:
Each input file contains one test case. For the 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 spaces), age (integer in (0, 200)) , and the Net worth (integer in [ -106,%]) of a person. Finally There are K lines of queries, each contains three positive integers:m (<=)-the maximum number of outputs , and [Amin, Amax] which are the range of ages. All of the numbers in a line are separated by a.
Output Specification:
For each query, the #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 is in non-increasing order of the net worths. In case there are equal worths, it must is in non-decreasing order of the ages. If both Worths and ages are the same, the then the output must is in non-decreasing the order of the alphabetical. It is guaranteed this there is no two persons share all the same of the three of pieces. In case no one is found, output "None".
Sample Input:
4
zoe_bill 2333
bob_volk 5888
anny_cin 999999 Williams 30-22 Cindy
76000
Alice 18
joe_mike 3222
Michael 5 300000 Rosemary 88888 5888
Dobby 5888
Billy 5888
Nobody 5 0< C12/>4
4
4 5 km
1 45 50
Sample Output:
Case #1:
Alice 88888,
Billy 5888
bob_volk 5888 Dobby case
#2:
5888 32 32
zoe_bill 2333
Williams 30-22 case
#3:
anny_cin 999999
Michael 5 300000
Alice 18 8 8888
Cindy 76000 case
#4:
None
The key is pruning:
#include <cstdio> #include <cstring> #include <algorithm> using namespace std;
struct rich{char name[9];
int age;
int worth;
BOOL operator < (const rich &a) const{if (worth!= A.worth) return worth > A.worth;
else if (age!= a.age) return to age < a.age;
else return strcmp (name, a.name) < 0;
}}num[100000];
int main () {//freopen ("g:\\input.in", "R", stdin);
int n, K;
scanf ("%d%d", &n, &k);
for (int i = 0; i < n; ++i) {scanf ("%s%d%d", Num[i].name, &num[i].age, &num[i].worth);
Sort (num, num + N);
int agecount[201] = {0};
int filter[100001];
int filter_num = 0;
for (int i = 0; i < n; i++) {if (++agecount[num[i].age] <) {filter[filter_num++] = i;
for (int i = 1; I <= K; i++) {int m, Amin, Amax;
scanf ("%d%d%d", &m, &amin, &amax);
printf ("Case #%d:\n", i);
int index = 0;
for (int j= 0; j < Filter_num; J + +) {int k = filter[j]; if (Num[k].age >= amIn && num[k].age <= amax && Index < m) {printf ("%s%d%d\n", Num[k].name, Num[k].age, Num[k].wor
TH);
index++;
} if (index = = 0) printf ("none\n");
return 0; }