如题:http://www.patest.cn/contests/pat-b-practise/1030
给定一个正整数数列,和正整数p,设这个数列中的最大值是M,最小值是m,如果M <= m * p,则称这个数列是完美数列。
现在给定参数p和一些正整数,请你从中选择尽可能多的数构成一个完美数列。
输入格式:
输入第一行给出两个正整数N和p,其中N(<= 105)是输入的正整数的个数,p(<= 109)是给定的参数。第二行给出N个正整数,每个数不超过109。
输出格式:
在一行中输出最多可以选择多少个数可以用它们组成一个完美数列。
输入样例:
10 8 2 3 20 4 5 1 6 7 8 9
输出样例:
8
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define max(a,b)(a>b?a:b)
long long a[100005];
int my_bound(int first,int last,int value)
{
int pos=first;
while(first+1<last)
{
int mid=(first+last)/2;
if(a[mid]>value)
{
last=mid-1;
pos=last;
}
else
{
first=mid;
pos=first;
}
}
return pos;
}
int main()
{
//freopen("C:\\1.txt","r",stdin);
int n,p;
cin>>n>>p;
int i;
for(i=0;i<n;i++)
cin>>a[i];
sort(a,a+n);
int res=0;
for(i=0;i<n;i++)
{
long long t=a[i]*p;
// int pos=my_bound(i,n,t); //返回[i,n)最后一个<=t的值
// int len=pos-i+1;
int pos=lower_bound(a+i,a+n,t)-a;
if(pos==n)
pos=n-1;
else if(a[pos]>t)
pos-=1;
int len=pos-i+1;
res=max(len,res);
}
cout<<res<<endl;
}