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 Npositive 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

题意:从一组序列中找出最大的满足条件的序列的长度。

分析:二分法,可以用STL例upper_bound函数。注意乘积可能超过int型表示范围
 /**
 * Copyright(c)
 * All rights reserved.
 * Author : Mered1th
 * Date : 2019-02-26-13.58.21
 * Description : A1085
 */
 #include<cstdio>
 #include<cstring>
 #include<iostream>
 #include<cmath>
 #include<algorithm>
 #include<string>
 #include<unordered_set>
 #include<map>
 #include<vector>
 #include<set>
 using namespace std;
 ;
 int a[maxn];
 int main(){
 #ifdef ONLINE_JUDGE
 #else
     freopen("1.txt", "r", stdin);
 #endif
     int n,p;
     scanf("%d%d",&n,&p);
     ;i<n;i++){
         scanf("%d",&a[i]);
     }
     sort(a,a+n);
     ;
     ;i<n;i++){
         ,a+n,(long long)a[i]*p)-a;
         num=max(num,j-i);
     }
     printf("%d\n",num);
     ;
 }

另一种方法是双指针,思路就是定义两个指针i,j,均从0开始遍历,j不断右移直到不满足条件,再i++。

 /**
 * Copyright(c)
 * All rights reserved.
 * Author : Mered1th
 * Date : 2019-02-26-14.18.14
 * Description : A1085
 */
 #include<cstdio>
 #include<cstring>
 #include<iostream>
 #include<cmath>
 #include<algorithm>
 #include<string>
 #include<unordered_set>
 #include<map>
 #include<vector>
 #include<set>
 using namespace std;
 ;
 int a[maxn];
 int main(){
 #ifdef ONLINE_JUDGE
 #else
     freopen("1.txt", "r", stdin);
 #endif
     int n,p;
     scanf("%d%d",&n,&p);
     ;i<n;i++){
         scanf("%d",&a[i]);
     }
     sort(a,a+n);
     ,j=,num=;
     while(i<n&&j<n){
         while(j<n&&a[j]<=(long long)a[i]*p){
             num=max(num,j-i+);
             j++;
         }
         i++;
     }
     cout<<num;
     ;
 }

1085 Perfect Sequence (25 分)的更多相关文章

  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 Advanced 1085 Perfect Sequence (25) [⼆分,two pointers]

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

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

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

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

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

  5. 1085. Perfect Sequence (25)

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

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

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

  7. 1085. Perfect Sequence (25)-水题

    #include <iostream> #include <cstdio> #include <algorithm> #include <string.h&g ...

  8. pat1085. Perfect Sequence (25)

    1085. Perfect Sequence (25) 时间限制 300 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CAO, Peng Give ...

  9. PAT 1085 Perfect Sequence[难]

    1085 Perfect Sequence (25 分) Given a sequence of positive integers and another positive integer p. T ...

随机推荐

  1. 玩转X-CTR100 l STM32F4 l U-Blox NEO-6M GPS卫星定位-nmealib解码库移植解码

    我造轮子,你造车,创客一起造起来!塔克创新资讯[塔克社区 www.xtark.cn ][塔克博客 www.cnblogs.com/xtark/ ]      本文介绍X-CTR100控制器 扩展GPS ...

  2. AngularJS中的DOM value与view value

    在看AngularJS的文档的时候经常会见到三个名词:DOM value.view value和model value. model value是模型值,view value是视图值,可这个DOM v ...

  3. L212

    Just 33 minutes into the New Year, NASA's New Horizons probe made space exploration history, flying ...

  4. firefox下reset()不好使的问题

    最近在测试项目时发现,在firefox下,form.reset()方法对于隐藏的<input>等不起效果,导致程序中出现了错误,以下面为例: js代码: document.agentFor ...

  5. PostgreSQL数据库创建只读用户总结

    好久没有弄,有点忘了,今天有客户问这个问题,发现几个SQL还解决不了,于是总结一下: --以超级用户登录数据库,创建用户: postgres=# create user test_read_only ...

  6. grafana+influxdb+telegraf监控服务器cpu,内存和硬盘

    随便抄了一篇,目前我们的项目也在用,这个是linux和windows通吃的一种监控方案,非常有效,详细和优美,需要监控什么具体的业务内容,自己向influxdb中插入就行了. 监控服务器状态是运维必不 ...

  7. S3TC IAP15F2K61S2点亮一个发光二极管keil和stc-isp软件操作

    1.安装破解软件 2.打开STC-ISP,找到头文件,选择保存文件 3.找到keil的安装目录,keil/C51/INC 并保存 4.在桌面新建文件夹 5.打开keil 6.找到在桌面上新建的文件夹 ...

  8. calculate MAC,Lisence,Checksum and generate muti-file

    /************************************************************************* * calculate MAC,Lisence,C ...

  9. Generate And Play A Tone In Android hacking

    /*********************************************************************************** * Generate And ...

  10. STS或eclipse安装SVN插件(转)

    安装sts--SVN插件 简介:sts是与eclipse类似的Java IDE开发工具(不了解的百度) 1.sts菜单栏 help->install New Software 依据大家的版本选择 ...