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. MyEclipse WebSphere开发教程:WebSphere 7安装指南(二)

    [周年庆]MyEclipse个人授权 折扣低至冰点!立即开抢>> [MyEclipse最新版下载] 三.禁用Windows系统服务 默认情况下,当安装WebSphere Applicati ...

  2. Linux内核参数优化记录

    //fs.file-max 最大打开文件数 //fs.nr_open=20480000 单个进程允许打开的文件句柄上限 //信号量及共享内存,可以使用ipcs -l来获取 //kernel.sem 信 ...

  3. HDU 4970

    http://acm.hdu.edu.cn/showproblem.php?pid=4970 比赛的时候线段树水过的,比赛后线段树一直T,看了下正解真的是智商压制 题意:走直线,长度1-N,还有一些人 ...

  4. BZOJ4872: [Shoi2017]分手是祝愿【概率期望DP】【思维好题】

    Description Zeit und Raum trennen dich und mich. 时空将你我分开.B 君在玩一个游戏,这个游戏由 n 个灯和 n 个开关组成,给定这 n 个灯的初始状态 ...

  5. gtx860M,cuda9路1080p多高斯运动检测测试

    多高斯背景差分,非常吃cpu,特别是多路视屏,所以想用gpu做检测 后面的跟踪一系列的规则判断用cpu opencv+cuda+stl做了个测试 代码: // MTTestCudaMog.cpp : ...

  6. 剑指offer-int类型负数补码中1的个数-位操作

    在java中Interger类型表示的最大数是 System.out.println(Integer.MAX_VALUE);//打印最大整数:2147483647 这个最大整数的二进制表示,头部少了一 ...

  7. 南阳oj-ASCII码排序-用了一个晚上

    #include <iostream> #include <sstream> #include <stdio.h> #include <string> ...

  8. hive 创建orc表

    orc表 创建具备ACID及Transactions的表 这里的表需要具备下面几个条件:  1. 必须以 ORC 格式存储  2. 必须分 bucket,且不能 sort  3. 必须显式声明tran ...

  9. Oracle 跨库 查询 复制表数据

    在目前绝大部分数据库有分布式查询的需要.下面简单的介绍如何在oracle中配置实现跨库访问. 比如现在有2个数据库服务器,安装了2个数据库.数据库server A和B.现在来实现在A库中访问B的数据库 ...

  10. Tensoflw.js - 02 - 模型与内存管理(易懂)

    Tensoflw.js - 02 - 模型与内存管理(易懂) 参考 W3Cschool 文档:https://www.w3cschool.cn/tensorflowjs/ 本文主要翻译一些英文注释,添 ...