PAT 1055 The World's Richest

#include <cstdio>
#include <cstdlib>
#include <cstring>

#include <vector>
#include <queue>
#include <algorithm>

using namespace std;

#define AGE_MAX 200

class People {
public:
    char name[9];
    int worth;
    int age;
    int idx;
    People(const char* _name, int _worth = 0, int _age = 0) {
        strcpy(name, _name);
        worth     = _worth;
        age     = _age;
        idx     = 0;
    }
};

bool people_compare(const People* a, const People* b) {
    if (a->worth > b->worth) {
        return true;
    } else if (a->worth < b->worth) {
        return false;
    }
    if (a->age < b->age) {
        return true;
    } else if (a->age > b->age) {
        return false;
    }

    return strcmp(a->name, b->name) < 0;
}

class mycmp {
public:
    bool operator() (const People* a, const People* b) {
        return !people_compare(a, b);
    }
};

int main() {
    int N = 0, K = 0;
    scanf("%d%d", &N, &K);

    vector<vector<People*> > peoples(AGE_MAX + 1);

    char name[10] = {‘\0‘};
    int worth = 0, age = 0;

    for (int i=0; i<N; i++) {
        scanf("%s%d%d", name, &age, &worth);
        peoples[age].push_back(new People(name, worth, age));
    }

    for (int i=0; i<=AGE_MAX; i++) {
        vector<People*>& list = peoples[i];
        if (!list.size()) continue;
        // sort people in each age list
        sort(list.begin(), list.end(), people_compare);

        for (int j=0; j<list.size(); j++) {
            list[j]->idx = j;
        }
    }

    for (int i=0; i<K; i++) {
        int M = 0, Amin = 0, Amax = 0;
        scanf("%d%d%d", &M, &Amin, &Amax);

        priority_queue<People*, vector<People*>, mycmp> age_leader;

        for(int j = Amin; j <= Amax; j++) {
            if (peoples[j].empty()) continue;
            age_leader.push(peoples[j].front());
        }
        printf("Case #%d:\n", i + 1);
        int m = 0;
        while (!age_leader.empty() && m < M) {
            m++;
            People* leader = age_leader.top();
            age_leader.pop();

            printf("%s %d %d\n", leader->name, leader->age, leader->worth);
            if (leader->idx + 1 >= peoples[leader->age].size()) continue;
            age_leader.push(peoples[leader->age][leader->idx + 1]);
        }
        if (m == 0) {
            printf("None\n");
        }
    }

    return 0;
}

室友说直接排序会超时,于是尝试着改进一下,实质上就是对有序多链表的Merge操作,这里有序链表就是以年龄划分的人群以worth等字段的排序结果,由于题目中指定最多显示的数目,这样可以不用把整个Merge做完,结果数量达到即可。一次过!

PAT 1055 The World's Richest

时间: 2024-10-27 07:49:16

PAT 1055 The World's Richest的相关文章

PAT:1055. The World&#39;s Richest (25) AC

#include<stdio.h> #include<string.h> #include<algorithm> using namespace std; struct Person { char name[10]; int age,money; }P[100010]; bool cmp(Person a,Person b) { if(a.money!=b.money) return a.money>b.money; else if(a.age!=b.age) r

PAT (Advanced Level) 1055. The World&#39;s Richest (25)

排序.随便加点优化就能过. #include<iostream> #include<cstring> #include<cmath> #include<algorithm> #include<cstdio> #include<map> #include<queue> #include<string> #include<stack> #include<vector> using names

PAT Advanced 1055 The World&#39;s Richest (25分)

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 n

1055. The World&#39;s Richest (25)

1 #include <stdio.h> 2 #include <vector> 3 #include<string.h> 4 #include<algorithm> 5 using namespace std; 6 7 struct MyStruct 8 { 9 char name[9]; 10 int age,worth; 11 }; 12 13 int cmp(MyStruct a,MyStruct b) 14 { 15 if(a.worth!=b.w

1055. The World&#39;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 n

1055 The World&#39;s Richest (25 分)

1055 The World's Richest (25 分) 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 ran

1055 The World&#39;s Richest (25分)

1. 题目 2. 思路 常规题 3. 注意点 注意超时 4. tip 善于使用题目的条件来减少数据量 fill(begin, end, value)注意[begin, end) 2. 代码 #include<cstdio> #include<algorithm> #include<string> #include<vector> #include<iostream> // 14:57 - using namespace std; struct p

PAT——1055. 集体照

拍集体照时队形很重要,这里对给定的N个人K排的队形设计排队规则如下: 每排人数为N/K(向下取整),多出来的人全部站在最后一排: 后排所有人的个子都不比前排任何人矮: 每排中最高者站中间(中间位置为m/2+1,其中m为该排人数,除法向下取整): 每排其他人以中间人为轴,按身高非增序,先右后左交替入队站在中间人的两侧(例如5人身高为190.188.186.175.170,则队形为175.188.190.186.170.这里假设你面对拍照者,所以你的左边是中间人的右边): 若多人身高相同,则按名字的

PAT 1055. 集体照 (25)

拍集体照时队形很重要,这里对给定的N个人K排的队形设计排队规则如下: 每排人数为N/K(向下取整),多出来的人全部站在最后一排: 后排所有人的个子都不比前排任何人矮: 每排中最高者站中间(中间位置为m/2+1,其中m为该排人数,除法向下取整): 每排其他人以中间人为轴,按身高非增序,先右后左交替入队站在中间人的两侧(例如5人身高为190.188.186.175.170,则队形为175.188.190.186.170.这里假设你面对拍照者,所以你的左边是中间人的右边): 若多人身高相同,则按名字的