#include <cstdio>
#include <cstdlib>
#include <cstring> #include <vector>
#include <queue>
#include <algorithm> using namespace std; #define AGE_MAX 200 class People {
public:
char name[];
int worth;
int age;
int idx;
People(const char* _name, int _worth = , int _age = ) {
strcpy(name, _name);
worth = _worth;
age = _age;
idx = ;
}
}; 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) < ;
} class mycmp {
public:
bool operator() (const People* a, const People* b) {
return !people_compare(a, b);
}
}; int main() {
int N = , K = ;
scanf("%d%d", &N, &K); vector<vector<People*> > peoples(AGE_MAX + ); char name[] = {'\0'};
int worth = , age = ; for (int i=; i<N; i++) {
scanf("%s%d%d", name, &age, &worth);
peoples[age].push_back(new People(name, worth, age));
} for (int i=; 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=; j<list.size(); j++) {
list[j]->idx = j;
}
} for (int i=; i<K; i++) {
int M = , Amin = , Amax = ;
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 + );
int m = ;
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 + >= peoples[leader->age].size()) continue;
age_leader.push(peoples[leader->age][leader->idx + ]);
}
if (m == ) {
printf("None\n");
}
} return ;
}

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

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

  1. PAT 1055 The World's Richest[排序][如何不超时]

    1055 The World's Richest(25 分) Forbes magazine publishes every year its list of billionaires based o ...

  2. PAT 甲级 1055 The World's Richest (25 分)(简单题,要用printf和scanf,否则超时,string 的输入输出要注意)

    1055 The World's Richest (25 分)   Forbes magazine publishes every year its list of billionaires base ...

  3. PAT(Advanced Level)1055.The World's Richest

    Forbes magazine publishes every year its list of billionaires based on the annual ranking of the wor ...

  4. PAT (Advanced Level) Practice 1055 The World's Richest (25 分) (结构体排序)

    Forbes magazine publishes every year its list of billionaires based on the annual ranking of the wor ...

  5. PAT (Advanced Level) 1055. The World's Richest (25)

    排序.随便加点优化就能过. #include<iostream> #include<cstring> #include<cmath> #include<alg ...

  6. PAT甲级1055 The World's Richest【排序】

    题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805421066272768 题意: 给定n个人的名字,年龄和身价. ...

  7. PAT甲题题解-1055. The World's Richest (25)-终于遇见一个排序的不水题

    题目简单,但解题的思路需要转换一下,按常规思路肯定超时,推荐~ 题意:给出n个人的姓名.年龄和拥有的钱,然后进行k次查询,输出年龄在[amin,amx]内的前m个最富有的人的信息.如果财富值相同就就先 ...

  8. 【PAT甲级】1055 The World's Richest (25 分)

    题意: 输入两个正整数N和K(N<=1e5,K<=1000),接着输入N行,每行包括一位老板的名字,年龄和财富.K次询问,每次输入三个正整数M,L,R(M<=100,L,R<= ...

  9. pat 1055 区间前k个

    http://pat.zju.edu.cn/contests/pat-a-practise/1055 第二组数据比较大,如果单纯排序直接检索会超时,因为每次都是对所有数据进行遍历. N/200=500 ...

随机推荐

  1. jeecms学习笔记

    jeecms学习笔记 内容管理系统 1.栏目列表标签 作用:获取栏目的列表 [@cms_channel_list] [#list tag_list as b] <a href="${b ...

  2. LeetCode记录之14——Longest Common Prefix

    本题虽然是easy难度,题目也一目了然,问题就是在这里,需要考虑的特殊情况太多,太多限制.导致我一点点排坑,浪费了较多时间. Write a function to find the longest ...

  3. Visual Studio Ultimate 2013 免费下载地址

    ed2k://|file|cn_visual_studio_2010_ultimate_x86_dvd_532347.iso|2685982720|4AE6228933DDE49D9BFA4C3467 ...

  4. Codeforces Round #549 (Div. 2) Solution

    传送门 A.The Doors 看懂题目就会写的题 给一个 $01$ 序列,找到最早的位置使得 $0$ 或 $1$ 已经全部出现 #include<iostream> #include&l ...

  5. P2801 教主的魔法

    传送门 $N$ 太大了主席树过不了 考虑分块 对每个块内的元素排序,询问就对大块二分查找,对两边小的部分暴力枚举 修改时维护 $add[i]$ 标记,维护当前块内整块已经加的数 那么整块的就直接修改 ...

  6. Echarts图表横坐标显示不全

    xAxis: { "axisLabel":{ //加上这个强制显示 interval: 0 }, type: 'category', data: self[theDataKey]. ...

  7. 腾讯云(Linux)安装Redis。

    参考:https://blog.csdn.net/a575553272/article/details/79743802 指令:ps -ef | grep redis   查看启动后的进程.

  8. postgreSQL 常用命令 二

    本次测试基与PostgreSQL 10.x版本 创建用户 [postgres@rtm2 data]$ /opt/pgsql-10/bin/createuser rentaomin [postgres@ ...

  9. WEB图表制作

    https://www.hcharts.cn/demo/highcharts/column-drilldown

  10. Mina初识

    1.概述 1.1 Apache的顶级项目,基于java NIO,支持TCP/IP.UDP/IP: 1.2 Mina对外屏蔽了java NIO使用的复杂性,并在性能上做了不少的优化: 1.3 Mina采 ...