题目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. centos7.4 开启ftp服务

    1.安装vsftpd yum install -y ftp vsftpd 2.配置/etc/vsftpd/vsftpd.conf 修改项如下: #不允许匿名访问 anonymous_enable=NO ...

  2. Swift 计算三角形角度、两条边夹角

    /// 计算三点之间的角度 /// /// - Parameters: /// - p1: 点1 /// - p2: 点2(也是角度所在点) /// - p3: 点3 /// - Returns: 角 ...

  3. C++中 线程函数为静态函数 及 类成员函数作为回调函数

    线程函数为静态函数: 线程控制函数和是不是静态函数没关系,静态函数是在构造中分配的地址空间,只有在析构时才释放也就是全局的东西,不管线程是否运行,静态函数的地址是不变的,并不在线程堆栈中static只 ...

  4. nginx与apache的参考配置

    nginx与apache是两大最主流的服务器,功能强大,但配置起来也比较麻烦,对于初学者来讲可能有些地方并不完全清楚其作用,这里搜集了一些配置的作用及其使用方法.其中nginx提供了推荐配置,而apa ...

  5. Android BLE蓝牙开发-读写数据 获取UUID

    https://www.jianshu.com/p/3711cfbf7128 一个ble蓝牙设备有多个包括多个Profile 一个Profile中有多个服务Service(通过服务的uuid找到对应的 ...

  6. Atitit 翻页功能的解决方案与版本历史 v4 r49

    Atitit 翻页功能的解决方案与版本历史 v4 r49 1. 版本历史与分支版本,项目版本记录1 1.1. 主干版本历史1 1.2. 分支版本  项目版本记录.1 2. Easyui 的翻页组件2 ...

  7. Python验证码识别 安装Pillow、tesseract-ocr与pytesseract模块的安装以及错误解决

    1.安装Pillow pip install Pillow 2.安装tesseract-ocr OCR(Optical Character Recognition, 光学字符识别) 软件 安装包含两个 ...

  8. MySQL中文参考手册

    1 MySQL 的一般信息 这是MySQL参考手册:它记载了MySQL版本3.23.7-alpha. MySQL 是一个快速.多线程.多用户和强壮的SQL数据库服务器. 对Unix和 OS/2 平台, ...

  9. 3D 特征点概述(2)

    还是紧接着上一文章的思路继续介绍3D特征点的基本概念问题,还是这个表格: Feature Name Supports Texture / Color Local / Global / Regional ...

  10. 关于Unity中NGUI的3D角色血条的实现

    首先要到Unity的Assets Store里面去下载一个扩展的Package叫NGUI HUD Text v1.13(81),注意如果没有安装NGUI就必须先安装NGUI插件,否则会用不了,因为HU ...