# PAT甲级真题1055 世界首富(K路归并）

《福布斯》杂志每年都会根据世界上最富有的人群的年度排名发布其亿万富翁名单。

Name Age Net_Worth

1≤N≤1051≤N≤105,
1≤K≤1031≤K≤103,
1≤M≤1001≤M≤100

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

#include <iostream>
#include <cstring>
#include <vector>
#include <algorithm>

using namespace std;

const int N = 210;

int n, m;
struct Person
{
string name;
int age, w;

bool operator< (const Person& t) const
{
if (w != t.w) return w > t.w;
if (age != t.age) return age < t.age;
return name < t.name;
}
};

vector<Person> ages[N];
int idx[N];

int main()
{
scanf("%d%d", &n, &m);

char name[10];
for (int i = 0; i < n; i ++ )
{
int age, w;
scanf("%s%d%d", name, &age, &w);
ages[age].push_back({name, age, w});
}

for (auto& age : ages) sort(age.begin(), age.end());

for (int C = 1; C <= m; C ++ )
{
printf("Case #%d:\n", C);
int cnt, a, b;
scanf("%d%d%d", &cnt, &a, &b);

memset(idx, 0, sizeof idx);
bool exists = false;
while (cnt -- )
{
int t = -1;
for (int i = a; i <= b; i ++ )
if (idx[i] < ages[i].size())
{
if (t == -1 || ages[i][idx[i]] < ages[t][idx[t]])
t = i;
}

if (t == -1) break;
auto& p = ages[t][idx[t]];
idx[t] ++ ;

printf("%s %d %d\n", p.name.c_str(), p.age, p.w);
exists = true;
}

if (!exists) puts("None");
}

return 0;
}


#include<iostream>
#include<cstdio>
#include<vector>
#include<algorithm>
#include<cstring>
using namespace std;
const int MAXN = 100;
struct node {
char name[9];
int age, money;
bool operator < (const node& rhs) const {
return money != rhs.money ? money > rhs.money : (age != rhs.age ? age < rhs.age : strcmp(name, rhs.name) < 0);
}
};
int n, k;
int amin, amax, num;
int book[MAXN];
int main() {
scanf("%d%d", &n, &k);
vector<node> v(n), res;
for(int i = 0; i < n; ++i) scanf("%s %d %d", v[i].name, &v[i].age, &v[i].money);
sort(v.begin(), v.end());
for(int i = 0; i < n; ++i)
if(book[v[i].age] < MAXN) {  //最多加100个
book[v[i].age]++;
res.push_back(v[i]);
}
for(int i = 1; i <= k; ++i) {
printf("Case #%d:\n", i);
vector<node> t;//重新分配空间
scanf("%d%d%d", &num, &amin, &amax);
for(int j = 0; j < res.size(); ++j) if(res[j].age >= amin && res[j].age <= amax) t.push_back(res[j]);
if(t.size() == 0) printf("None\n");
else {
for(int l = 0; l < t.size() && l < num; ++l) printf("%s %d %d\n", t[l].name, t[l].age, t[l].money);
}
}
return 0;
}



11-17 627

10-06 6780
09-10 1649
09-18 3065
02-13 1万+
09-10 1872
03-27 1万+
07-08 7776
08-12 3587
01-15 4772
03-26 1461
08-05 3362