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 (≤10^5) the total number of people, and K (≤10^3)- 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 [−10^6,10^6] 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
思路分析:
步骤1:输入数据,结构数组peo存储输入n个人的信息,结构数组k存储K组待输出的信息;
步骤2:将peo结构数组按财富值排序(财富值相等时,年龄小的排前,财富值年龄均相等时名字字典序小的排前)
步骤3:考虑到年龄在(0,200]范围内,每个age对应的输出最大人数不超过100人,形成一个以age为下标的结构数组Age,Age.x为node1型vector,存储Age[age].num个人的信息
步骤4:重复K次调用函数fuc,用cnt[age]指针指向未输出的财富最大值的下标
#include"stdafx.h"
#include<vector>
#include<algorithm>
#include<string.h>
using namespace std;
const int maxn1 = 100005;
const int maxn2 = 10005;
const int maxn3 = 205;
struct node1 {
char name[10];
int age, net_worth;
}peo[maxn1];
struct node2 {
int m, right, left;
}k[maxn2];
struct AGE {
int num=0;
vector<node1> x;
}Age[maxn3];
bool cmp(node1 a, node1 b) {
if (a.net_worth != b.net_worth) return a.net_worth > b.net_worth;
else if (a.age != b.age) return a.age < b.age;
else return strcmp(a.name,b.name)<0;
}
void fuc(int a, int b,int mm) {
int cnt[205] = { 0 },count=0; //cnt[age]为未输出年龄为age的人财富值最大的下标
for (int i = a; i <= b; i++) { //年龄[a,b]范围内人数;
count += Age[i].num;
}
if (count == 0) {
printf("None\n"); //[a,b]区间无人输出NONE
return;
}
if (count >= mm) count = mm; //count>=mm时取mm,count<mm时输出count人
for (int i = 0; i < count;i++) {
int hh = a;
while (Age[hh].num <= cnt[hh]) hh++; //hh为未全部输出的年龄的最小值
if (hh > b) return; //hh超出[a,b]范围返回
node1 max = Age[hh].x[cnt[hh]]; //初始化max
int x = hh; //初始化当前最大输出的年龄
for (int j = a; j <= b; j++) {
if (Age[j].num <= cnt[j]) continue;
node1 temp = Age[j].x[cnt[j]];
if (max.net_worth < temp.net_worth) {
max = temp; //修改最大值
x = j;
}
}
cnt[x]++;
printf("%s %d %d\n", max.name, max.age, max.net_worth);
}
}
int main()
{
int n, K;
scanf("%d %d",&n,&K);
for (int i = 0; i < n; i++) {
scanf("%s %d %d", &peo[i].name, &peo[i].age, &peo[i].net_worth);
}
sort(peo, peo + n, cmp);
for (int i = 0; i < K; i++) {
scanf("%d %d %d", &k[i].m, &k[i].left, &k[i].right);
}
for (int i = 0; i < n; i++) {
if (Age[peo[i].age].num <= 100) {
Age[peo[i].age].num++;
Age[peo[i].age].x.push_back(peo[i]);
}
}
for (int i = 0; i < K; i++) {
printf("Case #%d\n", i+1);
fuc(k[i].left, k[i].right, k[i].m);
}
return 0;
}