当前位置:网站首页>1055 The World‘s Richest
1055 The World‘s Richest
2022-07-19 21:39:00 【Brosto_Cloud】
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 (≤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.
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 [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
.
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
输入输出都用scanf、printf;
先排序,然后再根据年龄段选前m个人,也就是说只排序一次;
剪枝:由于m不会超过100,所以在根据年龄段选人时不需要把所有符合年龄条件的都选上,只要当前计数的cnt超过100就退出循环并输出答案。
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
int n, k, m, Left, Right;
struct Peo {
string name;
int age;
int worth;
} a[100010], b[1010];
bool cmp(Peo p1, Peo p2) {
return p1.worth == p2.worth ? (p1.age == p2.age ? (p1.name < p2.name) : p1.age < p2.age) : p1.worth > p2.worth;
}
int main() {
scanf("%d %d", &n, &k);
for (int i = 0; i < n; i++) {
a[i].name.resize(10);
scanf("%s %d %d", &a[i].name[0], &a[i].age, &a[i].worth);
}
int t = 0;
sort(a, a + n, cmp);
while (k != t) {
t++;
scanf("%d %d %d", &m, &Left, &Right);
int cnt = 0;
for (int i = 0; i < n && cnt <= 110; i++) {
if (a[i].age >= Left && a[i].age <= Right) {
b[cnt++] = a[i];
}
}
printf("Case #%d:\n", t);
int num = min(cnt, m);
for (int i = 0; i < num; i++) {
printf("%s %d %d", b[i].name.c_str(), b[i].age, b[i].worth);
if (i != num - 1) {
printf("\n");
}
}
if (cnt == 0) {
printf("None");
}
if (t != k) {
printf("\n");
}
}
return 0;
}
边栏推荐
- 微信小程序应用开发(一)
- 攻防世界----ics-05
- 【Matlab】解决使用Mex 报错There was a problem creating the mex file for Real Time Execution ,Please ensure y
- (cvpr2020)Learning texture transformer network for image super-resolution论文阅读
- 详解“洋葱架构”
- What is the key to implement MES management system
- DNS域名解析
- CPDA | first learn data analysis tools or data analysis thinking?
- es-从搜索中检索选定的字段
- Knowledge and location theory of golden K-line diagram
猜你喜欢
随机推荐
Design of the multi live architecture in different places of the king glory mall
DOM事件流
Debezium系列之:优化集群参数和支持debezium connector个性化设置
华为无线设备漫游配置同一业务VLAN的AP间非快速漫游
The most complete idea debug debugging skills in history (super detailed cases)
【Swoole系列2.5】异步任務
Dbeaver connect Oracle user display error and user does not exist?
Arthas小白入门
Day008 选择结构 (switch语句)
LeetCode:733. 图像渲染【BFS】
散户炒股选哪个证券公司手续费低,手机上开户安全吗
Escape the front line! After seven years of returning to the second tier from Shanghai, how is it now?
The study of classic diagram of K-line diagram is conducive to correct
从wolai转移到Notion
Oracle使用fy_recover_data恢复truncate删除的数据
L'intervieweur m'a demandé pourquoi l'algorithme de collecte par génération GC de JVM était conçu de cette façon.
k线图经典图解的学习有利于正确
[C # process control] - if/switch selection structure in C #
NPDP|没有技术背景能做好产品经理吗?
EasyDSS使用mysql数据库,无法启动该如何解决?