1055 The World's Richest (25分)(水排序)
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:
题目分析:写之前认为暴力排序再遍历选取满足条件的会超时 就用了认为稍微好一点的做法 。。但第三个测试点超时了 果然还是不行
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
但学到了用upper_bound和lower_bound排序 第三个点超时的
#define _CRT_SECURE_NO_WARNINGS
#include <climits>
#include<iostream>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<stack>
#include<algorithm>
#include<string>
#include<cmath>
using namespace std;
struct Person{
string Name;
int Age;
int Net_Worth;
Person(){}
Person(string name,int a,int b):Name(name),Age(a),Net_Worth(b){}
bool operator<(const Person p)const {
return Age < p.Age;
}
};
bool compare(const Person& a, const Person& b)
{
return (a.Net_Worth != b.Net_Worth) ? a.Net_Worth > b.Net_Worth :
(a.Age != b.Age) ? a.Age < b.Age : a.Name < b.Name;
}
bool com(const Person& a, const Person& b)
{
return a.Age < b.Age;
}
int main()
{
int N, K;
cin >> N >> K;
vector<Person> V(N);
for (int i = ; i < N; i++)
cin >> V[i].Name >> V[i].Age >> V[i].Net_Worth;
sort(V.begin(), V.end(), com);
for (int i = ; i <= K; i++)
{
int max_count, Amin, Amax;
int begin, end;
cin >> max_count >>Amin >> Amax;
begin = lower_bound(V.begin(), V.end(),Person("",Amin,)) - V.begin();
end = upper_bound(V.begin(), V.end(),Person("",Amax,)) - V.begin();
vector<Person> Ans;
Ans.insert(Ans.begin(), V.begin() + begin, V.begin() + end);
sort(Ans.begin(), Ans.end(), compare);
printf("Case #%d:\n", i);
if (!Ans.size())
cout << "None"<<endl;
else
{
for (int i = ; i <Ans.size()&&i<max_count; i++)
cout << Ans[i].Name << " " << Ans[i].Age << " " << Ans[i].Net_Worth << endl;
}
}
}
改了之后还是没过。。。。这次是第二个点 看人家用的是数组 我用的Vector ......
#define _CRT_SECURE_NO_WARNINGS
#include <climits>
#include<iostream>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<stack>
#include<algorithm>
#include<string>
#include<cmath>
using namespace std;
struct Person{
string Name;
int Age;
int Net_Worth;
};
bool compare(const Person& a, const Person& b)
{
return (a.Net_Worth != b.Net_Worth) ? a.Net_Worth > b.Net_Worth :
(a.Age != b.Age) ? a.Age < b.Age : a.Name < b.Name;
}
int main()
{
ios::sync_with_stdio(false);
int N, K;
cin >> N >> K;
vector<Person> V(N);
for (int i = ; i < N; i++)
cin >> V[i].Name >> V[i].Age >> V[i].Net_Worth;
sort(V.begin(), V.end(), compare);
for (int i = ; i <= K; i++)
{
int max_count, Amin, Amax;
cin >> max_count >>Amin >> Amax;
cout << "Case #" << i << ":" << endl;
int count = ;
for (int i = ; i < N; i++)
{
if (V[i].Age >= Amin && V[i].Age <= Amax)
{
cout << V[i].Name << " " << V[i].Age << " " << V[i].Net_Worth << endl;
count++;
}
if (count == max_count)
break;
}
if (!count)
cout << "None" << endl;
}
}
1055 The World's Richest (25分)(水排序)的更多相关文章
- 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 (Advanced Level) Practice 1055 The World's Richest (25 分) (结构体排序)
Forbes magazine publishes every year its list of billionaires based on the annual ranking of the wor ...
- 【PAT甲级】1055 The World's Richest (25 分)
题意: 输入两个正整数N和K(N<=1e5,K<=1000),接着输入N行,每行包括一位老板的名字,年龄和财富.K次询问,每次输入三个正整数M,L,R(M<=100,L,R<= ...
- PATA1055 The World's Richest (25 分)
1055 The World's Richest (25 分) Forbes magazine publishes every year its list of billionaires based ...
- 1036 Boys vs Girls (25分)(水)
1036 Boys vs Girls (25分) This time you are asked to tell the difference between the lowest grade o ...
- PAT 甲级 1028 List Sorting (25 分)(排序,简单题)
1028 List Sorting (25 分) Excel can sort records according to any column. Now you are supposed to i ...
- PAT甲题题解-1055. The World's Richest (25)-终于遇见一个排序的不水题
题目简单,但解题的思路需要转换一下,按常规思路肯定超时,推荐~ 题意:给出n个人的姓名.年龄和拥有的钱,然后进行k次查询,输出年龄在[amin,amx]内的前m个最富有的人的信息.如果财富值相同就就先 ...
- 1055. The World's Richest (25)
Forbes magazine publishes every year its list of billionaires based on the annual ranking of the wor ...
- PAT A1055 The World's Richest (25 分)——排序
Forbes magazine publishes every year its list of billionaires based on the annual ranking of the wor ...
随机推荐
- Flink系列之状态及检查点
Flink不同于其他实时计算的框架之处是它可以提供针对不同的状态进行编程和计算.本篇文章的主要思路如下,大家可以选择性阅读. 1. Flink的状态分类及不同点. 2. Flink针对不同的状态进行编 ...
- js中(event)事件对象
事件对象 • 什么是事件对象? • 就是当你触发了一个事件以后,对该事件的一些描述信息 • 例如: ° 你触发一个点击事件的时候,你点在哪个位置了,坐标是多少 ° 你触发一个键盘事件的时候,你按的是哪 ...
- TCP粘包很难么,为何我屡屡受挫??
无论走到哪里,都应该记住,过去都是假的,回忆是一条没有尽头的路,一切以往的春天都不复存在,就连那最坚韧而又狂乱的爱情归根结底也不过是一种转瞬即逝的现实. --马尔克斯 本文已经收录至我的GitHub, ...
- 阿里云centos安装oracle
目录 阿里云centos安装oracle 阿里云默认没有swap分区,oracle安装需要 安装Oracle所需的依赖包 创建用户和组 关闭SELINUX(阿里云缺省关闭) 开始安装 使用" ...
- CF1082B Vova and Trophies 题解
CF1082B Vova and Trophies 题解 瞎搞题,推荐的,一看是道水题,就随手A了-- 题目描述 Vova has won \(n\)trophies in different com ...
- java idea spring mvc 入门 最起码 我8080跑起来了
IDEA建立Spring MVC Hello World 详细入门教程 https://www.cnblogs.com/wormday/p/8435617.html
- 【Weiss】【第03章】链表例程
这种基础例程,如之前所提,会有一个实现和一个简单的测试代码. 链表其实没什么可说的,其实包括后面的栈和队列也没什么可说的,直接放代码吧. 下面这个是测试代码 #include <iostream ...
- 【股票盯盘软件】01_程序员炒股之开发一款极简风格的股票盯盘软件StockDog_V1.0.0.1
1.前言 话说最近一段时间受疫情的影响,股市各种妖魔横行.本人作为一个入股市不满三年的小韭菜,就有幸见证了好几次历史,也是满心惊喜,就权当是接受资本市场的再教育了吧. 小韭菜的炒股方法其实很简单,这两 ...
- java-乘法口诀表。(新手)
//创建的一个包名. package qige; //定义一个类. public class KJ { //公共静态的一个主方法. public static void main(String[] a ...
- docker-ce 在windows10下使用volume的注意事项
最近想搭建一套CI/CD环境尝试一下,因为手里云服务太小了(1C1G),撑不起来gitlab和jenkins.恰巧年前配了台高配版的windows机器,就想在家里的机器上通过docker装gitlab ...