1055. The World's Richest (25)

朱雀 2022-05-31 14:48 270阅读 0赞

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 [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

  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

题目大意:

代码:

  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<algorithm>
  4. #include<vector>
  5. using namespace std;
  6. struct node
  7. {
  8. char name[10];
  9. int age;
  10. int money;
  11. };
  12. bool cmp(struct node a,struct node b)
  13. {
  14. if(a.money!=b.money)
  15. return a.money>b.money;
  16. else if(a.age!=b.age)
  17. {
  18. return a.age<b.age;
  19. }
  20. else
  21. {
  22. return strcmp(a.name,b.name)<0;
  23. }
  24. }
  25. int n,num[201];
  26. int main()
  27. {
  28. int i,j,m,k,t,age1,age2;
  29. scanf("%d %d",&n,&m);
  30. vector<struct node> arr(n),tmp;
  31. for(i=0;i<n;i++)
  32. {
  33. scanf("%s %d %d",arr[i].name,&arr[i].age,&arr[i].money);
  34. }
  35. sort(arr.begin(),arr.end(),cmp);
  36. for(i=0;i<n;i++)
  37. {
  38. if(num[arr[i].age]<100)
  39. {
  40. tmp.push_back(arr[i]);
  41. }
  42. }
  43. for(i=1;i<=m;i++)
  44. {
  45. scanf("%d %d %d",&k,&age1,&age2);
  46. printf("Case #%d:\n",i);
  47. int flag=0;
  48. for(j=0;j<tmp.size()&&flag<k;j++)
  49. {
  50. if(tmp[j].age>=age1&&tmp[j].age<=age2)
  51. {
  52. printf("%s %d %d\n",tmp[j].name,tmp[j].age,tmp[j].money);
  53. flag++;
  54. }
  55. }
  56. if(flag==0)
  57. {
  58. printf("None\n");
  59. }
  60. }
  61. return 0;
  62. }

发表评论

表情:
评论列表 (有 0 条评论,270人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Stop-The-World

    一.概述: java对象内存申请过程: 1.JVM会试图为相关Java对象在Eden中初始化一块内存区域;当Eden空间足够时,内存申请结束。否则到下一步; 2.JVM

    相关 1055. 集体照 (25)

    拍集体照时队形很重要,这里对给定的N个人K排的队形设计排队规则如下: 每排人数为N/K(向下取整),多出来的人全部站在最后一排; 后排所有人的个子都不比前排任何人