1055. The World's Richest (25)

时间限制
400 ms
内存限制
128000 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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 (<=105) - the total number of people, and K (<=103) - 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 [-106, 106]) of a person. Finally there are K lines of queries, each contains three positive integers: M (<= 100) - 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 思路
排序题,需要注意的是对输入的数据要预处理下,比如在某个年龄的人数达到100后就不用再将剩余的该年龄点的人插入数据集了(M最大为100),因为不管怎么查,输出只会是某个年龄段的前100富翁。
代码
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std; class person
{
public:
string name;
int age;
int worth;
}; bool cmp(const person& a,const person& b)
{
if(a.worth != b.worth)
return a.worth > b.worth;
else if(a.age != b.age)
return a.age < b.age;
else
return a.name.compare(b.name) < 0;
} int main()
{
int n,k;
vector<int> age_count(201,0);
vector<person> res;
while(cin >> n >> k)
{
vector<person> people(n);
for(int i = 0;i < n;i++)
{
cin >> people[i].name >> people[i].age >> people[i].worth;
}
sort(people.begin(),people.end(),cmp);
for(int i = 0;i < people.size();i++) //减小查找量
{
if(age_count[people[i].age] < 100)
{
res.push_back(people[i]);
age_count[people[i].age]++;
}
}
for(int i = 1;i <= k;i++)
{
vector<person> output;
int cnt = 0,maxcnt,age_min,age_max;
cin >> maxcnt >> age_min >> age_max;
for(int j = 0;j < res.size() && cnt < maxcnt;j++)
{
if(res[j].age >= age_min && res[j].age <= age_max)
{
output.push_back(res[j]);
cnt++;
}
}
if(output.empty())
cout << "Case #" << i << ":\n" << "None" << endl;
else
{
cout << "Case #" << i << ":" << endl;
for(int j = 0;j < output.size();j++)
{
cout << output[j].name << " " << output[j].age << " " << output[j].worth << endl;
}
}
}
}
}

  

PAT1055:The World's Richest的更多相关文章

  1. pat1055. The World's Richest (25)

    1055. The World's Richest (25) 时间限制 400 ms 内存限制 128000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue ...

  2. 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 A1055 The World's Richest (25 分)——排序

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

  4. A1055. The World's Richest

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

  5. PAT 1055 The World's Richest[排序][如何不超时]

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

  6. 7-31 The World's Richest(25 分)

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

  7. A1055 The World's Richest(25 分)

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

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

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

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

随机推荐

  1. OpenCV特征点检测------Surf(特征点篇)

    Surf(Speed Up Robust Feature) Surf算法的原理                                                              ...

  2. 说说nio----1

    既然说到了nio,就得谈以下几个问题 为什么会出现新io,"旧io"有什么问题吗? ok,一步一步来,先给大家看几个例子: 1单线程的服务器程序 import java.net.* ...

  3. JavaScript进阶(十一)JsJava2.0版本

    JavaScript进阶(十一)JsJava2.0版本 2007年9月11日,JsJava团队发布了JsJava2.0版本,该版本不仅增加了许多新的类库,而且参照J2SE1.4,大量使用了类的继承和实 ...

  4. 网络小白之WAN与LAN的区别

    剑指Offer--网络小白之WAN与LAN的区别 基本作用 wan接口是外网接口,是用来连接互联网或局域网等外部网络的. lan接口是内网接口,是用来连接计算机终端或其他路由器等终端设备的. 举例 w ...

  5. saiku查询结果返回地址(saiku交互过程)

    提交mdx语句,执行后, 查询结果的获得,访问这个地址: http://99.999.99.99:8080/saiku/rest/saiku/anonymousUser/query/4FF7D01E- ...

  6. iOS中关于UIApplication的详细介绍

    UIApplication 什么是UIApplication? UIApplication对象是应用程序的象征.每一个应用都有自己的UIApplication对象,这个对象是系统自动帮我们创建的, 它 ...

  7. nodejs安装及故障解决

    本文来自:http://hxl2009.blog.51cto.com/779549/1031320 给开发搞nodejs环境,遇到点问题记录下过程,备忘. wget http://nodejs.org ...

  8. android studio JNI使用

    Step: 1. 添加native接口注意写好native接口和System.loadLibrary()即可了,并无特别之处. Step: 2.执行Build->Make Project 生成了 ...

  9. Aho-Corasick算法学习

    1.概述 Aho-Corasick自动机算法(简称AC自动机)1975年产生于贝尔实验室.该算法应用有限自动机巧妙地将字符比较转化为了状态转移.此算法有两个特点,一个是扫描文本时完全不需要回溯,另一个 ...

  10. 你不能错过.net 并发解决方案

    BlockingCollection集合是一个拥有阻塞功能的集合,它就是完成了经典生产者消费者的算法功能.所以BlockingCollection 很适合构造流水线模式的并发方案 BlockingCo ...