PAT甲级1055 The World's Richest【排序】
题目: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【排序】的更多相关文章
- 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 1055 The World's Richest[排序][如何不超时]
1055 The World's Richest(25 分) Forbes magazine publishes every year its list of billionaires based o ...
- PAT甲级——A1055 The World's Richest
Forbes magazine publishes every year its list of billionaires based on the annual ranking of the wor ...
- PAT甲级题分类汇编——排序
本文为PAT甲级分类汇编系列文章. 排序题,就是以排序算法为主的题.纯排序,用 std::sort 就能解决的那种,20分都算不上,只能放在乙级,甲级的排序题要么是排序的规则复杂,要么是排完序还要做点 ...
- PAT甲级1017题解——模拟排序
题目分析: 本题我第一次尝试去做的时候用的是优先队列,但是效率不仅代码量很大,而且还有测试样例过不去,很显然没有找到一个好的数据结构来解决这道题目(随着逐渐的刷PAT甲级的题会发现有时选择一个好的解题 ...
- PAT甲级题解(慢慢刷中)
博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6102219.html特别不喜欢那些随便转载别人的原创文章又不给 ...
- 【转载】【PAT】PAT甲级题型分类整理
最短路径 Emergency (25)-PAT甲级真题(Dijkstra算法) Public Bike Management (30)-PAT甲级真题(Dijkstra + DFS) Travel P ...
- PAT甲级题分类汇编——序言
今天开个坑,分类整理PAT甲级题目(https://pintia.cn/problem-sets/994805342720868352/problems/type/7)中1051~1100部分.语言是 ...
- PAT 甲级真题题解(1-62)
准备每天刷两题PAT真题.(一句话题解) 1001 A+B Format 模拟输出,注意格式 #include <cstdio> #include <cstring> #in ...
随机推荐
- TF_Server gRPC failed, call return code:8:Received message larger than max (45129801 vs. 4194304)
tensorflow_serving 遇到错误:gRPC failed, call return code:8:Received message larger than max (45129801 v ...
- NeoFinder for Mac(增强型文件管理工具)破解版安装
1.软件简介 NeoFinder 是 macOS 系统上一款帮助用户管理磁盘的 Mac 工具,NeoFinder for mac 能迅速组织您的数据,无论是在外部或内部磁盘,或任何其他卷.它能记 ...
- 9.12 翻译系列:数据注解特性之ConcurrencyCheck【EF 6 Code-First系列】
原文链接:https://www.entityframeworktutorial.net/code-first/concurrencycheck-dataannotations-attribute-i ...
- 【转载】JAVA基础:注解
原文:https://www.cnblogs.com/xdp-gacl/p/3622275.html#undefined 一.认识注解 注解(Annotation)很重要,未来的开发模式都是基于注解的 ...
- 【Android】Android布局文件的一些属性值
第一类:属性值 true或者 false android:layout_centerHrizontal 水平居中 android:layout_centerVertical 垂直居中 androi ...
- LeetCode: Recover Binary Search Tree 解题报告
Recover Binary Search Tree Two elements of a binary search tree (BST) are swapped by mistake. Recove ...
- MXNET:权重衰减
权重衰减是应对过拟合问题的常用方法. \(L_2\)范数正则化 在深度学习中,我们常使用L2范数正则化,也就是在模型原先损失函数基础上添加L2范数惩罚项,从而得到训练所需要最小化的函数. L2范数惩罚 ...
- 水塘抽样(Reservoir Sampling)问题
水塘抽样是一系列的随机算法,其目的在于从包含n个项目的集合S中选取k个样本,其中n为一很大或未知的数量,尤其适用于不能把所有n个项目都存放到主内存的情况. 在高德纳的计算机程序设计艺术中,有如下问题: ...
- JVM 内部原理(三)— 基本概念之类文件格式
JVM 内部原理(三)- 基本概念之类文件格式 介绍 版本:Java SE 7 每位使用 Java 的程序员都知道 Java 字节码在 Java 运行时(JRE - Java Runtime Envi ...
- [微信开发] 微信JSAPI - 获取用户地理位置信息
参考博客 http://blog.csdn.net/u013142781/article/details/50503299 主要JS 方法 wx.getLocation 获取地理位置信息传递参数 成功 ...