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

  1. 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:

  1. 12 4
  2. Zoe_Bill 35 2333
  3. Bob_Volk 24 5888
  4. Anny_Cin 95 999999
  5. Williams 30 -22
  6. Cindy 76 76000
  7. Alice 18 88888
  8. Joe_Mike 32 3222
  9. Michael 5 300000
  10. Rosemary 40 5888
  11. Dobby 24 5888
  12. Billy 24 5888
  13. Nobody 5 0
  14. 4 15 45
  15. 4 30 35
  16. 4 5 95
  17. 1 45 50

Sample Output:

  1. Case #1:
  2. Alice 18 88888
  3. Billy 24 5888
  4. Bob_Volk 24 5888
  5. Dobby 24 5888
  6. Case #2:
  7. Joe_Mike 32 3222
  8. Zoe_Bill 35 2333
  9. Williams 30 -22
  10. Case #3:
  11. Anny_Cin 95 999999
  12. Michael 5 300000
  13. Alice 18 88888
  14. Cindy 76 76000
  15. Case #4:
  16. None
  17.  
  18. 题目分析:写之前认为暴力排序再遍历选取满足条件的会超时 就用了认为稍微好一点的做法 。。但第三个测试点超时了 果然还是不行
    但学到了用upper_boundlower_bound排序
  19.  
  20. 第三个点超时的
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <climits>
  3. #include<iostream>
  4. #include<vector>
  5. #include<queue>
  6. #include<map>
  7. #include<set>
  8. #include<stack>
  9. #include<algorithm>
  10. #include<string>
  11. #include<cmath>
  12. using namespace std;
  13. struct Person{
  14. string Name;
  15. int Age;
  16. int Net_Worth;
  17. Person(){}
  18. Person(string name,int a,int b):Name(name),Age(a),Net_Worth(b){}
  19. bool operator<(const Person p)const {
  20. return Age < p.Age;
  21. }
  22. };
  23. bool compare(const Person& a, const Person& b)
  24. {
  25. return (a.Net_Worth != b.Net_Worth) ? a.Net_Worth > b.Net_Worth :
  26. (a.Age != b.Age) ? a.Age < b.Age : a.Name < b.Name;
  27. }
  28. bool com(const Person& a, const Person& b)
  29. {
  30. return a.Age < b.Age;
  31. }
  32. int main()
  33. {
  34. int N, K;
  35. cin >> N >> K;
  36. vector<Person> V(N);
  37. for (int i = ; i < N; i++)
  38. cin >> V[i].Name >> V[i].Age >> V[i].Net_Worth;
  39. sort(V.begin(), V.end(), com);
  40. for (int i = ; i <= K; i++)
  41. {
  42. int max_count, Amin, Amax;
  43. int begin, end;
  44. cin >> max_count >>Amin >> Amax;
  45. begin = lower_bound(V.begin(), V.end(),Person("",Amin,)) - V.begin();
  46. end = upper_bound(V.begin(), V.end(),Person("",Amax,)) - V.begin();
  47. vector<Person> Ans;
  48. Ans.insert(Ans.begin(), V.begin() + begin, V.begin() + end);
  49. sort(Ans.begin(), Ans.end(), compare);
  50. printf("Case #%d:\n", i);
  51. if (!Ans.size())
  52. cout << "None"<<endl;
  53. else
  54. {
  55. for (int i = ; i <Ans.size()&&i<max_count; i++)
  56. cout << Ans[i].Name << " " << Ans[i].Age << " " << Ans[i].Net_Worth << endl;
  57. }
  58. }
  59. }

