题目
思路
简单排序题,但是会出现超时问题,重点在题目中说最大输出为100,即我们排序后将相同年龄在优先级在100以后的数据删除掉,用新数组查询
代码
#include <iostream>
#include<algorithm>
#include<vector>
using namespace std;
#define nmax 105
#define inf 999999
typedef struct {
string name;
int age;
int worth;
}info;
vector<info> tmp;
vector<info> vec;
int mark[nmax];
bool cmp(const info& a, const info& 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 < b.name;
}
int main()
{
int pn, qn;
cin >> pn >> qn;
memset(mark, 0, sizeof(mark));
for (int i = 0; i < pn; ++i) {
info n;
cin >> n.name >> n.age >> n.worth;
tmp.emplace_back(n);
}
sort(tmp.begin(), tmp.end(), cmp);
for (auto& p : tmp) {
mark[p.age]++;
if (mark[p.age] <= 100) {
vec.emplace_back(p);
}
}
for (int i = 1; i <= qn; ++i) {
int maxout, low, high;
bool have = false;
cin >> maxout >> low >> high;
cout << "Case #" << i << ":" << endl;
for (auto p : vec) {
if (p.age >= low && p.age <= high) {
have = true;
cout << p.name << " " << p.age << " " << p.worth << endl;
--maxout;
}
if (maxout == 0)
break;
}
if (!have)
cout << "None" << endl;
}
return 0;
}