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. 精度试验结果报告Sleep, GetTickCount, timeGetTime, QueryPerformanceCounter

    一段简单的代码来实现精度试验 int main() {       // 初始化代码       ......       int i = 0;       while(i++ < 1000) ...

  2. jeecms jeecmsv93建库

    create tablespace jeecms93 datafile 'jeecms93.dbf' size 100M reuse autoextend on next 50M;1. 2.drop ...

  3. 自动生成DTO(Sugar框架)

    step1:启动api项目 step2:使用postman工具,填上接口地址http://localhost:7788/api/automapper/AutoMapperSuper step3:表格数 ...

  4. 使用CEfSharp之旅(5)CEFSharp 隔离Cookie

    原文:使用CEfSharp之旅(5)CEFSharp 隔离Cookie 版权声明:本文为博主原创文章,未经博主允许不得转载.可点击关注博主 ,不明白的进群191065815 我的群里问 https:/ ...

  5. MediatR 知多少 - 简书

    原文:MediatR 知多少 - 简书 引言 首先不用查字典了,词典查无此词.猜测是作者笔误将Mediator写成MediatR了.废话少说,转入正题. 先来简单了解下这个开源项目MediatR(作者 ...

  6. Linux真好玩阿,不过我家电脑不行,运行不够流畅

    不过呢....能编程就行了....   哎,总说不用QT不用QT,最后还是没办法,用QT了   连个界面都看不到的话,感觉太差了....   我还不会用QT呢....好好学习....争取像MFC那样习 ...

  7. java笔试之放苹果

    题目描述:M个同样的苹果放在N个同样的盘子里,允许有的盘子空着不放,问共有多少种不同的分法?(用K表示)5,1,1和1,5,1 是同一种分法. 输入:每个用例包含二个整数M和N.0<=m< ...

  8. 【颓废篇】easyx--2048

    整天待在机房是不是,一直保持学术的态度就比较的难啊~ 所以本蒟蒻就在学术之余学了些奇技淫巧,然后就写了一些奇奇怪怪的程序啊,比如让我们小组dalao们都颓得不亦乐乎的2048~~ 当然,2048的实现 ...

  9. python3 selenium 超时停止加载,并且捕捉异常, 进行下一步【亲测有效】

    from selenium import webdriver import os import re class GetPage: def __init__(self, url_path): self ...

  10. Android开发 多媒体提取器MediaExtractor详解_入门篇

    前言 MediaExtractor字面意思是多媒体提取器,它在Android的音视频开发里主要负责提取视频或者音频中的信息和数据流(例如将视频文件,剥离出音频与视频).本章博客将讲解一些入门简单的东西 ...