PAT 1055 The World's Richest

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 (≤) - the total number of people, and K (≤) - 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 [−]) of a person. Finally there are Klines of queries, each contains three positive integers: M (≤) - the maximum number of outputs, and [AminAmax] 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 [AminAmax]. 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 4
Zoe_Bill 35 2333
Bob_Volk 24 5888
Anny_Cin 95 999999
Williams 30 -22
Cindy 76 76000
Alice 18 88888
Joe_Mike 32 3222
Michael 5 300000
Rosemary 40 5888
Dobby 24 5888
Billy 24 5888
Nobody 5 0
4 15 45
4 30 35
4 5 95
1 45 50

Sample Output:

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

题目大意:找出区间[low, high]之间最富有的M个人;
思路: 先通过输出要求对所有数据进行排序, 然后再遍历所有数据输出在区间内的数据
 1  #include<iostream>
 2 #include<vector>
 3 #include<algorithm>
 4 #include<cstring>
 5 #include<queue>
 6 using namespace std;
 7 struct Node{
 8   char name[10];
 9   int age, worth;
10   Node(){}
11   Node(char _name[], int _age, int _worth){
12     strcpy(name, _name);
13     age = _age;
14     worth = _worth;
15   }
16 };
17 
18 bool cmp(Node& a, Node& b){
19   if(a.worth!=b.worth) return a.worth>b.worth;
20   if(a.age!=b.age) return a.age<b.age;
21   return strcmp(a.name, b.name)<0;
22 }
23 
24 
25 int main(){
26   int n, k, i;
27   scanf("%d%d", &n, &k);
28   vector<Node> v(n);
29   for(i=0; i<n; i++){
30     char name[10];
31     int age, worth;
32     scanf("%s%d%d", name, &age, &worth);
33     Node node(name, age, worth);
34     v[i] = node;
35   }
36   sort(v.begin(), v.end(), cmp);
37   for(i=0; i<k; i++){
38     int cnt, low, high, j;
39     scanf("%d%d%d", &cnt, &low, &high);
40     int num=0;
41     printf("Case #%d:
", i+1);
42     for(j=0; j<n && num<cnt; j++){
43       if(v[j].age>=low && v[j].age<=high){
44         printf("%s %d %d
", v[j].name, v[j].age, v[j].worth);
45         num++;
46       }
47     }
48     if(num==0) printf("None
");
49   }
50   return 0;
51 }

刚开始的思路:是先对所有的数据按照年龄进行排序, 然后找出区间内的数据, 再对数据进行排序, 然后再输出; 

       这种方法因为涉及到多个数据的复制以及二次排序, 会导致第三个测试点超时; 

   这里尝试了常用容器自定义结构的比较函数

   

bool operator < (const Node& other) const{
     return ele<other.ele;  
}

格式为:

 1 #include<iostream>
 2 #include<vector>
 3 #include<algorithm>
 4 #include<cstring>
 5 #include<queue>
 6 using namespace std;
 7 struct Node{
 8   char name[10];
 9   int age, worth;
10   Node(){}
11   Node(char _name[], int _age, int _worth){
12     strcpy(name, _name);
13     age = _age;
14     worth = _worth;
15   }
16   bool operator < (const Node& other)const{
17     if(worth!=other.worth) return worth<other.worth;
18     if(age!=other.age) return age>other.age;
19     return strcmp(name, other.name)>0;
20   }
21 };
22 
23 bool cmp(Node& a, Node& b){
24   return a.age < b.age;
25 }
26 
27 bool cmp1(Node& a, Node& b){
28   if(a.worth!=b.worth) return a.worth>b.worth;
29   if(a.age!=b.age) return a.age<b.age;
30   return strcmp(a.name, b.name)<0;
31 }
32 int lower_bound(vector<Node>& v, int key, int len){
33   int low = 0, high=len-1;
34   while(low<high){
35     int mid = (low+high)/2;
36     if(v[mid].age>=key) high=mid;
37     if(v[mid].age<key) low=mid+1;
38   }
39   return low;
40 }
41 
42 int upper_bounder(vector<Node>& v, int key, int len){
43   int low=0, high=len;
44   while(low<high){
45     int mid=(low+high)/2;
46     if(v[mid].age>key) high=mid;
47     if(v[mid].age<=key) low=mid+1;
48   }
49   return low;
50 }
51 
52 int main(){
53   int n, k, i;
54   scanf("%d%d", &n, &k);
55   vector<Node> v(n);
56   for(i=0; i<n; i++){
57     char name[10];
58     int age, worth;
59     scanf("%s%d%d", name, &age, &worth);
60     Node node(name, age, worth);
61     v[i] = node;
62   }
63   sort(v.begin(), v.end(), cmp);
64   for(i=0; i<k; i++){
65     int cnt, low, high, j;
66     scanf("%d%d%d", &cnt, &low, &high);
67     int lidx=lower_bound(v, low, n), ridx=upper_bounder(v, high, n)-1;
68     /*priority_queue<Node, vector<Node> > q;
69     for(j=lidx; j<=ridx; j++) q.push(v[j]);
70     printf("Case #%d:
", i+1);
71     if(q.size()==0) printf("None
");
72     for(j=0; j<cnt&&q.size(); j++){
73       Node temp = q.top();
74       printf("%s %d %d
", temp.name, temp.age, temp.worth);
75       q.pop();
76     }*/
77     vector<Node> ans;
78     for(j=lidx; j<=ridx; j++) ans.push_back(v[j]);
79     sort(ans.begin(), ans.end(), cmp1);
80     printf("Case #%d:
", i+1);
81     if(lidx>ridx) printf("None
");
82     for(j=0; j<ans.size() && j<cnt; j++) printf("%s %d %d
", ans[j].name, ans[j].age, ans[j].worth);
83   }
84   return 0;
85 }
原文地址:https://www.cnblogs.com/mr-stn/p/9566387.html