改了之后还是没过。。。。这次是第二个点 看人家用的是数组 我用的Vector ......

  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <climits>
  3. #include<iostream>
  4. #include<vector>
  5. #include<queue>
  6. #include<map>
  7. #include<set>
  8. #include<stack>
  9. #include<algorithm>
  10. #include<string>
  11. #include<cmath>
  12. using namespace std;
  13. struct Person{
  14. string Name;
  15. int Age;
  16. int Net_Worth;
  17. };
  18. bool compare(const Person& a, const Person& b)
  19. {
  20. return (a.Net_Worth != b.Net_Worth) ? a.Net_Worth > b.Net_Worth :
  21. (a.Age != b.Age) ? a.Age < b.Age : a.Name < b.Name;
  22. }
  23. int main()
  24. {
  25. ios::sync_with_stdio(false);
  26. int N, K;
  27. cin >> N >> K;
  28. vector<Person> V(N);
  29. for (int i = ; i < N; i++)
  30. cin >> V[i].Name >> V[i].Age >> V[i].Net_Worth;
  31. sort(V.begin(), V.end(), compare);
  32. for (int i = ; i <= K; i++)
  33. {
  34. int max_count, Amin, Amax;
  35. cin >> max_count >>Amin >> Amax;
  36. cout << "Case #" << i << ":" << endl;
  37. int count = ;
  38. for (int i = ; i < N; i++)
  39. {
  40. if (V[i].Age >= Amin && V[i].Age <= Amax)
  41. {
  42. cout << V[i].Name << " " << V[i].Age << " " << V[i].Net_Worth << endl;
  43. count++;
  44. }
  45. if (count == max_count)
  46. break;
  47. }
  48. if (!count)
  49. cout << "None" << endl;
  50. }
  51. }

1055 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. 【PAT甲级】1055 The World's Richest (25 分)

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

  4. PATA1055 The World's Richest (25 分)

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

  5. 1036 Boys vs Girls (25分)(水)

    1036 Boys vs Girls (25分)   This time you are asked to tell the difference between the lowest grade o ...

  6. PAT 甲级 1028 List Sorting (25 分)(排序,简单题)

    1028 List Sorting (25 分)   Excel can sort records according to any column. Now you are supposed to i ...

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

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

  8. 1055. The World's Richest (25)

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

  9. PAT A1055 The World's Richest (25 分)——排序

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

随机推荐

  1. 开篇词The Start以及[Vjudge][HDU2242]空调教室

    开篇 这是我写的第一篇记录好题的博客,也是博客园上我发布的第一篇博客. 以后我的所有博客都将在洛谷和博客园上同时发布,同志们有兴趣的在哪里都可以看一看. [https://www.luogu.com. ...

  2. MATLAB神经网络(3) 遗传算法优化BP神经网络——非线性函数拟合

    3.1 案例背景 遗传算法(Genetic Algorithms)是一种模拟自然界遗传机制和生物进化论而形成的一种并行随机搜索最优化方法. 其基本要素包括:染色体编码方法.适应度函数.遗传操作和运行参 ...

  3. ES6中的find与filter的区别

    一直以来以为find和filter是一样的效果,最近在梳理,才发现是不一样的. 首先,filter和find区别:filter返回的是数组,find返回的是对象. 注意:find()找到第一个元素后就 ...

  4. js 数组一些简单应用

    把两个数组连接成按从小到大的一个数组例如: var allowVlan = '23-25,45,4-6,67,50-53'; var unTagVlan = '1-5'; 完成时应该是1-6,23-2 ...

  5. 数据库开发 Oracle与mysql间的批量处理接口 SSIS+存储过程实现

    公司目前不同的业务系统用了不同的数据库,涉及到oracle.mysql.sqlserver.而一些核心的业务在mysql中,所以平时经常要把oracle.sqlserver中的数据插入到mysql中. ...

  6. C++总结之template

    函数模板 我们可以把函数模板当做一种特殊的函数,里面的参数类型可以是任意类型,这样的话我们就可以减少重复定义,从而让这个函数模板自动适应不同的参数类型,也就是说函数可以适应多种类型的参数,例如doub ...

  7. 【springboot spring mybatis】看我怎么将springboot与spring整合mybatis与druid数据源

    目录 概述 1.mybatis 2.druid 壹:spring整合 2.jdbc.properties 3.mybatis-config.xml 二:java代码 1.mapper 2.servic ...

  8. docker安装mysql主从

    docker安装mysql主从 启动主库: 1.docker run --name master -p 3306:3306 -e MYSQL_ROOT_PASSWORD=root -d mysql:5 ...

  9. MyBatis框架——逆向工程

    什么是逆向工程? 逆向工程师MyBatis提供的一种自动化配置方案,针对数据表自动生成MyBatis所需的各种资源,包括实体类.Mapper接口.Mapper.xml,但是逆向工程的缺陷在于只能针对单 ...

  10. Dubbo 扩展点加载机制:从 Java SPI 到 Dubbo SPI

    SPI 全称为 Service Provider Interface,是一种服务发现机制.当程序运行调用接口时,会根据配置文件或默认规则信息加载对应的实现类.所以在程序中并没有直接指定使用接口的哪个实 ...