pat甲级1085
1085 Perfect Sequence (25 分)
Given a sequence of positive integers and another positive integer p. The sequence is said to be a perfect sequence if M≤m×p where M and m are the maximum and minimum numbers in the sequence, respectively.
Now given a sequence and a parameter p, you are supposed to find from the sequence as many numbers as possible to form a perfect subsequence.
Input Specification:
Each input file contains one test case. For each case, the first line contains two positive integers N and p, where N (≤105) is the number of integers in the sequence, and p (≤109) is the parameter. In the second line there are N positive integers, each is no greater than 109.
Output Specification:
For each test case, print in one line the maximum number of integers that can be chosen to form a perfect subsequence.
Sample Input:
10 8
2 3 20 4 5 1 6 7 8 9
Sample Output:
8
第一种方法:
用max记录当前的最长序列长度,因为是要找出最大长度,因此对于每个i,只需要让j从i + max开始。
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std; vector<int> v; int main()
{
int N, i, j;
long p;
cin >> N >> p;
v.resize(N);
for (i = ; i < N; i++) cin >> v[i];
sort(v.begin(), v.end());
int t, max = ;
for (i = ; i < N; i++)
{
for (j = i + max; j < N && v[j] <= v[i] * p; j++);
if (j - i > max) max = j - i;
}
cout << max;
return ;
}
关于lower_bound()和upper_bound()的使用:
在从小到大的排序好的数组中:
(lower_bound( begin,end,num):从数组的begin位置到end-1位置二分查找第一个大于或等于num的数字,找到返回该数字的地址,不存在则返回end。通过返回的地址减去起始地址begin,得到找到数字在数组中的下标。
upper_bound( begin,end,num):从数组的begin位置到end-1位置二分查找第一个大于num的数字,找到返回该数字的地址,不存在则返回end。通过返回的地址减去起始地址begin,得到找到数字在数组中的下标。
来源:CSDN
原文:https://blog.csdn.net/qq_40160605/article/details/80150252 )
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std; vector<int> v; int main()
{
int N, i, j;
long p;
cin >> N >> p;
v.resize(N);
for (i = ; i < N; i++) cin >> v[i];
sort(v.begin(), v.end());
int t, max = ;
for (i = ; i < N; i++)
{
t = upper_bound(v.begin() + i, v.end(), v[i] * p) - (v.begin() + i);
if (t > max) max = t;
}
cout << max;
return ;
}
参考:
https://www.liuchuo.net/archives/1908
pat甲级1085的更多相关文章
- PAT 甲级 1085 Perfect Sequence
https://pintia.cn/problem-sets/994805342720868352/problems/994805381845336064 Given a sequence of po ...
- PAT甲级题解(慢慢刷中)
博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6102219.html特别不喜欢那些随便转载别人的原创文章又不给 ...
- PAT甲级题分类汇编——杂项
本文为PAT甲级分类汇编系列文章. 集合.散列.数学.算法,这几类的题目都比较少,放到一起讲. 题号 标题 分数 大意 类型 1063 Set Similarity 25 集合相似度 集合 1067 ...
- PAT甲级题分类汇编——序言
今天开个坑,分类整理PAT甲级题目(https://pintia.cn/problem-sets/994805342720868352/problems/type/7)中1051~1100部分.语言是 ...
- PAT甲级1131. Subway Map
PAT甲级1131. Subway Map 题意: 在大城市,地铁系统对访客总是看起来很复杂.给你一些感觉,下图显示了北京地铁的地图.现在你应该帮助人们掌握你的电脑技能!鉴于您的用户的起始位置,您的任 ...
- PAT甲级1127. ZigZagging on a Tree
PAT甲级1127. ZigZagging on a Tree 题意: 假设二叉树中的所有键都是不同的正整数.一个唯一的二叉树可以通过给定的一对后序和顺序遍历序列来确定.这是一个简单的标准程序,可以按 ...
- PAT甲级1123. Is It a Complete AVL Tree
PAT甲级1123. Is It a Complete AVL Tree 题意: 在AVL树中,任何节点的两个子树的高度最多有一个;如果在任何时候它们不同于一个,则重新平衡来恢复此属性.图1-4说明了 ...
- PAT甲级1119. Pre- and Post-order Traversals
PAT甲级1119. Pre- and Post-order Traversals 题意: 假设二叉树中的所有键都是不同的正整数.一个唯一的二进制树可以通过给定的一对后序和顺序遍历序列来确定,也可以通 ...
- PAT甲级1114. Family Property
PAT甲级1114. Family Property 题意: 这一次,你应该帮我们收集家族财产的数据.鉴于每个人的家庭成员和他/她自己的名字的房地产(房产)信息,我们需要知道每个家庭的规模,以及他们的 ...
随机推荐
- django组件之ajax
AJAX简介 AJAX(Asynchronous Javascript And XML)翻译成中文就是“异步Javascript和XML”.即使用Javascript语言与服务器进行异步交互,传输的数 ...
- Java实现范围内随机数
JDK1.7及以上 int randomNum = ThreadLocalRandom.current().nextInt(min, max + 1); // 包含max,所以要加1 JDK1.7以前 ...
- 搭建element-ui Vue结构
1.安装淘宝镜像 npm install cnpm -g --registry=https://registry.npm.taobao.org 2.安装webpack cnpm install web ...
- 事物及exec
事物3要出不多讲: 1.BEGIN TRANSACTION--开启事务 2.COMMIT TRANSACTION--事务执行 3.ROLLBACK TRANSACTION--事务回滚 俩总捕捉事物的方 ...
- docker基本命令日志
docker run - Run a command in a new container 启动一个新的容器,一般在docker pull之后首次运行此image -i 保持stdout打开 -t 打 ...
- log4j2重复打印日志问题解决
log4j2.xml <?xml version="1.0" encoding="UTF-8"?> <Configuration> &l ...
- 非局部均值(Nonlocal-Mean)
转载自网站:http://www.cnblogs.com/luo-peng/p/4785922.html 非局部均值去噪(NL-means) 非局部均值(NL-means)是近年来提出的一项新型的 ...
- Problem05 判断分数等级
题目:利用条件运算符的嵌套来完成此题:学习成绩>=90分的同学用A表示,60-89分之间的用B表示,60分以下的用C表示. 程序分析:(a>b)?a:b这是条件运算符的基本例子. impo ...
- hive - load CSV file NULL value 加载csv文件出现结果全是空值
这个问题的根源是,创建表的时候没有指定列分隔符还有行分隔符. 因此修改建表语句 问题依然重现,此问题苦恼了一个下午,有一次用describe tablename 发现了问题所在,原来是一直没有删除ta ...
- SQL datetime和smalldatetime区别
datetime 存储大小8个字节,精确到分后的3为小数,日期范围从1753 年 1 月 1 日到 9999 年 12 月 31 日:而 smalldatetime存储大小为4个字节,精确到分,日期范 ...