题目https://pintia.cn/problem-sets/994805342720868352/problems/994805421066272768

题意:

给定n个人的名字,年龄和身价。k次查询,每次询问某一个年龄区间的人的前m个最富有的人。

思路:

我好傻系列。

刚开始撒比排序先按照年龄从小到大排然后存某一年龄的开始下标和个数。然后每次复制出某一区间的人,再按答案要求排序。

好傻。后来想想直接就按照答案的要求排序,对于符合要求的那些人,他们输出的时候的相对顺序就是固定的。

所以我只需要从头到尾找到前m个符合要求的人就可以了。

 #include<stdio.h>
#include<stdlib.h>
#include<map>
#include<set>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<vector>
#include<cmath>
#include<stack>
#include<queue> #define inf 0x7fffffff
using namespace std;
typedef long long LL;
typedef pair<int, int> pr; int n, k;
const int maxn = 1e5 + ;
struct node{
string name;
int age;
int net_worth;
}peo[maxn], tmp[maxn]; bool cmp1(node a, node b)
{
if(a.age == b.age)return a.net_worth > b.net_worth;
else return a.age < b.age;
} bool cmp(node a, node b)
{
if(a.net_worth == b.net_worth){
if(a.age == b.age)return a.name < b.name;
else return a.age < b.age;
}
else return a.net_worth > b.net_worth;
} int main()
{
scanf("%d%d", &n, &k);
for(int i = ; i <= n; i++){
cin>>peo[i].name>>peo[i].age>>peo[i].net_worth;
}
sort(peo + , peo + + n, cmp); int m, amin, amax;
for(int cas = ; cas <= k; cas++){
scanf("%d%d%d", &m, &amin, &amax);
printf("Case #%d:\n", cas); int cnt = ;
for(int pos = ; pos <= n; pos++){
if(peo[pos].age >= amin && peo[pos].age <= amax){
cnt++;
cout<<peo[pos].name;
printf(" %d %d\n", peo[pos].age, peo[pos].net_worth);
}
if(cnt == m)break;
}
if(cnt == ){
printf("None\n");
}
}
return ;
}

PAT甲级1055 The World's Richest【排序】的更多相关文章

  1. 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 ...

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

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

  3. PAT甲级——A1055 The World's Richest

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

  4. PAT甲级题分类汇编——排序

    本文为PAT甲级分类汇编系列文章. 排序题,就是以排序算法为主的题.纯排序,用 std::sort 就能解决的那种,20分都算不上,只能放在乙级,甲级的排序题要么是排序的规则复杂,要么是排完序还要做点 ...

  5. PAT甲级1017题解——模拟排序

    题目分析: 本题我第一次尝试去做的时候用的是优先队列,但是效率不仅代码量很大,而且还有测试样例过不去,很显然没有找到一个好的数据结构来解决这道题目(随着逐渐的刷PAT甲级的题会发现有时选择一个好的解题 ...

  6. PAT甲级题解(慢慢刷中)

    博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6102219.html特别不喜欢那些随便转载别人的原创文章又不给 ...

  7. 【转载】【PAT】PAT甲级题型分类整理

    最短路径 Emergency (25)-PAT甲级真题(Dijkstra算法) Public Bike Management (30)-PAT甲级真题(Dijkstra + DFS) Travel P ...

  8. PAT甲级题分类汇编——序言

    今天开个坑,分类整理PAT甲级题目(https://pintia.cn/problem-sets/994805342720868352/problems/type/7)中1051~1100部分.语言是 ...

  9. PAT 甲级真题题解(1-62)

    准备每天刷两题PAT真题.(一句话题解) 1001 A+B Format  模拟输出,注意格式 #include <cstdio> #include <cstring> #in ...

随机推荐

  1. zip压缩解压

    zip在linux中使用相对不太频繁,但是在window中使用频繁! zip参数 -q //不显示指令的执行过程,静默执行-r //递归处理文件-T //检测zip文件是否可用-u //更新文件,根据 ...

  2. MDK-ARM输出HEX文件重命名设置

    输出的可执行文件和库的名称就是在这里定义.比如我们常见输出Hex文件,其名称就是这里定义的.

  3. java解惑--摘要

    (1)下面是一个试图解决上述问题的程序,它会打印出什么呢?public class Change{public static void main(String args[]){System.out.p ...

  4. Shell脚本编程(一):初识shell script

    Shell简介 Shell是一个命令解释器,它是一个用 C 语言编写的程序,它是用户使用 Linux 的桥梁.Shell 是指一种应用程序,这个应用程序提供了一个界面,用户通过这个界面访问操作系统内核 ...

  5. JS 引擎执行机制

    JS JS 是单线程语音 JS 的 Event Loop 是 JS 的执行机制.类似于 Android Handler 消息分发机制 JS 单线程 技术的出现都跟现实世界里的应用场景密切相关 JS 单 ...

  6. vue项目使用webpack loader把px转换为rem

    下载lib-flexible https://github.com/amfe/lib-flexible npm i lib-flexible --save 在main.js中引入lib-flexibl ...

  7. 【Linux高级驱动】触摸屏驱动的移植

    触摸屏驱动的移植 流程 注意:看框架图 1.添加input.c组件 Device Drivers  ---> Input device support  --->  Generic inp ...

  8. 按enter键触发登录事件

    $(document).keydown(function(event){ if(event.keyCode==13){ $(".submit").click(); } });

  9. js实现鼠标拖动框选元素小狗

    方法一: <html> <head></head> <style> body{padding:100px;} .fileDiv{float:left;w ...

  10. c++ clr编译dll在c#调用时出现“试图加载不正确的格式”“找不到dll”错误的解决

    用depends发现缺了一堆API-MS-WIN什么的dll,网上查找是因为少了VC++2010,VC++2015等一系列,装好后仍然不行,原来这种错误并不是该原因导致的,也并不缺少那些dll(dep ...