题目

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

题目分析

已知正整数序列seq[N],最大值为M,最小值为m,已知另一个正整数p(<=10^9),从数列中抽出一部分数字,求可以满足M<=m*p的数字最多抽取个数

要满足M<=mp抽取的数字最多(即:M与m中间夹的数字最多),需要取所有满足M<=mp的情况中,m最小,M最大

解题思路

思路 01(最优、二分查找、查找M复杂度O(logn))

  1. 对seq[N]升序排序
  2. 依次遍历seq[i],在i+1到N之间,找到最大满足M<=mp的数字(即:第一个满足大于mp的数字下标j-1)

思路 02 (two pointer、查找M复杂度O(n))

  1. 对seq[N]升序排序
  2. 依次遍历seq[i],j初始为0,开始从上次j往后找(因为i+1后m增大,m*q>=M,所以M增大,j只能在上次j之后)

易错点

  1. p(<=10^9),所以m*p有可能超过int范围,数组元素类型需为long long,否则第5个测试点错误
  2. 取第一个大于mp的数字下标-1,而不是第一个大于等于mp的数字下标(因为大于的情况下要-1,等于的情况下不需要-1,处理麻烦)
  3. 思路02中,只能从前往后找第一个不满足条件m*q>=M的,不能从后往前找最后一个满足条件的(测试点4超时)

Code

Code 01

#include <iostream>
#include <algorithm>
using namespace std;
int main(int argc,char * argv[]) {
int n,p;
scanf("%d %d",&n,&p);
long long seq[n]= {0}; // 若为int,第5个测试点错误
for(int i=0; i<n; i++) {
scanf("%d",&seq[i]);
}
sort(seq,seq+n);
int maxnum=0;
for(int i=0; i<n; i++) {
// 二分查找
int left=i+1,right=n;
int mid = left+((right-left)>>1);
while(left<right) {
mid = left+((right-left)>>1);
if(seq[mid]>seq[i]*p) { //若是求第一个大于等于seq[i]*p,测试点2错误
right=mid;
} else {
left=mid+1;
}
}
if(right-i>maxnum)maxnum=right-i;
}
printf("%d",maxnum);
return 0;
}

Code 01

#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;
int main(int argc,char * argv[]) {
int n,p;
scanf("%d %d",&n,&p);
long long seq[n]= {0}; // 若为int,第5个测试点错误
for(int i=0; i<n; i++) {
scanf("%d",&seq[i]);
}
sort(seq,seq+n);
// 写法一:
int maxnum=0,j = 0;
for(int i=0; i<n; i++) {
while(j<n&&seq[i]*p>=seq[j]) j++;
maxnum=max(maxnum,j-i);
} // 写法二:
// int i=0,j=0,maxnum=1;
// while(i<n&&j<n) {
// while(j<n&&seq[j]<=(long long)seq[i]*p) {
// maxnum=max(maxnum,j-i+1);
// j++;
// }
// i++;
// } /*
使用下面代码,第四个测试点超时
j从后往前找最后一个满足条件的,测试点4超时
*/
// int maxnum=0,prej=0; //prej用于记录上次j的位置,之后的j只可能比prej大,m*p>=M;i+1因为m增大了,所以M一定增大
// for(int i=0; i<n; i++) {
// int j = n-1;
// while(prej<=j&&seq[i]*p<seq[j]) j--;
// maxnum=max(maxnum,j-i+1);
// prej=j;
// } printf("%d",maxnum);
return 0;
}

PAT Advanced 1085 Perfect Sequence (25) [⼆分,two pointers]的更多相关文章

  1. 1085 Perfect Sequence (25 分)

    Given a sequence of positive integers and another positive integer p. The sequence is said to be a p ...

  2. 【PAT甲级】1085 Perfect Sequence (25 分)

    题意: 输入两个正整数N和P(N<=1e5,P<=1e9),接着输入N个正整数.输出一组数的最大个数使得其中最大的数不超过最小的数P倍. trick: 测试点5会爆int,因为P太大了.. ...

  3. PAT 甲级 1051 Pop Sequence (25 分)(模拟栈,较简单)

    1051 Pop Sequence (25 分)   Given a stack which can keep M numbers at most. Push N numbers in the ord ...

  4. PAT Advanced 1020 Tree Traversals (25 分)

    1020 Tree Traversals (25 分)   Suppose that all the keys in a binary tree are distinct positive integ ...

  5. 1085. Perfect Sequence (25) -二分查找

    题目如下: Given a sequence of positive integers and another positive integer p. The sequence is said to ...

  6. PAT 甲级 1085 Perfect Sequence

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

  7. PAT (Advanced Level) 1085. Perfect Sequence (25)

    可以用双指针(尺取法),也可以枚举起点,二分终点. #include<cstdio> #include<cstring> #include<cmath> #incl ...

  8. 1085. Perfect Sequence (25)

    the problem is from PAT,which website is http://pat.zju.edu.cn/contests/pat-a-practise/1085 At first ...

  9. PAT Advanced 1140 Look-and-say Sequence (20 分)

    Look-and-say sequence is a sequence of integers as the following: D, D1, D111, D113, D11231, D112213 ...

随机推荐

  1. 用cmd运行java可以javac不行(win10)

    今天发现个有趣的问题,用cmd运行java可以javac不行.(win10) java-home和classpath配置没有问题,最后发现问提出先在path,在这里看并没有异常. 在上面图片中点击编辑 ...

  2. [题解] LuoguP4609 [FJOI2016]建筑师

    传送门 首先对于高度为\(n\)的建筑,他的左边有\(A-1\)个建筑能被看到,右边有\(B-1\)个建筑能被看到,这两者类似,所以先来看左边. 一个建筑将会遮挡住它后面的高度比它矮的建筑,直到一个高 ...

  3. elasticsearch下载与安装

    目录 安装之前 下载 安装 测试 安装之前 必须注意的是:安装路径不允许有中文及空格和非法字符,尤其是中文 下载 打开elasticsearch官网.选择免费试用. 选择对应产品与版本(选择6.5.4 ...

  4. h5-伸缩布局-小案例

    1.伸缩布局案例1-基本页面布局 1.1.html <div class="layout"> <header></header> <mai ...

  5. 吴裕雄--天生自然C++语言学习笔记:C++ 常量

    常量是固定值,在程序执行期间不会改变.这些固定的值,又叫做字面量. 常量可以是任何的基本数据类型,可分为整型数字.浮点数字.字符.字符串和布尔值. 常量就像是常规的变量,只不过常量的值在定义后不能进行 ...

  6. VUE注册全局组件和局部组件

    全局组件 第一步:在components文件夹下建立一个子文件Users.vue <template> <div class="users"> {{msg} ...

  7. C++程序--helloworld

    #include<iostream>using namespace std; int main(){ cout << "hello world!" < ...

  8. jQuery.ajax提交JSON数据

    $.ajax({ type: 'POST', url: "URL", contentType:'application/json', //需要加contentType crossD ...

  9. 18 12 23 html 基本学习

    一个html的基本结构如下: <!DOCTYPE html> <html lang="en"> <head> <meta charset= ...

  10. [SDOI2016]游戏(树剖+李超树)

    趁着我把李超树忘个一干二净的时候来复习一下吧,毕竟马上NOI了. 题解:看着那个dis就很不爽,直接把它转换成深度问题,然后一条直线x->y,假设其lca为z,可以拆分成x->z和z-&g ...