• 1055 The World‘s Richest


    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 (≤105) - the total number of people, and K (≤103) - 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 [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:

    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

    题目大意

    给你N个人的信息 : name age rich

    查询K次

    每次输出年龄段[left,right]内,rich排前X名的人,rich相同比较age,age相同比较name


    思路

    见代码


    C/C++ 

    1. #include
    2. using namespace std;
    3. struct People{
    4. string name;
    5. int age,rich;
    6. }p;
    7. vector age[201],result;
    8. bool cmp(const People& x,const People& y){
    9. return x.rich != y.rich ? x.rich > y.rich : x.age!=y.age ? x.age
    10. }
    11. int main()
    12. {
    13. int N,M,left,right;
    14. cin >> N >> M;
    15. while (N--){
    16. cin >> p.name >> p.age >> p.rich;
    17. age[p.age].push_back(p);
    18. }
    19. for(int z=1;z<=200;z++) {
    20. sort(age[z].begin(),age[z].end(),cmp);
    21. while (age[z].size()>100) age[z].pop_back();
    22. }
    23. for(int z=1;z<=M;z++){
    24. printf("Case #%d:\n",z);
    25. cin >> N >> left >> right;
    26. while (left<=right){
    27. for(const People& x:age[left]) result.push_back(x);
    28. left++;
    29. if(result.size()>=N) break;
    30. }
    31. sort(result.begin(),result.end(),cmp);
    32. while (result.size()>N) result.pop_back();
    33. while (left<=right){
    34. for(const People& x: age[left]){
    35. if(x.rich<=result.back().rich) break;
    36. int flag = N;
    37. while (flag>0 && x.rich > result[flag-1].rich) flag--;
    38. result.insert(result.begin()+flag,x);
    39. result.pop_back();
    40. }
    41. left++;
    42. }
    43. for(const People& x: result) cout << x.name << " " << x.age << " " << x.rich << endl;
    44. if(result.empty()) puts("None");
    45. else result.clear();
    46. }
    47. return 0;
    48. }


  • 相关阅读:
    day3-day4【代码随想录】长度最小的子数组
    vue2 项目中嵌入视频
    代码中统一异常如何处理,才能让代码更清晰
    干货!半监督预训练对话模型 SPACE
    国标视频融合云平台EasyCVR视频汇聚平台关于远程控制的详细介绍
    MC Instruction Decoder
    《opencv学习笔记》-- 图像修补
    Vue实战篇二十八:实现一个手机版的购物车
    如何使用 Docker 部署 GitLab
    elasticsearch-exporter部署手册
  • 原文地址:https://blog.csdn.net/daybreak_alonely/article/details/127894778