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.
Each input file contains one test case. For each case, the first line 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 space), age (integer in (0, 200]), and the net worth (integer in [−106,106]) of a person. Finally there are K lines of queries, each contains three positive integers: M (≤100) - the maximum number of outputs, and [Amin
, Amax
] which are the range of ages. All the numbers in a line are separated by a space.
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 [Amin
, Amax
]. 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
.
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
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
思路:1.错误思路:不知道你是否也和我一样,先将符合the age在[Amin,Amax]的所有富翁存入一个数组中,再对这个数组进行排序,输出前m个富翁的信息,然后测试点1超时。
2.正确思路:先将所有富翁进行排序,然后再查找the age在[Amin,Amax]的富翁,输出符合要求的前m个富翁的信息。
3.思路1和思路2比较:思路1进行了多次排序,时间复杂度高于遍历查找的时间复杂度(在数据量特别大的情况下),而思路2进行了一次排序,多次遍历查找,时间复杂度低于思路1
AC代码
#include <iostream>
#include <string.h>
#include <vector>
#include <algorithm>
using namespace std;
struct inform {
char name[10];
int age;
int worth;
};
bool cmp(inform x, inform y){
if (x.worth != y.worth)
return x.worth > y.worth;
else if (x.age != y.age)
return x.age < y.age;
else return strcmp(x.name, y.name) < 0;
}
int main() {
int n, k;
scanf("%d%d", &n, &k);
vector<inform> rich(n);
for (int i = 0; i < n; i++) {
scanf("%s%d%d", rich[i].name, &rich[i].age, &rich[i].worth);
}
sort(rich.begin(), rich.end(), cmp);
int step = 1;
while (k--) {
printf("Case #%d:\n", step++);
int cnt = 0;
int num, amin, amax;
scanf("%d%d%d", &num, &amin, &amax);
for (int i = 0; i < n && cnt < num; i++) {
if (rich[i].age >= amin && rich[i].age <= amax) {
printf("%s %d %d\n", rich[i].name, rich[i].age, rich[i].worth);
cnt++;
}
}
if (cnt == 0) {
printf("None\n");
}
}
return 0;
}
更多PAT甲级题目:请访问,感谢支持!
因篇幅问题不能全部显示,请点此查看更多更全内容
Copyright © 2019- huatuo9.cn 版权所有 赣ICP备2023008801号-1
违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com
本站由北京市万商天勤律师事务所王兴未律师提供法律服务