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. ansible 循环register

    在有循环的task中使用register,register保存的是一个列表,整个属性为results results 是一个单个循环返回的结果的列表 - debug: msg="{{ ite ...

  2. DevExpress v17.2新版亮点——CodeRush篇(一)

    用户界面套包DevExpress v17.2日前终于正式发布,本站将以连载的形式为大家介绍各版本新增内容.本文将介绍了CodeRush v17.2 的新功能,快来下载试用新版本! Unit Test ...

  3. SharePoint 用户配置文件服务

    1. 获取用户配置文件服务应用程序标识 Get-SPServiceApplication 2. 获取指定的用户配置文件服务应用程序实例 $profileSA = Get-SPServiceApplic ...

  4. <二叉树的基本操作>

    #include<stdio.h> #include<stdlib.h> #include<string.h> #define num 100 #define OK ...

  5. win+linux双系统安装笔记

    1.出现win与linux只能引导之一启动,此时启动linux并更改启动文件可以用linux自带的grub2引导启动 2.ubuntu64位安装时需要联网,因为其有bug,镜像文件中缺少gurb2,需 ...

  6. JAVA解压文件

    package com.chauvet.utils; import java.io.File; import java.io.FileOutputStream; import java.io.IOEx ...

  7. 重磅来袭,开源Asp.Net MVC网上商城BrnShop正式发布,提供源码下载(转)

    BrnShop网上商城是以Asp.Net mvc3为基础开发的网上商城,源代码完全开源(企业版的源代码目前还没有完全整理完成,一旦整理完成也全部开源). 啥话也不说了,直接上源码:下载源码(由于公司服 ...

  8. apache airflow docker 运行简单试用

    airflow 是一个编排.调度和监控workflow的平台,由Airbnb开源,现在在Apache Software Foundation 孵化. airflow 将workflow编排为tasks ...

  9. 一 JAVA整体概念以及安装部署

    JAVA 基本概念  JVM(JAVA virtual machine)java虚拟机,是java的能跨平台的核心,java的跨平台实现,就是在各种系统中布置JVM,然后java应用运行在JVM中,相 ...

  10. AQS源码分析

    AQS全程为AbstractQueuedSynchronizer,其定义了一套多线程访问共享资源的同步框架,大部分的同步类的实现都依赖于他,比如ReentrantLock,ReentrantReadW ...