PAT甲级——A1055 The World's Richest
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 [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
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:
第一个代码是用vector存储,但遍历超时,第二个时使用数组存储,测试通过.
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遍历时间就比数组差那么多?
#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的更多相关文章
- 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 ...
- PAT甲级1055 The World's Richest【排序】
题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805421066272768 题意: 给定n个人的名字,年龄和身价. ...
- PAT甲级题解(慢慢刷中)
博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6102219.html特别不喜欢那些随便转载别人的原创文章又不给 ...
- 【转载】【PAT】PAT甲级题型分类整理
最短路径 Emergency (25)-PAT甲级真题(Dijkstra算法) Public Bike Management (30)-PAT甲级真题(Dijkstra + DFS) Travel P ...
- PAT甲级题分类汇编——排序
本文为PAT甲级分类汇编系列文章. 排序题,就是以排序算法为主的题.纯排序,用 std::sort 就能解决的那种,20分都算不上,只能放在乙级,甲级的排序题要么是排序的规则复杂,要么是排完序还要做点 ...
- PAT甲级题分类汇编——序言
今天开个坑,分类整理PAT甲级题目(https://pintia.cn/problem-sets/994805342720868352/problems/type/7)中1051~1100部分.语言是 ...
- PAT甲级1131. Subway Map
PAT甲级1131. Subway Map 题意: 在大城市,地铁系统对访客总是看起来很复杂.给你一些感觉,下图显示了北京地铁的地图.现在你应该帮助人们掌握你的电脑技能!鉴于您的用户的起始位置,您的任 ...
- PAT甲级1127. ZigZagging on a Tree
PAT甲级1127. ZigZagging on a Tree 题意: 假设二叉树中的所有键都是不同的正整数.一个唯一的二叉树可以通过给定的一对后序和顺序遍历序列来确定.这是一个简单的标准程序,可以按 ...
- PAT甲级1123. Is It a Complete AVL Tree
PAT甲级1123. Is It a Complete AVL Tree 题意: 在AVL树中,任何节点的两个子树的高度最多有一个;如果在任何时候它们不同于一个,则重新平衡来恢复此属性.图1-4说明了 ...
随机推荐
- 尚学python课程---14、python中级语法
尚学python课程---14.python中级语法 一.总结 一句话总结: var[1:5] 访问模式:比如字符串,比如列表元祖,字典等 del 删除模式:比如列表.元祖.字典 1.Python的N ...
- JSF(JavaServer Faces)简介
JavaServer Faces (JSF) 是一种用于构建Java Web 应用程序的标准框架(是Java Community Process 规定的JSR-127标准).它提供了一种以组件为中心的 ...
- poj-3468-A Simple Problem with Integers-线段树入门+区间更新
You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of op ...
- PAT甲级——A1132 Cut Integer
Cutting an integer means to cut a K digits lone integer Z into two integers of (K/2) digits long int ...
- import socketserver 模块 (27-03)
使用socketserver实现并发聊天 服务端可以比喻做一部电话. ("127.0.0.1", 8000) 比喻做服务端的一个号码. 1,server.py import soc ...
- 6_4.springboot2.x数据整合springData介绍
介绍 Spring Data 项目的目的是为了简化构建基于Spring 框架应用的数据访问技术,包括非关系数据库.Map-Reduce 框架.云数据服务等等:另外也包含对关系数据库的访问支持. spr ...
- springcloud(十五):服务网关zuul
前面的文章我们介绍了,Eureka用于服务的注册于发现,Feign支持服务的调用以及均衡负载,Hystrix处理服务的熔断防止故障扩散,Spring Cloud Config服务集群配置中心,似乎一个 ...
- MyBatis与Hibernate
Mybatis和hibernate不同,它不完全是一个ORM框架,因为MyBatis需要程序员自己编写Sql语句.mybatis可以通过XML或注解方式灵活配置要运行的sql语句,并将java对象和s ...
- 廖雪峰Java13网络编程-2Email编程-2接收Email
1接收Email协议类型 接收Email:收件人通过MUA软件把邮件从MDA抓取到本地计算机的过程. 1.1 POP3 从MUA到MDA使用最广泛的是协议是POP3 Post Office Proto ...
- 容斥原理——状态压缩zoj3233 zoj2836升级版
zoj2836就是裸的求lcm进行容斥,用dfs比较直观 zoj3233增加了一个集合b,lcm(b)的倍数是不符合条件的 那么在zoj2836的基础上,把lcm(x,lcm(b))造成的影响减去即可 ...