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_WorthThe 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 50Sample 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
分析:這道題目意思就是說給出你一些有錢人的信息,然後要你在它給的年齡範圍內求出前M名,並將他們輸出。題目不難,採用c++STL的話很容易就能做出來,但是我不得不吐槽這道題目,坑,天坑,坑在哪裏呢?坑在時間,如果你就用我開始說的思路做下去,絕對有測試點會超時,所以,一直在想該如何去優化這個代碼?
仔細閱讀題目,你會發現這樣一個信息:M<=100,這個信息表明的意思就是,當某個年齡出現的次數大於一百的時候可以過濾掉。
可是我後來又試出了一種,那就是在你輸出處理時,那個循環有必要循環N次嗎?對,是沒必要的,你只需要輸出M個就好了,何必去循環那麼多次。測試也是通過了,不過比前一種方式時間長一點。我下面的代碼是第二種,好理解。
源碼:
#include<iostream>
#include <cstdio>
#include<algorithm>
#include<vector>
#include<string>
using namespace std;
//億萬富翁結點
struct billionaires{
string name;
int age;
int net_worth;
billionaires(string n, int a, int nw):name(n), age(a), net_worth(nw){}
void output(){
cout << name;
printf(" %d %d\n", age, net_worth);
}
};
//比較函數
bool compare(const billionaires &m1, const billionaires &m2){
if (m1.net_worth != m2.net_worth){
return m1.net_worth > m2.net_worth;
}
else{
if (m1.age != m2.age){
return m1.age < m2.age;
}
else{
return m1.name < m2.name;
}
}
}
int main()
{
int N, K;
scanf("%d %d", &N, &K); // 第一行輸入
vector<billionaires> vec;
for (int i = 0; i < N; i++){ //輸入信息存儲
string name;
cin >> name;
int age, net_worth;
scanf("%d %d",&age, &net_worth);
vec.push_back(billionaires(name, age, net_worth));
}
//排序
sort(vec.begin(), vec.end(), compare);
for (int i = 0; i < K; i++){ //k個case
int M, Amin, Amax;
scanf("%d %d %d", &M, &Amin, &Amax); //輸入case信息
printf("Case #%d:\n", i + 1);
bool flag = false; //判斷是否有位於此區間的數據
for (int k = 0,count = 0;k<N && count<M; k++){
if (vec[k].age >= Amin && vec[k].age<=Amax){
vec[k].output();
count++;
flag = true; //說明有信息
}
}
if (!flag){ //沒有在此範圍內的數據
printf("None\n");
continue;
}
}
return 0;
}