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
题目分析:写之前认为暴力排序再遍历选取满足条件的会超时 就用了认为稍微好一点的做法 。。但第三个测试点超时了 果然还是不行
但学到了用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分)(水排序)的更多相关文章

  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 (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 ...

  3. 【PAT甲级】1055 The World's Richest (25 分)

    题意: 输入两个正整数N和K(N<=1e5,K<=1000),接着输入N行,每行包括一位老板的名字,年龄和财富.K次询问,每次输入三个正整数M,L,R(M<=100,L,R<= ...

  4. PATA1055 The World's Richest (25 分)

    1055 The World's Richest (25 分) Forbes magazine publishes every year its list of billionaires based ...

  5. 1036 Boys vs Girls (25分)(水)

    1036 Boys vs Girls (25分)   This time you are asked to tell the difference between the lowest grade o ...

  6. PAT 甲级 1028 List Sorting (25 分)(排序,简单题)

    1028 List Sorting (25 分)   Excel can sort records according to any column. Now you are supposed to i ...

  7. PAT甲题题解-1055. The World's Richest (25)-终于遇见一个排序的不水题

    题目简单,但解题的思路需要转换一下,按常规思路肯定超时,推荐~ 题意:给出n个人的姓名.年龄和拥有的钱,然后进行k次查询,输出年龄在[amin,amx]内的前m个最富有的人的信息.如果财富值相同就就先 ...

  8. 1055. The World's Richest (25)

    Forbes magazine publishes every year its list of billionaires based on the annual ranking of the wor ...

  9. PAT A1055 The World's Richest (25 分)——排序

    Forbes magazine publishes every year its list of billionaires based on the annual ranking of the wor ...

随机推荐

  1. IOS手动添加的View 在代码中使用(自动布局)autoLayout

    - (void)viewDidLoad { [super viewDidLoad]; UIButton *btnTest = [UIButton buttonWithType:UIButtonType ...

  2. 安装msyql报错——error: Failed dependencies

    报错原因: 1.存在两个版本的msyql-community-release. 解决方法: 1.将不要的哪个进行去除,使用命令: rpm -e --nodeps mysql80-community-r ...

  3. 网址封锁的几种方法 公司把 pan.baidu.com 封了 研究实现原理

    HTTP 和 HTTPS 协议HTTP 协议在 头部会发送 host 就是要访问的域名,可以用来被检测. HTTPS 协议虽然会加密全部通讯,但是在握手之前还是明文传输.有证书特证可被检测. 1, D ...

  4. Windows下EDK2环境的搭建以及经典的程序设计Print Hello World !-----(Linux下的待后续熟练了再更新)

    很久没有更新博客了,之前的博客末尾有提到过要写有关EDK2环境搭建的博客,现在就是完成的时候了,后续博客更新会比较规律(大概每周一篇?) 本人博客仅仅发表于博客园,本人主页为         http ...

  5. ajax5

    处理跨域方法 (代理) 一个域名地址的组成: /script/jQuery.js 协议    子域名  主域名   端口号  请求资源地址 当协议,子域名,主域名,端口号中任意一个不相同时,都算作不同 ...

  6. JavaScript表单序列化的方法详解

    本文介绍下,在javascript中实现表单序列化的方法,通过实例加深理解,有需要的朋友参考下吧. 在JavaScript中,可以利用表单字段的type属性,连同name和value属性一起实现对表单 ...

  7. 深入理解Java内存模型(摘)

    --摘自 周志明<深入理解Java虚拟机> 转自 https://www.jianshu.com/p/15106e9c4bf3 深入理解Java内存模型(摘) java内存模型(Java ...

  8. Tomcat 启动过滤器异常

    严重 [RMI TCP Connection(2)-127.0.0.1] org.apache.catalina.core.StandardContext.filterStart 启动过滤器异常 ja ...

  9. python中可变长度参数详解

    1. *args用法:python会将所有位置的参数收集到一个元组中 2. **args用法:python会将关键字参数传递给一个新的字典.**允许将关键字参数转换为字典 用法见如下代码: def f ...

  10. Spring框架——IOC 工厂方法

    IoC 是典型的⼯厂模式,如何使⽤用⼯厂模式创建 bean, IoC 通过⼯厂模式创建 bean 有以下两种⽅式 xml <?xml version="1.0" encodi ...