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
第一个代码是用vector存储,但遍历超时,第二个时使用数组存储,测试通过.
搞不明白同样的算法复杂度,为什么vector遍历时间就比数组差那么多?
 #include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
struct Node
{
string name;
int age, val;
}node;
int N, K, M, num, Amin, Amax;
bool cmp(Node a, Node b)
{
if (a.val == b.val && a.age == b.age)
return a.name < b.name;
else if (a.val == b.val)
return a.age < b.age;
else
return a.val > b.val;
}
int main()
{
cin >> N >> K;
vector<Node>v;
vector<vector<Node>>res(K);
for (int i = ; i < N; ++i)
{
cin >> node.name >> node.age >> node.val;
v.push_back(node);
}
sort(v.begin(), v.end(), cmp);
for (int i = ; i < K; ++i)
{
cin >> num >> Amin >> Amax;
int flag = ;
cout << "Case #" << i + << ":" << endl;
for (auto a : v)
{
if (a.age >= Amin && a.age <= Amax)
{
cout << a.name << " " << a.age << " " << a.val << endl;
flag++;
if (flag == num)
break;
}
}
if (flag == )
cout << "None" << endl;
}
return ;
}
 #include<bits/stdc++.h>
using namespace std;
struct Person{//存储相应信息的结构体
string name;
int age,worth;
};
Person person[(int)(1e5+)];
int main(){
int N,K;
scanf("%d%d",&N,&K);
for(int i=;i<N;++i)
cin>>person[i].name>>person[i].age>>person[i].worth;
sort(person,person+N,[](const Person&p1,const Person&p2){//比较函数
if(p1.worth!=p2.worth)
return p1.worth>p2.worth;
else if(p1.age!=p2.age)
return p1.age<p2.age;
else
return p1.name<p2.name;
});//排序
for(int i=;i<=K;++i){
int M,Amin,Amax;
bool f=false;
scanf("%d%d%d",&M,&Amin,&Amax);
printf("Case #%d:\n",i);
for(int j=;j<N&&M>;++j)//遍历数组
if(person[j].age>=Amin&&person[j].age<=Amax){//找到符合要求的人
printf("%s %d %d\n",person[j].name.c_str(),person[j].age,person[j].worth);//输出
--M;//将M递减
f=true;//表示已输出过
}
if(!f)//在该年龄段没有任何输出
printf("None\n");//输出None
}
return ;
}

PAT甲级——A1055 The World's Richest的更多相关文章

  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甲级1055 The World's Richest【排序】

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

  3. PAT甲级题解(慢慢刷中)

    博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6102219.html特别不喜欢那些随便转载别人的原创文章又不给 ...

  4. 【转载】【PAT】PAT甲级题型分类整理

    最短路径 Emergency (25)-PAT甲级真题(Dijkstra算法) Public Bike Management (30)-PAT甲级真题(Dijkstra + DFS) Travel P ...

  5. PAT甲级题分类汇编——排序

    本文为PAT甲级分类汇编系列文章. 排序题,就是以排序算法为主的题.纯排序,用 std::sort 就能解决的那种,20分都算不上,只能放在乙级,甲级的排序题要么是排序的规则复杂,要么是排完序还要做点 ...

  6. PAT甲级题分类汇编——序言

    今天开个坑,分类整理PAT甲级题目(https://pintia.cn/problem-sets/994805342720868352/problems/type/7)中1051~1100部分.语言是 ...

  7. PAT甲级1131. Subway Map

    PAT甲级1131. Subway Map 题意: 在大城市,地铁系统对访客总是看起来很复杂.给你一些感觉,下图显示了北京地铁的地图.现在你应该帮助人们掌握你的电脑技能!鉴于您的用户的起始位置,您的任 ...

  8. PAT甲级1127. ZigZagging on a Tree

    PAT甲级1127. ZigZagging on a Tree 题意: 假设二叉树中的所有键都是不同的正整数.一个唯一的二叉树可以通过给定的一对后序和顺序遍历序列来确定.这是一个简单的标准程序,可以按 ...

  9. PAT甲级1123. Is It a Complete AVL Tree

    PAT甲级1123. Is It a Complete AVL Tree 题意: 在AVL树中,任何节点的两个子树的高度最多有一个;如果在任何时候它们不同于一个,则重新平衡来恢复此属性.图1-4说明了 ...

随机推荐

  1. Linux课程---13、linux中任务计划介绍(任务计划分类)

    Linux课程---13.linux中任务计划介绍(任务计划分类) 一.总结 一句话总结: 1.一次性任务计划:at 2.周期性任务计划:crontab 1.linux中如何添加一次性任务计划? at ...

  2. IEnumerable_vs_IEnumerator

    using System; using System.Collections; using System.Collections.Generic; using System.Linq; using S ...

  3. 洛谷P1792——[国家集训队]种树

    传送门:QAQQAQ 题意:$n$个点中选$m$个不相邻的点,使得这些点不相邻(1和n算相邻),求这些点的最大值 思路:这不是神仙题不是神仙题…… 刚看到这题觉得不难,好像只要贪心就可以了但贪心不知从 ...

  4. docker 可持续集成及日志管理及监控报警

  5. PAT甲级——A1138 Postorder Traversa【25】

    Suppose that all the keys in a binary tree are distinct positive integers. Given the preorder and in ...

  6. WPF drag过程中显示ToolTip.

    原文:WPF drag过程中显示ToolTip. 在drag/drop过程中,我们在判断出over的元素上是否可以接受drag的东西之后,通常是通过鼠标的样式简单告诉用户这个元素不接受现在drag的内 ...

  7. Spring MVC上传、下载 文件

    1,上传文件 public static String upload(MultipartFile file, SysUserBean sysUserBean, HttpServletRequest r ...

  8. h5对接jssdk支付分并调用开启支付分页面

    1.ws.config签名   调用ticket等获取ws.config的签名,下面会调用方法再调用方法时需要再次按照调用方法的签名 wx.config({ debug: true, // 开启调试模 ...

  9. thinkphp 数据写入

    直线电机优势 ThinkPHP的数据写入操作使用add方法,使用示例如下: $User = M("User"); // 实例化User对象 $data['name'] = 'Thi ...

  10. 「题解」:[BZOJ4558]方

    问题: 方 时间限制: 2 Sec  内存限制: 256 MB 题面 题目描述 上帝说,不要圆,要方,于是便有了这道题.由于我们应该方,而且最好能够尽量方,所以上帝派我们来找正方形 上帝把我们派到了一 ...