#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. iis 重启命令

    打开IIS配置窗口的CMD命令:开始---运行---CMD----输入inetmgr  直接使用CMD我们可以操作很多事情,比如启动IIS,重启IIS,停止IIS 重启IIS服务器,开始->运行 ...

  2. 封装的一个sorted_vector示例,实现了stl::set的一部分接口

           STL set能保证最坏情况下的查找和插入效率,logN.但是维护红黑树开销较大.set内的元素按照一定的逻辑顺序组织,查找.插入等操作的结果都和排序规则有关.       适合STL ...

  3. P3994 高速公路 树形DP+斜率优化+二分

    $ \color{#0066ff}{ 题目描述 }$ C国拥有一张四通八达的高速公路网树,其中有n个城市,城市之间由一共n-1条高速公路连接.除了首都1号城市,每个城市都有一家本地的客运公司,可以发车 ...

  4. 【离散数学】SDUT OJ 指定长度路径数

    指定长度路径数 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Problem Description 题目给出一个有n个节点 ...

  5. leetcode-229.求众数(二)

     题目: 给定一个大小为 n 的数组,找出其中所有出现超过 ⌊ n/3 ⌋ 次的元素. 说明: 要求算法的时间复杂度为O(n),空间复杂度为O(1). 示例 1: 输入: [3,2,3] 输出: [3 ...

  6. Qt 学习之路 2(61):使用 SAX 处理 XML

    Qt 学习之路 2(61):使用 SAX 处理 XML  豆子  2013年8月13日  Qt 学习之路 2  没有评论 前面两章我们介绍了使用流和 DOM 的方式处理 XML 的相关内容,本章将介绍 ...

  7. python学习之路---day02

    一:while循环 while 条件语句 #如果条件成立则执行下面的循环语句 循环语句 eg1:1+2+3+4+5......+100=? num=0 #给num和sum赋初值 sum=0 while ...

  8. HDU - 4686 函数积的前缀和

    题意:求\(\sum_{i=0}^{n-1}a_ib_i\) 其中,\(a_i=A_xa_{i-1}+A_y,b_i=B_xb_{i-1}+B_y\) 构造矩阵分别维护\(a_ib_i,a_i,b_i ...

  9. java的Spring学习3- mybatis

    1.java的maven依赖包 <properties> <project.build.sourceEncoding>UTF-8</project.build.sourc ...

  10. 【总结】sqlmap-tamper编写小结

    目的:修改sqlmap中的tamper脚本来绕过代码对特定参数的过滤和转义 环境:win10.phpstudy2016.sqli-labs-master平台 工具:sqlmap.burpsuite 地 ...