#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. 微信开发——测试号申请,接口配置,JS接口安全域名,自定义菜单

    1.申请测试账号: 先申请公众号后,点击进入公从号的管理页面:找到“开发者工具”,找到“公众平台测试账号”,点击“进入”. 2.接口配置信息设置 必须要外网哦,也就是微信服务器要能访问到你填写到url ...

  2. P2045 方格取数加强版 最大费用最大流

    $ \color{#0066ff}{ 题目描述 }$ 给出一个n*n的矩阵,每一格有一个非负整数Aij,(Aij <= 1000)现在从(1,1)出发,可以往右或者往下走,最后到达(n,n),每 ...

  3. 最小生成树问题:Kruskal算法 AND Prim算法

    Kruskal算法: void Kruskal ( ) {     MST = { } ;                           //边的集合,最初为空集     while( Edge ...

  4. SpeechVoiceSpeakFlags枚举类型的详细解释

    http://blog.csdn.net/zhou_xw6511/article/details/8313528

  5. PIE SDK ISODATA分类

    1.算法功能简介 ISODATA(IterativeSelf-OrganizingDataAnalysisTechniqueAlgorithm)即迭代式自组织数据分析技术, 其大致原理是首先计算数据空 ...

  6. Docker:网络模式详解

    Docker作为目前最火的轻量级容器技术,牛逼的功能,如Docker的镜像管理,不足的地方网络方面. Docker自身的4种网络工作方式,和一些自定义网络模式 安装Docker时,它会自动创建三个网络 ...

  7. 单一指责原则(Single Responsibility Principle) SRP

    using System; using System.Collections.Generic; using System.Text; namespace SingleResponsibilityPri ...

  8. Hadoop Ecosytem

    There are a lot of Hadoop related projects which are open sourced and widely used by many componies. ...

  9. nutz框架使用记录之Cnd.wrap

    这是对Cnd.wrap 官方用法 , 直接硬编码 , [JAVA]List<Person> crowd = dao.query(Person.class, Cnd.wrap("n ...

  10. unity3d发布到安卓平台

    1.首先你得装上JDK并且配置好环境(就像学java配置环境一样) 百度jdk把下载安装成功 找到安装jdk目录的bin目录,复制路径,例如 C:\Program Files (x86)\Java\j ...