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 ...
随机推荐
- 如何在国内离线安装Chrome扩展并科学查资料
国内离线安装Chrome扩展 这些链接是从知乎国内离线安装 Chrome 扩展程序的方法总结 - 知乎看到的, 怕这个链接失效, 在这里自己备一份: Crx4Chrome - Download CRX ...
- C++ 重载关系操作符
#include <iostream> using namespace std; class AAA { public: AAA() //默认构造 { } AAA(int id, stri ...
- git回滚到任意一个版本
1.首先查找提交的记录(-3表示显示最近的3条) git log -3 2.强制回滚到制定版本 git reset --hard 制定版本commitId 如:git reset --hard 4ba ...
- vue中计算属性中的set和get
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script> <body& ...
- gulp VS grunt
前公司代码一直用grunt部署, 偶然了解gulp后:学习gulp并用gulp和grunt在一小项目中实践,对两者之间的用法及区别有所了解后总结这一篇小博文:本文根据实战的项目配置文件,简单讲解实现相 ...
- macOS Catalina 升级软件问题
最近升级macOS Catalina系统,升级失败时多尝试几次就可以执行成功了,在使用过程中发现以下问题,大家谨慎升级!!! 存在软件启动不兼容,不存在已软件激活失效问题. 有道词典不兼容,启动异常 ...
- 基于Redis未授权访问的挖矿蠕虫分析
0x01 攻击方式 利用的是通用漏洞入侵服务器并获得相关权限,从而植入挖矿程序再进行隐藏. 通过对脚本的分析,发现黑客主要是利用 Redis未授权访问漏洞进行入侵.脚本里有个python函数. imp ...
- XiaoQi.Study项目(一)
项目地址:https://github.com/xiaoqiyaozou1/XiaoQi.Study 感谢:“老张的哲学”.“晓晨”.“杨旭”等大佬的知识分享 一.项目创建 vs 2019 创建 as ...
- Pyinstaller通过spec文件打包py程序(多个py脚本)
Pyinstaller pyinstaller是python的一个第三方模块,使用它可以将python程序打包为可执行文件,实现打包后的程序在没有python环境的机器上也可以运行.pyinstall ...
- iOS开发如何面对疫情过后的面试高峰期 !
2020年本应该是一个 "爱你.爱你"的年份!却因为 黑天鹅 给我们带来非常大的影响! 一.2020年iOS招聘数据分析 这里是 2020年3月份BOSS直聘 北京iOS招聘前几页 ...