PAT1 1055 The World's Richest

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/LSC_333/article/details/91418868

题目链接
我的github

题目大意

给一些人的信息,和一些查询。对每个查询要求在某个年龄范围内最富有的 M M 个人

输入

每组包含一个测试用例
第一行是两个正整数 N 1 0 5 N\leq10^5 表示总人数, K 1 0 3 K\leq10^3 表示查询的次数
之后有 N N 行,每行给出一个人的信息姓名 年龄 财富姓名是长度不超过8且不含空格的字符串,年龄是范围在 ( 0 , 200 ] (0, 200] 的整数,财富是范围在 [ 1 0 6 , 1 0 6 ] [-10^6, 10^6] 的整数
最后有 K K 行,每行表示一个查询m aMin aMaxm 100 \leq100 表示最富有的人数,aMin表示年龄的最小值,aMax表示年龄的最大值

输出

对每个查询
先在一行中输出Case #X:X是表示这是第几次查询
然后就是年龄在 [ a M i n , a M a x ] [aMin, aMax] M M 个最富有人的信息,每个人单独一行。如果没有人就输出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个(因为 M M 的最大值为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;
}

猜你喜欢

转载自blog.csdn.net/LSC_333/article/details/91418868