PAT 1085 Perfect Sequence

题目:

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

地址:http://pat.zju.edu.cn/contests/pat-a-practise/1085

注意题意,说的是从数组中取任意多个数字满足完美子序列,所以对数字的要求没有顺序性。我的方法是,先对数组做一个排序,时间为O(nlogn),然后在用线性的时间找到这个序列,如果该题用O(n^2)的算法则会有一个case超时。现在来说说如何在线性时间找到这个序列:首先,用下标i表示当前找到的最大值,用下标j表示当前找到的最小值。从j=0开始,i可以从0一直遍历到第一不满足data[0]*p >= data[i],此时,说明序列0到i-1是满足完美子序列,也就是以data[0]为最小值的最大完美子序列,我们把其个数记为count;然后j加一,此时最小值变为data[1],这是data[1]*p >= data[0]*p>=data[i],也就是说原来的count个也都满足,但由于data[0]比data[1]要小,所以不满足data[1]为最小值,故count减一,然后在从当前的i开始往后继续找,找到第一个不满足data[1]*p>=data[i],此时的count为以data[1]为最小值的最大完美子序列的个数,然后j继续加一,count减一,i继续向前遍历,如此循环直到j走到底。由于i,j在遍历时都没有回头,故时间复杂度为线性的。代码:

 #include <stdio.h>
#include <algorithm>
using namespace std; int main()
{
long long n,p;
long long data[];
while(scanf("%lld%lld",&n,&p) != EOF){
for(int i = ; i < n; ++i){
scanf("%lld",&data[i]);
}
sort(data,data+n);
int result = ;
int count = ;
int i = , j = ;
long long sum;
while(i < n){
sum = data[j] * p;
while(i < n && data[i] <= sum){
++count;
++i;
}
if(count > result)
result = count;
++j;
--count; }
printf("%d\n",result);
}
return ;
}

PAT 1085 Perfect Sequence的更多相关文章

  1. PAT 1085 Perfect Sequence[难]

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

  2. 1085 Perfect Sequence (25 分)

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

  3. PAT 甲级 1085 Perfect Sequence

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

  4. 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 ...

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

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

  6. 1085. Perfect Sequence

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

  7. 1085 Perfect Sequence (25 分)

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

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

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

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

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

随机推荐

  1. 《精通Ext JS 》

    <精通Ext JS > 基本信息 原书名:Mastering Ext JS 作者: (巴西)Loiane Groner 译者: 卢俊祥 丛书名: 图灵程序设计丛书 出版社:人民邮电出版社 ...

  2. Coursera课程《大家的Python》中一些资料

    Printed copies of Python for Informatics are available for $10 or less from Amazon and $2 or less on ...

  3. 把Scala代码当作脚本运行

    1. 在类UNIX系统上作为脚本运行 在类Unix系统上,你可以设置一个shell前导词来执行脚本.如下例: Script.scala #!/usr/bin/env scala !# println( ...

  4. 【Todo】React & Nodejs学习 &事件驱动,非阻塞IO & JS知识栈:Node为主,JQuery为辅,Bootstrap & React为辅辅,其他如Angular了解用途即可

    JS知识栈:Node为主,JQuery为辅,Bootstrap & React为辅辅,其他如Angular了解用途即可 今天在学习ReactJS和NodeJS,看到关于ReactJS的这篇文章 ...

  5. UVA 10012 How Big Is It?(暴力枚举)

      How Big Is It?  Ian's going to California, and he has to pack his things, including his collection ...

  6. Best Time to Buy and Sell Stock III leetcode java

    题目: Say you have an array for which the ith element is the price of a given stock on day i. Design a ...

  7. Java+FlashWavRecorder实现网页录音并上传

    [注意] 最新版本号请看这里:http://uikoo9.com/blog/detail/java-flashwavrecorder [前言] 肯定有需求要网页录音,并且要上传.这奇葩需求. 然后找到 ...

  8. C#.NET常见问题(FAQ)-abstract抽象类如何理解

    例如有太多相似,但是不一样的类,他们都继承自同一个基类(比如大型游戏有各个种族,每个种族有各种人物,加起来几百种类型,然后基本上他们都是一个角色,都有基本相同的属性和方法,比如都会走,只是速度不同,都 ...

  9. 翻译记忆软件-塔多思TRADO经典教程_4

    SDL Trados是世界上最流行的计算机辅助翻译(CAT) 软件,在全球拥有20万多客户,全球500强企业有超过90%的公司都在使用SDL Trados来为日常的本地化翻译工作服务.我们的使命是以强 ...

  10. .NET破解之迅捷PDF转换器(续)

    在以前的博文<.NET破解之迅捷PDF转换器>中使用了暴力破解的方法,现在软件版本从5.0升级到6.3,所以也尝试用新的方法. 方法一:暴力破解法 如往常一样,查找搜索到关键的函数,即Is ...