题目大意
给一些人的信息,和一些查询。对每个查询要求在某个年龄范围内最富有的 个人
输入
每组包含一个测试用例
第一行是两个正整数
表示总人数,
表示查询的次数
之后有
行,每行给出一个人的信息姓名 年龄 财富
,姓名
是长度不超过8且不含空格的字符串,年龄
是范围在
的整数,财富
是范围在
的整数
最后有
行,每行表示一个查询m aMin aMax
,m
表示最富有的人数,aMin
表示年龄的最小值,aMax
表示年龄的最大值
输出
对每个查询
先在一行中输出Case #X:
,X
是表示这是第几次查询
然后就是年龄在
个最富有人的信息,每个人单独一行。如果没有人就输出None
输出时,按照财富降序输出,如果财富相同就按照年龄升序,如果年龄也相同就按照姓名的字典序升序输出
样例输入
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
样例输出
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个(因为
的最大值为100)。
之后再查找年龄在范围内的人,然后按序输出即可
这题python会超时,C++AC
超时的python
# -*- coding: utf-8 -*-
# @Time : 2019/6/11 10:32
# @Author : ValarMorghulis
# @File : 1055.py
import functools
class node:
def __init__(self, name, age, worth):
self.name = name
self.age = age
self.worth = worth
def toString(self):
return "%s %d %d" % (self.name, self.age, self.worth)
def cmp(a, b):
if a.worth != b.worth:
return -1 if a.worth > b.worth else 1
elif a.age != b.age:
return -1 if a.age < b.age else 1
else:
return -1 if a.name < b.name else 1
def solve():
n, k = map(int, input().split())
tot = list()
for i in range(n):
name, age, worth = input().split()
tot.append(node(name, int(age), int(worth)))
tot.sort(key=functools.cmp_to_key(cmp))
person = list()
ageOfAll = [0 for i in range(211)]
for i in range(n):
if ageOfAll[tot[i].age] < 100:
person.append(tot[i])
ageOfAll[tot[i].age] += 1
for i in range(k):
m, aMin, aMax = map(int, input().split())
ans = list()
for j in person:
if aMin <= j.age <= aMax:
ans.append(j)
ans=ans[0:m]
print("Case #%d:" % (i + 1))
if ans:
for j in ans:
print(j.toString())
else:
print("None")
if __name__ == "__main__":
solve()
AC的C++
#include<stdio.h>
#include<stdlib.h>
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<stack>
#include<cstring>
#include<string>
#include<cmath>
#define inf 0xffffffff
using namespace std;
typedef struct
{
char name[10];
int age;
int worth;
}node;
bool cmp(node a, node b)
{
if(a.worth!=b.worth)
return a.worth>b.worth;
if(a.age!=b.age)
return a.age<b.age;
return strcmp(a.name, b.name)<0;
}
int main()
{
int n, k;
scanf("%d%d", &n, &k);
vector<node> tot(n), person;
for(int i=0; i<n; i++)
scanf("%s%d%d", tot[i].name, &tot[i].age, &tot[i].worth);
sort(tot.begin(), tot.end(), cmp);
vector<int> ageOfAll(211, 0);
for(int i=0; i<n; i++)
if(ageOfAll[tot[i].age]<100)
{
person.push_back(tot[i]);
ageOfAll[tot[i].age]++;
}
for(int i=0; i<k; i++)
{
int m, aMin, aMax;
scanf("%d%d%d", &m, &aMin, &aMax);
vector<node> ans;
for(int j=0; j<person.size(); j++)
if(person[j].age>=aMin&&person[j].age<=aMax)
ans.push_back(person[j]);
printf("Case #%d:\n", i+1);
if(ans.size()>0)
for(int j=0; j<m&&j<ans.size(); j++)
printf("%s %d %d\n", ans[j].name, ans[j].age, ans[j].worth);
else
printf("None\n");
}
return 0;
}