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 (≤10​5​​) is the number of integers in the sequence, and p (≤10​9​​) is the parameter. In the second line there are N positive integers, each is no greater than 10​9​​.

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的更多相关文章

  1. PAT 甲级 1085 Perfect Sequence

    https://pintia.cn/problem-sets/994805342720868352/problems/994805381845336064 Given a sequence of po ...

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

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

  3. PAT甲级题分类汇编——杂项

    本文为PAT甲级分类汇编系列文章. 集合.散列.数学.算法,这几类的题目都比较少,放到一起讲. 题号 标题 分数 大意 类型 1063 Set Similarity 25 集合相似度 集合 1067 ...

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

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

  5. PAT甲级1131. Subway Map

    PAT甲级1131. Subway Map 题意: 在大城市,地铁系统对访客总是看起来很复杂.给你一些感觉,下图显示了北京地铁的地图.现在你应该帮助人们掌握你的电脑技能!鉴于您的用户的起始位置,您的任 ...

  6. PAT甲级1127. ZigZagging on a Tree

    PAT甲级1127. ZigZagging on a Tree 题意: 假设二叉树中的所有键都是不同的正整数.一个唯一的二叉树可以通过给定的一对后序和顺序遍历序列来确定.这是一个简单的标准程序,可以按 ...

  7. PAT甲级1123. Is It a Complete AVL Tree

    PAT甲级1123. Is It a Complete AVL Tree 题意: 在AVL树中,任何节点的两个子树的高度最多有一个;如果在任何时候它们不同于一个,则重新平衡来恢复此属性.图1-4说明了 ...

  8. PAT甲级1119. Pre- and Post-order Traversals

    PAT甲级1119. Pre- and Post-order Traversals 题意: 假设二叉树中的所有键都是不同的正整数.一个唯一的二进制树可以通过给定的一对后序和顺序遍历序列来确定,也可以通 ...

  9. PAT甲级1114. Family Property

    PAT甲级1114. Family Property 题意: 这一次,你应该帮我们收集家族财产的数据.鉴于每个人的家庭成员和他/她自己的名字的房地产(房产)信息,我们需要知道每个家庭的规模,以及他们的 ...

随机推荐

  1. spring AOP正则表达式的几个问题

    基于包名的正则表达式,是根据抽象父类的包名过滤,还是实现类的包名过滤, 还是抽象父类实现的接口的包名过滤? org.springframework.aop.aspectj.AspectJExpress ...

  2. Python 3.6 TypeEror: iter() returned non-iterator of type

    环境:Python 3.6 class Fabs(object): def __init__(self,max): self.max = max self.n, self.a, self.b = 0, ...

  3. Mac使用zsh导致maven命令无效的解决方案

    第一步: vim ~/.zshrc 第二步:在.zshrc末尾加上 source ~/.bash_profile: 保存推出了 第三步 source ~/.bash_profile

  4. javascrip基础学习

    JS是一种解释性脚本语言,在网页开发用经常用到(HTML CSS),用于控制网页的行为.现在RTT的柿饼UI也是用JS来开发的,所以很有必要学习一下. 注释:// ./*  */ 语句分行: 折行\ ...

  5. C++的静态Static

    类的静态数据成员是属于类(即与类关联)而不属于类的每个对象(不与类的每个对象关联)(相当于该静态对象在所有的类对象中共享.),所以初始化方法与一般的变量不同,需要在类的构造函数之外进行初始化. 类的静 ...

  6. my___sublime Text配置

    sublime text 备份 插件下载 http://www.cnblogs.com/457220157-FTD/p/5546545.html https://www.jianshu.com/p/3 ...

  7. 我想和你们说说java和C++___C加加

    头痛头痛之一: java里面,本质上来说,一个类是一个程序员定义的类型,类是一种引用类型(reference type),这意味着该类类型的变量都可以引用该类的一个实例.从表面上,对象引用变量中似乎存 ...

  8. OpenStack Weekly Rank 2015.08.03

    Module Reviews Drafted Blueprints Completed Blueprints Filed Bugs Resolved Bugs Cinder 7 1 1 7 11 Sw ...

  9. spring的IOC和AOP详细讲解

    1.解释spring的ioc? 几种注入依赖的方式?spring的优点? IOC你就认为他是一个生产和管理bean的容器就行了,原来需要在调用类中new的东西,现在都是有这个IOC容器进行产生,同时, ...

  10. httpd.conf 配置

    # # This is the main Apache server configuration file. It contains the # configuration directives th ...