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;
}
}

最新文章

  1. Web 入门之 XML
  2. mvc之文件下载
  3. 读书笔记-JVM
  4. BZOJ 2038 小z的袜子 &amp; 莫队算法(不就是个暴力么..)
  5. hdu 3999 The order of a Tree (二叉搜索树)
  6. 在 远程桌面 权限不足无法控制 UAC 提示时,可使用 计划任务 绕开系统的 UAC 提示
  7. Windows Security 学习笔记
  8. 20160805_笔记本_CentOS6.4x64分区
  9. TKinter布局之pack
  10. 一.OSI与TCP
  11. jstl if条件判断字符串代码
  12. CMake with Win&amp;MinGW
  13. javascript grunt安装和使用
  14. MVC ViewData和ViewBag
  15. excel设置单元格不可编辑
  16. bcp sqlcmd bulkinsert在unicode问题,Unexpected EOF encountered in BCP data-file
  17. Ubuntu(Linux) + mono + jexus +asp.net MVC3
  18. 201521123039 《java程序设计》第九周学习总结
  19. 【面试】足够应付面试的Spring事务源码阅读梳理(建议珍藏)
  20. Java架构师趣谈Hbase之宏观架构

热门文章

  1. elasticjob学习二:封装elasticjob-spring-boot-starter
  2. 使用jquery实现动态时钟
  3. SpringBoot——Cache使用原理及Redis整合
  4. vscode使用cnpm报错
  5. npm 安装 electron 出现的奇葩错误
  6. MVC设计模式简介
  7. 《Python学习手册 第五版》 -第18章 参数
  8. 使用Eclipse开发python
  9. 《闲扯Redis一》五种数据类型之String型
  10. POJ1270 toposort+DFS+回溯