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 (≤) - the total number of people, and K (≤) - 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 [−]) of a person. Finally there are K lines of queries, each contains three positive integers: M (≤) - the maximum number of outputs, and [AminAmax] 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 [AminAmax]. 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

题意:

  按照所给的要求排序。

思路:

  最朴素的方法就是先按照年龄排序,然后再将所在年龄区间的人数再次排序。但是这样做的话有一组数据会超时。看了别人的blog后发现,问题出在每次查询的人数k < 100. 如果将所有满足要求年龄区间的人数都算进去的话,数字要远大于100,所以要先进行预处理,即先按照要求排序,再将让每一个年龄的人数最多有一百个。这样处理过之后,如果用cin和cout还是有一组数据过不了,换成scanf和printf就能够通过了。但是换过之后有一个比较容易卡到的点就是比较两个char数组的方法,这里我们使用strcmp()函数。

C 库函数 int strcmp(const char *str1, const char *str2) 把 str1 所指向的字符串和 str2 所指向的字符串进行比较。

该函数返回值如下:

  • 如果返回值小于 0,则表示 str1 小于 str2。
  • 如果返回值大于 0,则表示 str1 大于 str2。
  • 如果返回值等于 0,则表示 str1 等于 str2。

Code:

 1 #include <bits/stdc++.h>
2
3 using namespace std;
4
5 struct People {
6 char name[10];
7 int age;
8 int netWorth;
9 };
10
11 bool cmp(People a, People b) {
12 if (a.netWorth != b.netWorth)
13 return a.netWorth > b.netWorth;
14 else if (a.age != b.age)
15 return a.age < b.age;
16 else
17 return (strcmp(a.name, b.name) < 0);
18 }
19
20 int main() {
21 int n, k;
22 scanf("%d%d", &n, &k);
23 vector<People> v(n);
24 for (int i = 0; i < n; ++i)
25 scanf("%s%d%d", v[i].name, &v[i].age, &v[i].netWorth);
26 sort(v.begin(), v.end(), cmp);
27 vector<People> temp;
28 vector<int> nums(205, 0);
29 for (int i = 0; i < n; ++i) {
30 if (nums[v[i].age] < 100) {
31 temp.push_back(v[i]);
32 nums[v[i].age]++;
33 }
34 }
35 int m, Amin, Amax, cnt = 1;
36 while (cnt <= k) {
37 scanf("%d%d%d", &m, &Amin, &Amax);
38 vector<People> res;
39 for (int i = 0; i < temp.size(); ++i) {
40 if (temp[i].age >= Amin && temp[i].age <= Amax)
41 res.push_back(temp[i]);
42 }
43 printf("Case #%d:\n", cnt++);
44 for (int i = 0; i < res.size() && i < m; ++i) {
45 printf("%s %d %d\n", res[i].name, res[i].age, res[i].netWorth);
46 }
47 if (res.empty()) printf("None\n");
48 }
49
50 return 0;
51 }

1055 The World's Richest的更多相关文章

  1. PAT 1055 The World's Richest[排序][如何不超时]

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

  2. 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 ...

  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(Advanced Level)1055.The World's Richest

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

  5. 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 ...

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

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

  7. PAT (Advanced Level) 1055. The World's Richest (25)

    排序.随便加点优化就能过. #include<iostream> #include<cstring> #include<cmath> #include<alg ...

  8. PAT甲级1055 The World's Richest【排序】

    题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805421066272768 题意: 给定n个人的名字,年龄和身价. ...

  9. PAT甲题题解-1055. The World's Richest (25)-终于遇见一个排序的不水题

    题目简单,但解题的思路需要转换一下,按常规思路肯定超时,推荐~ 题意:给出n个人的姓名.年龄和拥有的钱,然后进行k次查询,输出年龄在[amin,amx]内的前m个最富有的人的信息.如果财富值相同就就先 ...

随机推荐

  1. MySQL连接中出现的问题

    异常: Establishing SSL connection without server's identityverification is not recommended. According ...

  2. 箭头函数this的指向

    箭头函数的this 什么是箭头函数,箭头函数是es6的新特性,其出现就是为了更好的表示(代替)回调函数 // 箭头函数 (arg1, arg2) => {} // 当箭头函数只有一个参数 arg ...

  3. 【转载】Java泛型详解

    [转载]http://www.importnew.com/24029.html 对java的泛型特性的了解仅限于表面的浅浅一层,直到在学习设计模式时发现有不了解的用法,才想起详细的记录一下. 本文参考 ...

  4. 【白话科普】CDN & 游戏加速器,两者是一个原理吗?

    说起加速,大家可能就会联想到"游戏加速"之类的场景,而说到现在流行的云服务加速,则离不开 CDN 这个词.那么 CDN 和游戏加速器是同一种东西么?从效果上看两者都是为了" ...

  5. 任务队列 与 Celery概述

    一.任务队列(Task Queues) 1.1 什么是任务队列? 任务队列用于管理后台工作,通常这些后台工作必须在 HTTP请求-响应循环 之外执行. 1.2 为什么需要任务队列? 对于那些不是由客户 ...

  6. C#关于个Base64,MD5,16进制的转换

    1,待签名数据以UTF-8的格式转字节流,对字节流进行MD5算法得到的签名字节流,再转换为16进制字符串,即生成了数字签名. byte[] targetData = md5.ComputeHash(S ...

  7. mongodb 聚合(Map-Reduce)

    介绍 Map-reduce 是一种数据处理范式,用于将大量数据压缩为有用的聚合结果.对于 map-reduce 操作,MongoDB 提供MapReduce数据库命令. MongoDB中的MapRed ...

  8. Elasticsearch 复合查询——多字符串多字段查询

    前言 有时我们在搜索电影的时候,包含了多个条件,比如主演是周星驰,打分8分以上,上映时间是1990年~2001年的,那么Elasticsearch又该如何帮我们做查询呢?这里我们可以用 bool 查询 ...

  9. python-给一个参数n,例如3:先输出1,2,3,4,5,6,7,8,9,每三个数后换行,后输出1,4,7,2,5,8,3,6,9

    """ 2 定义一个函数,fn(n)其中n表示输入n行n列的矩阵,需要满足的要求是在n为 3时先输出 3 1 2 3 4 4 5 6 5 7 8 9 6 后输出 7 1 ...

  10. F - Fluctuation Limit HDU - 6860

    题目链接:https://vjudge.net/problem/HDU-6860 题意:相邻两天的差值的绝对值不超过K. 思路:该题的关键在于前面的点会影响后面的点,后面的点会影响前面的点,我们要找到 ...