PAT1 1055 The World's Richest

Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/LSC_333/article/details/91418868

Topic Link to
my github

Subject to the effect

Give some information, and some queries. Requirements for each query within a certain age range richest M M Personal

Entry

Each test case comprising a
first line of two positive integers N 1 0 5 N\leq10^5 represents the total number of people, K 1 0 3 K\leq10^3 represents the number of queries
have after N N lines of a given person's information姓名 年龄 财富,姓名is 8 and the string length does not exceed without space,年龄is in the range of ( 0 , 200 ] (0, 200] integer,财富is in the range of [ 1 0 6 , 1 0 6 ] [-10^6, 10^6] integer
finally there K K lines, each line represents a querym aMin aMax,m 100 \leq100 represents the richest number,aMinrepresents the minimum age,aMaxrepresents the maximum age

Export

Each query to
the first output in a row Case #X:, Xis how many times the query
then is aged [ a M i n , a M a x ] [aMin, aMax] M M a most information was that everyone on a separate line. If there is no output onNone
the output, the output in descending order according to wealth, if wealth on the same ascending order by age, if age is the same as on the ascending order according to the name of the dictionary output

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

Resolve

First everyone sorted and then for each age only the leading 100 (since M M maximum value is 100).
After re-locate people within the age range, and then outputted in order to

This problem python timeout, C ++ AC

Timeout 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()

C ++ 的 AM

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

Guess you like

Origin blog.csdn.net/LSC_333/article/details/91418868