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. c# 中xml序列化时相同节点存入不同类型值

    先上需要序列话的类定义: [System.Xml.Serialization.XmlIncludeAttribute(typeof(DescriptionType))] [System.CodeDom ...

  2. UVA 511 Do You Know the Way to San Jose?

    题目链接:https://vjudge.net/problem/UVA-511 题目翻译摘自<算法禁赛入门经典> 题目大意 有 n 张地图(已知名称和某两个对角线端点的坐标)和 m 个地名 ...

  3. 小程序跳坑 --- navigator 和 API中wx.系列的跳转(如 wx.navigateTo、wx.reLaunch等)

    工作之余,想着帮老妈开发个小程序,一是宣传一下她的业务,二是学习使用一下微信小程序的开发,哈哈.在此过程中遇到了navigator跳转的问题,最终还是成功解决了,下面就记录下来,并做个系列总结以作记录 ...

  4. mybatis框架中XxxxMaper.xml的文件

    我们知道在mybatis框架中,config.xml中会关联到许多的XxxxMapper的xml文件,这些文件又对应着一个个的接口,来观察下这些xml文件 从以下这个文件为例子: <?xml v ...

  5. 《初识Python之认识常量type函数》

    <初识Python之认识常量type函数> 1.2 认识常量 1.常量:我们用的就是它字面意义上的值或内容. 2.数字(Number) (1)整数表示:97. (2)浮点数表示:5.29 ...

  6. Hadoop2.7.1配置NameNode+ResourceManager高可用原理分析

    关于NameNode高可靠需要配置的文件有core-site.xml和hdfs-site.xml 关于ResourceManager高可靠需要配置的文件有yarn-site.xml 逻辑结构: Nam ...

  7. nodejs和vuejs的关系

    转自:https://blog.csdn.net/myKurt/article/details/79914078 nodejs类比Java中:JVM 详述: 就前端来说nodejs具有划时代的意义, ...

  8. 内核下枚举进程 (二)ZwQuerySystemInformation

    说明: SYSTEM_INFORMATION_CLASS 的5号功能枚举进程信息.其是这个函数对应着ring3下的 NtQuerySystemInformation,但msdn上说win8以后ZwQu ...

  9. axios——post请求时把对象obj数据转为formdata格式

    转载自:https://blog.csdn.net/feizhong_web/article/details/80514436  在调用后台接口的时候,上传报名信息,利用axios 的post请求,发 ...

  10. 【核心核心】10.Spring事务管理【TX】XML+注解方式

    转账案例环境搭建 1.引入JAR包 IOC的6个包 AOP的4个包 C3P0的1个包 MySQL的1个驱动包 JDBC的2个目标包 整合JUnit测试1个包 2.引入配置文件 log4j.proper ...