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. 函数开始处的MOV EDI, EDI的作用

    调试程序调试到系统库函数的代码时,总会发现系统函数都是从一条MOV EDI, EDI指令开始的,紧接着这条指令下面才是标准的建立函数局部栈的代码.对系统DLL比如ntdll.dll进行反汇编,可以发现 ...

  2. <Django> 高级(其他知识点)

    1. 管理静态文件 什么是静态文件? 项目中的CSS.图片.js都是静态文件 配置静态文件(settings.py) # Static files (CSS, JavaScript, Images) ...

  3. 面试系列10 es生产集群的部署架构

    如果你确实干过es,那你肯定了解你们生产es集群的实际情况,部署了几台机器?有多少个索引?每个索引有多大数据量?每个索引给了多少个分片?你肯定知道! 但是如果你确实没干过,也别虚,我给你说一个基本的版 ...

  4. marquee标签(跑马灯效果)

  5. 线性回归代码实现(matlab)

    1 代价函数实现(cost function) function J = computeCost(X, y, theta) %COMPUTECOST Compute cost for linear r ...

  6. angular 级联选择

    HTML: <link rel="stylesheet" href="views/tree/checkbox.css"/> <div clas ...

  7. How to use view controller containment

    https://www.hackingwithswift.com/example-code/uikit/how-to-use-view-controller-containment private f ...

  8. 使用 /proc 文件系统

    /proc 文件系统是一个特殊的软件创建的文件系统, 内核用来输出消息到外界. /proc 下 的每个文件都绑到一个内核函数上, 当文件被读的时候即时产生文件内容. 我们已经见到 一些这样的文件起作用 ...

  9. telnet- Linux必学的60个命令

    1.作用 telnet表示开启终端机阶段作业,并登入远端主机.telnet是一个Linux命令,同时也是一个协议(远程登陆协议). 2.格式 telnet [-8acdEfFKLrx][-b][-e] ...

  10. WPF 先显示登录成功,验证成功后显示主窗口

    /// 设置显示登录窗口的方法: /// 在 App.xaml 中把这句 /// 删掉 StartupUri="MainWindow.xaml" 改为 StartupUri=&qu ...