1085. Perfect Sequence (25)

1085. Perfect Sequence (25)

时间限制
300 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CAO, Peng

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

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<algorithm>
using namespace std;
const int maxn=1000007;
typedef long long ll;
template<class T>inline T read(T&x)
{
    char c;
    while((c=getchar())<=32);
    bool f=false;
    if(c=='-')f=true,c=getchar();
    for(x=0; c>32; c=getchar())x=x*10+c-'0';
    if(f)x=-x;
    return x;
}
template<class T>inline void write(T x)
{
    if(x<0)putchar('-');
    else if(x<10)putchar(x+'0');
    else write(x/10),putchar(x%10+'0');
}
template<class T>inline void writeln(T x)
{
    write(x);
    puts("");
}
//--------IO template--------------------

ll a[maxn];
int n;
int bin_ser(ll x)//二分查找
{
    int left=0,right=n-1;
    while(left<=right)
    {
        int mid=(left+right)>>1;
        if(a[mid]==x)return mid;
        else if(x<a[mid])right=mid-1;
        else left=mid+1;
    }
    return left-1;//没有的话,返回当前的位置
}

//不需要输入输出优化,只是自己这样写比较方便

 int main()
{
    int m,i,j,k,t;
    ll p;
    read(n);read(p);
    for(i=0;i<n;i++)read(a[i]);
    if(p<0){write(0);return 0;}
    sort(a,a+n);
    i=0;
    //while(a[i]*p<a[n-1])i++;write(n-i)///这样写只有一个case过不去,明显数据还是比较弱的
    int ans=1;
    for(i=0;i<n;i++)
    {
        ll x=p*a[i];
        int index=bin_ser(x);//二分枚举
        ans=max(index-i+1,ans);
    }
    writeln(ans);
    return 0;
}








你可能感兴趣的:(sequence)