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. DevExpress v17.2新版亮点—DevExtreme篇(一)

    用户界面套包DevExpress DevExtreme v17.2终于正式发布,本站将以连载的形式为大家介绍各版本新增内容.本文将介绍了DevExtreme v17.2 的New Color Sche ...

  2. Alpha版本展示

    一.团队成员的简介和个人博客地址,团队的源码仓库地址. 1.团队的源码仓库地址 我们主要通过码云来管理代码,码云地址:https://gitee.com/ljl36/team_programming ...

  3. cmd 环境变量设置的简单方法

    1.查看当前所有可用的环境变量:输入 set 即可查看. 2.查看某个环境变量:输入 “set 变量名”即可,比如想查看temp变量的值,即输入 set temp 3.修改环境变量 :输入 “set ...

  4. RESTful API 学习

    /********************************************************************************* * RESTful API 学习 ...

  5. 【c++基础】accumulate

    accumulate // accumulate example #include <iostream> // std::cout #include <functional> ...

  6. 记一次MSSQL到MySQL大数据迁移过程

    工作中遇到一个需求 要将MSSQL数据库中共计12张表的数据大概1000W行数据迁移到MySQL数据库中,去年另一个同事负责这件事情,他采用的方法是先将MSSQL数据库里的数据生成同MySQL数据库表 ...

  7. PR4

    4.1视频切换特效: 视频切换:视频和视频之间的过渡 4.2特效参数设置方法 4.3视频切换特效的技巧 多张图片,拖入特效慢. 选中多张图片 右键选中“设置所选..” 设置图片时间: 点常规后 设置完 ...

  8. 51Nod 1058: N的阶乘的长度(斯特林公式)

    1058 N的阶乘的长度  基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题  收藏  关注 输入N求N的阶乘的10进制表示的长度.例如6! = 720,长度为3. Inp ...

  9. Codeforces Round #462 (Div. 2) B-A Prosperous Lot

    B. A Prosperous Lot time limit per test 1 second memory limit per test 256 megabytes input standard ...

  10. CTF-练习平台-Misc之 想蹭网先接开密码

    十三.想蹭网先接开密码 用wireshark打开,WiFi认证过程重点在WPA的四次握手包,找到EAPOL握手协议,另存为 根据题目提示,写字典 #include<stdio.h>int  ...