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[];
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的更多相关文章
- PAT 1055 The World's Richest[排序][如何不超时]
1055 The World's Richest(25 分) Forbes magazine publishes every year its list of billionaires based o ...
- 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 ...
- 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 ...
- 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 ...
- PAT (Advanced Level) 1055. The World's Richest (25)
排序.随便加点优化就能过. #include<iostream> #include<cstring> #include<cmath> #include<alg ...
- PAT甲级1055 The World's Richest【排序】
题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805421066272768 题意: 给定n个人的名字,年龄和身价. ...
- PAT甲题题解-1055. The World's Richest (25)-终于遇见一个排序的不水题
题目简单,但解题的思路需要转换一下,按常规思路肯定超时,推荐~ 题意:给出n个人的姓名.年龄和拥有的钱,然后进行k次查询,输出年龄在[amin,amx]内的前m个最富有的人的信息.如果财富值相同就就先 ...
- 【PAT甲级】1055 The World's Richest (25 分)
题意: 输入两个正整数N和K(N<=1e5,K<=1000),接着输入N行,每行包括一位老板的名字,年龄和财富.K次询问,每次输入三个正整数M,L,R(M<=100,L,R<= ...
- pat 1055 区间前k个
http://pat.zju.edu.cn/contests/pat-a-practise/1055 第二组数据比较大,如果单纯排序直接检索会超时,因为每次都是对所有数据进行遍历. N/200=500 ...
随机推荐
- 洛谷P4012 深海机器人问题(费用流)
传送门 图给的好坑……还得倒过来…… 用大佬的图做个示范 我们考虑左图吧 把每一个点向下连边,容量$1$,费用为给出的价值(表示一个机器人可以过去取得标本) 再连一条边,容量$inf$,费用$0$(表 ...
- 条目七《如果容器中包含了通过new操作创建的指针,切记在容器对象析构前将指针delete掉》
如果容器中包含了通过new操作创建的指针,切记在容器对象析构前将指针delete掉 在STL中容器是智能的,可以在容器销毁时自动调用容器里对象的析构函数来销毁容器存储的对象. STL的容器虽然比较智能 ...
- Vue 环境配置
最开始摸element的时候,像盲人摸象,完全没有头绪. https://panjiachen.github.io/vue-element-admin-site/zh/guide/essentials ...
- 扩展jQuery高亮网页中的文本选中
<script type="text/javascript"> //1.扩展jQuery $.fn.selectRange = function (start, end ...
- js 事件的阶段
事件有三个阶段: 1.事件捕获阶段 :从外向内 2.事件目标阶段 :最开始选择的那个 3.事件冒泡阶段 : 从里向外 为元素绑定事件 addEventListener("没有on的事件类 ...
- 把display 属性改为block样式变化问题
可能的值 值 描述 none 此元素不会被显示. block 此元素将显示为块级元素,此元素前后会带有换行符. inline 默认.此元素会被显示为内联元素,元素前后没有换行符. inline-blo ...
- HDU-2063(二分图匹配模板题)
过山车Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submissi ...
- 洛谷 P2542 [AHOI2005]航线规划(Link-cut-tree)
题面 洛谷 bzoj 题解 离线处理+LCT 有点像星球大战 我们可以倒着做,断边变成连边 我们可以把边变成一个点 连边时,如果两个点本身不联通,就\(val\)赋为\(1\),并连接这条边 如果,两 ...
- zoj4062 Plants vs. Zombies 二分+模拟(贪心的思维)
题目传送门 题目大意:有n个植物排成一排,标号为1-n,每株植物有自己的生长速度ai,每对植物浇一次水,该株植物就长高ai,现在机器人从第0个格子出发,每次走一步,不能停留,每一步浇一次水,总共可以走 ...
- 什么是SocLib
一.SocLib简介 SoCLib是用于多处理器片上系统(MP-SoC)虚拟原型开发的开放平台.该项目始于ANR创建的项目, 现在维持在https://www.lip6.fr/.该平台的核心是用于虚拟 ...