#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. Node.js的mysql执行多表联合查询

    数据库(test)中的表结构(admin.user) //执行多表结合查询 var mysql = require('mysql'); var connection = mysql.createCon ...

  2. 对结构化学习(structured learning)的理解

    接触深度学习以来一直接触的概念都是回归,分类,偶尔接触到结构化学习的概念,似懂非懂的糊弄过去,实在是不负责的表现 翻阅维基百科https://en.wikipedia.org/wiki/Structu ...

  3. C++_代码重用2-包含对象成员的类

    对于姓名可以使用字符数组来表示,但这将限制姓名的长度.当然,还可以使用char指针和动态内存分配,但这要求提供大量的支持代码.有一个好的方法就是使用一个他人开发好的类的对象来表示.如果C++库提供了合 ...

  4. Modular Inverse (拓展欧几里得求逆元)

    The modular modular multiplicative inverse of an integer a modulo m is an integer xsuch that a-1≡x ( ...

  5. .Net支持Redis哨兵模式

    csredis 博客 csRedisgit地址 csRedis3.2.1 Nuget地址 (在使用csredis3.2.1获取sentinel时产生运行时异常,调查问题最后发现是获取sentinel的 ...

  6. DictionaryHelper2

    /// <summary> /// DictionaryHelper /// </summary> public static class DictionaryHelper { ...

  7. springboot(六)-使用shiro

    前提 写之前纠结了一番,这一节放在shiro里面还是springboot里面.后来想了下,还是放springboot里吧,因为这里没有shiro的新东西,只有springboot添加了新东西的使用. ...

  8. 网络知识之ipset

    ipset介绍 ipset是iptables的扩展,它允许你创建 匹配整个地址集合的规则.而不像普通的iptables链只能单IP匹配, ip集合存储在带索引的数据结构中,这种结构即时集合比较大也可以 ...

  9. 【记录】adb连不上手机

    1.\用户\.android文件夹下新建adb_usb.ini,内容为手机的VID值,如0x9BB5 2.重启adb adb kill-server adb start-server adb devi ...

  10. The user specified as a definer ('root'@'%') does not exist解决方案

    今天操作以root身份操作MySQL数据库的时候报出了这个异常: Error updating database. Cause: java.sql.SQLException: The user spe ...