题目描述
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.
翻译:财富杂志每年出版关于年度世界最富有的亿万富翁排名榜。现在你需要模仿这个工作,但是仅仅集中于一个时间段的人。给你N个人物的价值,你需要输出年龄在给定范围内的M个最有钱的人。
INPUT FORMAT
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.
翻译:每个输入文件包含一组测试数据。对于每组输入数据,第一行包括2个正整数:N(<=105) -总人数,和K (<=103) - 查询总数。接着N行,每行包括一个人的姓名(不超过8位没有空格的字符串),age((0, 200]的正整数),和净资产( [-106, 106]的正整数)。最后又K行查询,每一行包括3个正整数:M(<= 100) - 输出的最大人数,和[Amin, Amax] ,代表年龄的范围。一行内所有数字之间用空格隔开。
OUTPUT FORMAT
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”.
翻译:对于每组输入数据,第一行输出”Case #X:” ,X代表从1开始的第几次查询。接着输出M个在[Amin, Amax]范围内最有钱的人。每个人的信息包括一行,按照以下格式输出:
Name Age Net_Worth
输出必须按照净资产降序输出。如果财富相同,那么必须按照年龄升序输出。如果财富和年龄都相同,那么按照姓名的字母序升序输出。数据保证没有两个人3个信息都完全相同。如果没有人符合,则输出”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
解题思路
这道题说实话,我做的时候很头铁,不过有一种一力破十会的感觉。
1.首先,题目需要我们输出结果按照金钱->年龄->姓名的顺序排序,我就写了个结构体和排序,用优先队列保存,准备秒过。
2.然后发现是多组查询,那么就按照年龄存储到优先队列中。
3.然后发现优先队列pop了就没了啊,就定义一个新的优先队列保存。
4.剩下的就比较简单了,做完后提交,居然超时了,仔细观察后发现是因为每次扔num*ageLength 个进队列复杂度太高,就想判断如果比最小的小,就break。但是优先队列不能反向抛出啊,那就定义一个反向的优先队列吧。
5.数据是存完了,怎么输出呢?它是反的啊,那就再定义一个正向优先队列保存,因为num<=100,所以基本可以忽略不计。就这样写完了。
6.我看了算法笔记后发现确实每个年龄存100个人排序后判断年龄的方法是更好的,但是由于掌握了不少算法知识,现在居然可以头铁一路跟着自己想法写下来,这应该算是进步了吧。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<string>
#include<vector>
#include<queue>
#include<algorithm>
#define INF 99999999
using namespace std;
struct Peo{
char name[10];
int age;
int money;
Peo(char n[],int a,int m):age(a),money(m){
strcpy(name,n);
}
bool operator<(const Peo &a)const{
return money==a.money?(age==a.age?strcmp(name,a.name)>0:age>a.age):money<a.money;
}
bool operator>(const Peo &a)const{
return money==a.money?(age==a.age?strcmp(name,a.name)<0:age<a.age):money>a.money;
}
};
int N,K;
priority_queue<Peo> p[201];
priority_queue<Peo,vector<Peo> ,greater<Peo> > q;
priority_queue<Peo> ans;
priority_queue<Peo> temp;
int main(){
scanf("%d%d",&N,&K);
char n[10];
int a,m;
for(int i=0;i<N;i++){
scanf("%s%d%d",n,&a,&m);
p[a].push(Peo(n,a,m));
}
int num,pre,pro;
for(int i=0;i<K;i++){
scanf("%d%d%d",&num,&pre,&pro);
for(int j=pre;j<=pro;j++){
temp=p[j];
int ccount=0;
while(!temp.empty()&&ccount<num){
Peo t=temp.top();temp.pop();
if(q.size()==num){
if(q.top()<t){
q.pop();
q.push(t);
}
else break;
}
else q.push(t);
ccount++;
}
}
printf("Case #%d:\n",i+1);
int flag=q.size();
while(!q.empty()){
Peo t=q.top();q.pop();
ans.push(t);
}
while(!ans.empty()){
Peo t=ans.top();ans.pop();
printf("%s %d %d\n",t.name,t.age,t.money);
}
if(flag==0)printf("None\n");
}
return 0;
}