1. 程式人生 > >【PAT甲級】1055 The World's Richest

【PAT甲級】1055 The World's Richest

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 (≤10​5​​) - the total number of people, and K (≤10​3​​) - 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 [−10​6​​,10​6​​]) of a person. Finally there are K lines of queries, each contains three positive integers: M (≤100) - the maximum number of outputs, and [Amin

Amax] 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 [Amin

Amax]. 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

題意理解

輸入給出n個人的姓名、年齡和財富,然後進行k次查詢,每次查詢某個年齡段最富有的m個人,按照財富從大到小輸出,如果財富相同則按照年齡從小到大輸出,如果財富年齡都相同則按姓名字母序輸出,如果沒有符合要求的人輸出None,如果符合要求的人數小於查詢人數,則輸出所有符合要求的人。

個人思路

這題是排序題的應用,一開始以為很簡單,但是由於資料比較大,時間限制比較小,經過了三次大改動才AC。

第一次想法:先將所有的人按照年齡從小到大排序,然後進行查詢時遍歷按照年齡排序後的陣列,找到年齡區間,將這個區間內的所有人再放入另外一個數組中按照題目的要求進行排序,然後輸出。這種思路大大超時,有兩個樣例都TLE了。

第二次的想法:建立205個動態陣列,下標代表年齡,在輸入n個人時就將每個人存入對應年齡的動態陣列中,然後查詢時直接將對應年齡的動態數組合並然後排序輸出。這種思路比第一種快,但是還有一個樣例TLE了。

程式碼如下:

#include <cstdio>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <vector>
#include <cmath>
#include <algorithm>
#include <iostream>
#define ll long long
#define eps 1e-8
#define INF 0x7FFFFFFF

using namespace std;

struct People{
    string name;
    int age, money;
};

bool cmp_query(People p1, People p2) {
    if (p1.money != p2.money) return p1.money > p2.money;
    else if (p1.age != p2.age) return p1.age < p2.age;
    else return p1.name < p2.name;
}

int main() {
    int n, k;
    vector<People> ages[205];
    cin >> n >> k;
    for (int i = 0; i < n; i ++) {
        People tmp;
        cin >> tmp.name >> tmp.age >> tmp.money;
        ages[tmp.age].push_back(tmp);
    }
    for (int i = 0; i < k; i ++) {
        int num, amin, amax, cnt = 0;
        cin >> num >> amin >> amax;
        vector<People> query_list;
        for (int a = amin; a <= amax; a ++) {
            for (vector<People>::iterator it = ages[a].begin(); it != ages[a].end(); it ++) {
                query_list.push_back(*it);
                cnt ++;
            }
        }
        cout << "Case #" << i+1 << ":" << endl;
        if (cnt == 0) {
            cout << "None" << endl;
        }
        else {
            sort(query_list.begin(), query_list.end(), cmp_query);
            num = min(num, cnt);
            for (int j = 0; j < num; j ++) {
                cout << query_list[j].name << " " << query_list[j].age << " " << query_list[j].money << endl;
            }
        }
    }
    return 0;
}

第三次的想法:之前兩個思路都TLE後,我有點沒頭緒了,想不出為什麼T,然後就再次求助了柳神的部落格https://blog.csdn.net/liuchuo/article/details/52225204,然後知道了由於查詢數量m的最大值100和總人數的最大值10^5相差太大,如果按照第二種思路,有可能每次都查詢很大的年齡區間就有很多人,然後在進行排序的過程雖然是快排,複雜度為O(nlogn),但是查詢次數還有1000次呢,所以還是有可能超時的。

所以我參考了柳神的思路後按照她的想法修改了自己的程式碼,在輸入n個人的資訊後將所有人一起按照題目要求排序並存在陣列中,然後設定一個結構體陣列query_all存放進行查詢的人的資訊,同時設立一個整型陣列age_cnt[205]存放某個年齡要加入查詢列表的人的數量,因為最多查詢100人,所以當某個年齡的人數量大於100後就不再將這個年齡的加入query_all了。然後進行k次查詢時直接遍歷query_all將滿足年齡條件的人加入結果陣列最後輸出即可。

【這裡還有一個坑】

當我按照柳神的思路實現程式碼後,發現還是會TLE,檢查程式碼後發現所有的輸入輸出我都是用cout和cin流實現的,突然想起刷乙級題的時候也遇上過因為cout和cin超時的題,然後改成了printf和scanf後就過了。

實現程式碼如下:

#include <cstdio>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <vector>
#include <cmath>
#include <algorithm>
#include <iostream>
#define ll long long
#define eps 1e-8
#define INF 0x7FFFFFFF

using namespace std;

struct People{
    char name[10];
    int age, money;
};

bool cmp(People p1, People p2) {
    if (p1.money != p2.money) return p1.money > p2.money;
    else if (p1.age != p2.age) return p1.age < p2.age;
    else return (strcmp(p1.name, p2.name) < 0);
}

int main() {
    // 輸入
    int n, k;
    vector<People> all_people;
    cin >> n >> k;
    for (int i = 0; i < n; i ++) {
        People tmp;
        scanf("%s %d %d",tmp.name, &tmp.age, &tmp.money);
        all_people.push_back(tmp);
    }
    // 將所有人進行排序
    sort(all_people.begin(), all_people.end(), cmp);
    // 選出每個年齡段的前100人
    vector<People> query_all;
    int age_num[205] = {0};
    for (int i = 0; i < n; i ++) {
        People tmp = all_people[i];
        if (age_num[tmp.age] < 100) {
            query_all.push_back(tmp);
            age_num[tmp.age] ++;
        }
    }
    // k次查詢
    for (int i = 0; i < k; i ++) {
        int num, amin, amax, cnt = 0;
        scanf("%d %d %d",&num, &amin, &amax);
        // 選出滿足年齡條件的人
        vector<People> query_list;
        for (int j = 0; j < query_all.size(); j ++) {
            if (query_all[j].age >= amin && query_all[j].age <= amax) {
                query_list.push_back(query_all[j]);
                cnt ++;
            }
        }
        // 輸出
        cout << "Case #" << i+1 << ":" << endl;
        if (cnt == 0) {
            cout << "None" << endl;
        }
        else {
            for (int j = 0; j < num && j < query_list.size(); j ++) {
                printf("%s %d %d\n", query_list[j].name, query_list[j].age, query_list[j].money);
            }
        }
    }
    return 0;
}

總結

學習不息,繼續加油

發現問題、解決問題的過程還是很有成就感的。