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 ...
随机推荐
- 分布式图数据库 Nebula Graph 的 Index 实践
导读 索引是数据库系统中不可或缺的一个功能,数据库索引好比是书的目录,能加快数据库的查询速度,其实质是数据库管理系统中一个排序的数据结构.不同的数据库系统有不同的排序结构,目前常见的索引实现类型如 B ...
- IntelliJ IDEA 2020 全家桶注册码
WU78YHTY7E-eyJsaWNlbnNlSWQiOiJPUVQzT0oyNVhFIiwibGljZW5zZWVOYW1lIjoi5rC45LmF5r+A5rS7IGlkZWEubWVkZW1pb ...
- 简说python之安装
Python是跨平台程序语言,做为世界流行的语言之一,它可以平滑地部署在Windows,Linux,Mac等平台之上,并有很多第三方模块的函数可供使用. 学习Python,首先需要把Python的编译 ...
- OpenCV3入门(十四)图像特效—挤压、哈哈镜、扭曲
一.图像挤压特效 1.原理 图像压效果本质的图像坐标的非线性变换,将图像向内挤压,挤压的过程产生压缩变形,从而形成的效果. 挤压效果的实现是通过极坐标的形式,设图像中心为O(x,y),某点距离中心O的 ...
- MATLAB神经网络(6) PID神经元网络解耦控制算法——多变量系统控制
6.1 案例背景 6.1.1 PID神经元网络结构 PID神经元网络从结构上可以分为输入层.隐含层和输出层三层,$n$个控制量的PID神经元网络包含$n$个并列的相同子网络,各个子网络间既相互独立,又 ...
- 使用twisted将mysql插入变成异步执行
python 异步MySQL存库 对于异步框架而言,这些延迟是无法接受的.因此, Twisted 提供了 twisted.enterprise.adbapi, 遵循DB-API 2.0协议的一个异 ...
- python基础学习day03
基础数据类型总览 why:机器无法像人一样分编各种类型 int(数字) str(字符串)作用:存储少量信息. '12','我和你','qw' bool值 作用:判断真假 True False list ...
- js遍历删除对象的key
// 如果用户没有填写值,则删除对象的key. Object.keys(obj).forEach( (key) => { if (!obj[key]) { // !obj[key]表示 ...
- Ubuntu的BEEP去哪里了?
一直知道ubuntu的beep不响应了,但是一直都没太关注过它怎么了. 今天关注了一下,发现网上都是在问怎么关掉它的,时间还是在07年左右. 搜索到了一些帖子,有一些是说没有找到恢复的方法,还有一些, ...
- 动态规划/MinMax-Predict the Winner
2018-04-22 19:19:47 问题描述: Given an array of scores that are non-negative integers. Player 1 picks on ...