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. php返回数据格式

    PHP返回HTML代码:     header('Content-type:text/html; charset=utf-8'); PHP返回xml代码:header('content-type: t ...

  2. hxml总结

    段落<p> br 换行 hr 分割线 &nbsp 空格 &gt  > &lt < &  &amp h        7级标题 <i ...

  3. Log4web独立config配置

    第一步:config配置,独立文件的 <?xml version="1.0" encoding="utf-8"?> <configuratio ...

  4. (转)如何使用caffe的MATLAB接口

    编译MatCaffe 转自: http://blog.csdn.net/ws_20100/article/details/50525879 使用如下命令编译MatCaffe make all matc ...

  5. 分治法 - Divide and Conquer

    在计算机科学中,分治法是一种很重要的算法.分治法即『分而治之』,把一个复杂的问题分成两个或更多的相同或相似的子问题,再把子问题分成更小的子问题……直到最后子问题可以简单的直接求解,原问题的解即子问题的 ...

  6. 信息领域热词分析系统--python统计

    统计词语出现的频率,并且按从高到低的顺序报错在文件中 def main(): file=open("F:\大数据\大作业\分词后的文件\data4_xinxi.txt",'r') ...

  7. SpringBoot中使用Servlet

    1.在入口Application类上加入注解@ServletComponentScan @SpringBootApplication @ServletComponentScan public clas ...

  8. elastic search语句

    基本匹配: { "query":{ "match":{ "title" : "quick" } } } ES语法结构: ...

  9. HTML_DOM学习

    HTML DOM 树 通过ID/类名/标号可以定位HTML元素,然后可用JS改变这些元素的样式内容,并对DOM事件作出反应 对HTML事件的响应: onmousedown 和onmouseup/onc ...

  10. 理解 Linux backlog/somaxconn 内核参数

    https://jaminzhang.github.io/linux/understand-Linux-backlog-and-somaxconn-kernel-arguments/ 各参数的含义:h ...