PAT 1055. The World's Richest (25)

1055. The World's Richest (25)

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

 此题网上普遍利用了题目中的M<=100的这个条件,将每个年龄段大于100的给过滤掉了。

以下的并没有利用那个条件,首先我们将所有的人按照每个年龄进行分类,然后对每个年龄进行从大到小的排序。然后根据每个给定的年龄段,我们跳出相应的人来。

 1 #include <iostream>
 2 #include <vector>
 3 #include <string>
 4 #include <cstring>
 5 #include <algorithm>
 6 
 7 using namespace std;
 8 
 9 struct People
10 {
11     string name;
12     int age;
13     int wealth;
14 };
15 
16 bool cmp(const People& lhs, const People& rhs)
17 {
18     if (lhs.wealth == rhs.wealth&&lhs.age == rhs.age)
19         return lhs.name < rhs.name;
20     else if (lhs.wealth == rhs.wealth)
21         return lhs.age < rhs.age;
22     else
23         return lhs.wealth > rhs.wealth;
24 }
25 
26 vector<People> age[201];
27 
28 int FindMax(int *index, int Amin, int Amax)
29 {
30     People max;
31     int maxAge = -1;
32     max.wealth = -0x7ffffff;
33     //max.name = "max";
34     //max.age = -1;
35     for (int i = Amin; i <= Amax; i++)
36     {
37         if (index[i] < age[i].size() && cmp(age[i][index[i]], max))
38         {
39             max = age[i][index[i]];
40             maxAge = i;
41         }
42     }
43     
44     return maxAge;
45 }
46 
47 int main()
48 {
49     int peopleNum, queryNum;
50     cin >> peopleNum >> queryNum;
51 
52     for (int i = 0; i < peopleNum; i++)
53     {
54         People tmp;
55         char s[9];
56         scanf("%s%d%d", s, &tmp.age, &tmp.wealth);
57         tmp.name = string(s);
58         age[tmp.age].push_back(tmp);
59     }
60     for (int i = 1; i < 201; i++)
61         sort(age[i].begin(), age[i].end(), cmp);
62 
63     for (int i = 0; i < queryNum; i++)
64     {
65         printf("Case #%d:\n", i + 1);
66         int max, Amin, Amax;
67         scanf("%d%d%d", &max, &Amin, &Amax);
68         int index[201], cnt = 0;
69         memset(index, 0, 201 * sizeof(int));
70         while (cnt < max)
71         {
72             int maxAge = FindMax(index, Amin, Amax);
73             if (maxAge == -1)
74                 break;
75             else
76             {
77                 People max = age[maxAge][index[maxAge]];
78                 printf("%s %d %d\n", max.name.c_str(), max.age, max.wealth);
79                 index[maxAge]++;
80                 cnt++;
81             }
82         }
83         if (cnt == 0)
84             printf("None\n");
85     }
86 }

 

转载于:https://www.cnblogs.com/jackwang822/p/4754529.html

"sgmediation.zip" 是一个包含 UCLA(加利福尼亚大学洛杉矶分校)开发的 sgmediation 插件的压缩包。该插件专为统计分析软件 Stata 设计,用于进行中介效应分析。在社会科学、心理学、市场营销等领域,中介效应分析是一种关键的统计方法,它帮助研究人员探究变量之间的因果关系,尤其是中间变量如何影响因变量与自变量之间的关系。Stata 是一款广泛使用的统计分析软件,具备众多命令和用户编写的程序来拓展其功能,sgmediation 插件便是其中之一。它能让用户在 Stata 中轻松开展中介效应分析,无需编写复杂代码。 下载并解压 "sgmediation.zip" 后,需将解压得到的 "sgmediation" 文件移至 Stata 的 ado 目录结构中。ado(ado 目录并非“adolescent data organization”缩写,而是 Stata 的自定义命令存放目录)目录是 Stata 存放自定义命令的地方,应将文件放置于 "ado\base\s" 子目录下。这样,Stata 启动时会自动加载该目录下的所有 ado 文件,使 "sgmediation" 命令在 Stata 命令行中可用。 使用 sgmediation 插件的步骤如下:1. 安装插件:将解压后的 "sgmediation" 文件放入 Stata 的 ado 目录。如果 Stata 安装路径是 C:\Program Files\Stata\ado\base,则需将文件复制到 C:\Program Files\Stata\ado\base\s。2. 启动 Stata:打开 Stata,确保软件已更新至最新版本,以便识别新添加的 ado 文件。3. 加载插件:启动 Stata 后,在命令行输入 ado update sgmediation,以确保插件已加载并更新至最新版本。4
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值