zoukankan      html  css  js  c++  java
  • 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] = {''};
        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:
    ", i + 1);
            int m = 0;
            while (!age_leader.empty() && m < M) {
                m++;
                People* leader = age_leader.top();
                age_leader.pop();
                
                printf("%s %d %d
    ", 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
    ");
            }
        }
        
        return 0;
    }

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

  • 相关阅读:
    SEO高手和SEO屌丝的八个区
    【织梦免费模板】防火涂料网站模版
    利用火车头采集A67手机电影教程一
    SEO高级技巧
    .NET代码设计简单规范
    JAVA 和.NET在安全功能的比较
    poi实现excel数据的导入和导出
    eclipse复制bpmn文件到idea下乱码问题处理
    分账接收方与原请求方不一致,微信分账总结
    Java List<T> 去重
  • 原文地址:https://www.cnblogs.com/lailailai/p/4021371.html
Copyright © 2011-2022 走看看