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 (≤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 [−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

技术总结

  • 题意:主要是讲,有N个富豪,然后分K次查询,每个查询是分好年龄小组的,每组最多输出前M位(M小于100)

  1. 想法一:之前想的是,首先建立一个数组结构体然后存下所有的富豪信息,最后再建立K个同样的结构体,用来存放每个年龄段的富豪,再对这个年龄段的富豪排序,输出前M位 。但是这样会超时,也许是时间复杂太高了吧
  2. 想法二:就是首先对富豪,然后对排序后N个富豪中剔除每个年龄100名以后的,因为每一组最多输出前一百名。最后就是输出,直接输出就好了。具体看代码,下面是方法二的代码。

  • 第一步建立富豪的结构体,还有存放每一小组输出要求的结构体。
  • 第二步读入信息。
  • 第三部进行算法具体实现也就是方法二。
  • 第四步根据要求输出。
  • 注意事项:看清具体要求,输出时是否有换行符,是否有空格。

参考代码:

#define _CRT_SECURE_NO_WARNINGS
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; const int MAX_N = 100010;
const int MAX_K = 1010;
struct Billionaires {//用于存储人物信息
char name[10];
int age;
int money;
}Billion[MAX_N], rangeB[MAX_N]; int Age[MAX_N] = { 0 };
struct Groups {
int num;//每一小组的前多少名
int minage;//最小年龄
int maxage;//最大年龄
}Group[MAX_K]; //struct Billionaires ;//可以将每一段年龄的人分开到不同的小组中用于排序 //比较函数,用于判断分离年龄后的内部排序
bool cmp(Billionaires a, Billionaires b) {
int s = strcmp(a.name, b.name);
if (a.money != b.money) {
return a.money > b.money;
}
else if (a.age != b.age) {
return a.age < b.age;
}
else {
return s < 0;
}
} int main() {
int N, K;
scanf("%d%d", &N, &K);
//信息的读取写入
for (int i = 0; i < N; i++) {
scanf("%s%d%d", Billion[i].name, &Billion[i].age, &Billion[i].money);
}
for (int i = 0; i < K; i++) {
scanf("%d%d%d", &Group[i].num, &Group[i].minage, &Group[i].maxage);
}
int len = 0; sort(Billion, Billion + N, cmp);//进行排序所有成员
int validNum = 0;
//这个for循环是为了将每个年龄的前一百保留,因为在每个年龄组最多输出一百位 for (int i = 0; i < N; i++) {
if (Age[Billion[i].age] < 100) {
Age[Billion[i].age]++;
rangeB[validNum++] = Billion[i];
}
} for (int i = 0; i < K; i++) {
printf("Case #%d:\n", i + 1);
int flag = 0;//用来标记该组是否有输出对象,初始化0表示没有,1表示有输出对象
int num = 0;//用来记录已输出人数
for (int j = 0; num < Group[i].num && j < N; j++) {
if (rangeB[j].age >= Group[i].minage && rangeB[j].age <= Group[i].maxage) {
printf("%s %d %d\n", rangeB[j].name, rangeB[j].age, rangeB[j].money);
flag = 1;
num++;
}
}
if (flag == 0) {
printf("None\n");
}
}
system("pause");
return 0; }

PATA1055 The World's Richest (25 分)的更多相关文章

  1. PAT 甲级 1055 The World's Richest (25 分)(简单题,要用printf和scanf,否则超时,string 的输入输出要注意)

    1055 The World's Richest (25 分)   Forbes magazine publishes every year its list of billionaires base ...

  2. PAT (Advanced Level) Practice 1055 The World's Richest (25 分) (结构体排序)

    Forbes magazine publishes every year its list of billionaires based on the annual ranking of the wor ...

  3. 1055 The World's Richest (25分)(水排序)

    Forbes magazine publishes every year its list of billionaires based on the annual ranking of the wor ...

  4. 【PAT甲级】1055 The World's Richest (25 分)

    题意: 输入两个正整数N和K(N<=1e5,K<=1000),接着输入N行,每行包括一位老板的名字,年龄和财富.K次询问,每次输入三个正整数M,L,R(M<=100,L,R<= ...

  5. A1055 The World's Richest(25 分)

    A1055 The World's Richest(25 分) Forbes magazine publishes every year its list of billionaires based ...

  6. PTA - - 06-图1 列出连通集 (25分)

    06-图1 列出连通集   (25分) 给定一个有NN个顶点和EE条边的无向图,请用DFS和BFS分别列出其所有的连通集.假设顶点从0到N-1N−1编号.进行搜索时,假设我们总是从编号最小的顶点出发, ...

  7. 中国大学MOOC-陈越、何钦铭-数据结构-2015秋 01-复杂度2 Maximum Subsequence Sum (25分)

    01-复杂度2 Maximum Subsequence Sum   (25分) Given a sequence of K integers { N​1​​,N​2​​, ..., N​K​​ }. ...

  8. PTA 字符串关键字的散列映射(25 分)

    7-17 字符串关键字的散列映射(25 分) 给定一系列由大写英文字母组成的字符串关键字和素数P,用移位法定义的散列函数H(Key)将关键字Key中的最后3个字符映射为整数,每个字符占5位:再用除留余 ...

  9. PTA 旅游规划(25 分)

    7-10 旅游规划(25 分) 有了一张自驾旅游路线图,你会知道城市间的高速公路长度.以及该公路要收取的过路费.现在需要你写一个程序,帮助前来咨询的游客找一条出发地和目的地之间的最短路径.如果有若干条 ...

随机推荐

  1. tomcat启动项目报404错误

    1.请求的时候报404错误,而且我的请求API地址是/account/sendSmsCode,从后台获取到的竟然变成了/account/account/sendSmsCode. ​ ​ 2.后来发现是 ...

  2. 【More Effective C++ 条款3】最好不要以多态方式处理数组

    1.在数组与多态混用的情况下,数组元素的访问会出现不可预期的结果(因为数组元素的访问会使用到下标运算) 将一个子类对象数组传递给一个父类对象数组声明的函数,编译器会允许这个行为,但是由于子类对象和父类 ...

  3. C# 删除文件到回收站

    首先添加Microsoft.VisualBasic引用 程序中引用 Microsoft.VisualBasic.FileIO 来进行处理 Console.WriteLine("删除文件到回收 ...

  4. halcon机器视觉工程开发思路

    参考:halcon学习笔记——机器视觉工程应用的开发思路https://www.cnblogs.com/hanzhaoxin/archive/2013/02/15/2912879.html

  5. 安装oracle时出现的问题

    数据库引擎和几个功能安装失败后  ,重新再装还是一样,好不容易全部功能装完成后结果发现登录不了oracle!!!!!!!!!!!!! 气死人,搞了一上午才发现原来是微软账号在搞事,登录本地管理员账户就 ...

  6. Python - 注释 - 第四天

    注释 确保对模块, 函数, 方法和行内注释使用正确的风格 Python中的注释有单行注释和多行注释: Python中单行注释以 # 开头,例如: # 这是一个注释 print('Hello Pytho ...

  7. php中,5行代码实现无限级分类

    <?php /** * 此方法由@Tonton 提供 * http://my.oschina.net/u/918697 * @date 2012-12-12 */function genTree ...

  8. ES6 入门系列 (三) 尾递归

    递归我们不陌生, 那什么是尾递归呢? 为什么要用尾递归呢? 尾递归怎么用呢? 带着这三个问题我们来了解它, 我们知道递归非常耗费内存,一不小心就会发生‘栈溢出’, 相信你一定遇到过这个错误: stac ...

  9. this(this的4种指向和改变this指向的方式)

    this是Javascript语言的一个关键字. 随着函数使用场合的不同,this的值会发生变化.但是有一个总的原则,那就是this指的是,调用函数的那个对象. 1.this指向的形式4种 a.如果是 ...

  10. PO设计模式

    BasePage类: 在PO模式中抽象封装成一个BasePage类,该基类应该拥有一个只实现webdriver实例的属性. 基础页面类中包含公用方法:点击.输入.获取元素等 Page: 每个页面封装为 ...