B1030 perfect series

Given a positive integer sequence, and positive integers p, provided that the maximum number of columns is M, the minimum value is m, if M≤mp, the number of columns is called a perfect sequence.

Now given parameter p and a number of positive integers, please choose as many numbers constitute a perfect series.

Input format:
input of the first line gives two positive integers N and p, where N (≦ 10
. 5
) is the number of input positive integer, P (≦ 10
. 9
) is given parameters . The second line gives a positive integer N, the number of not more than 10
. 9
.

Output format:
output line number of the maximum number thereof can be selected form a perfect series.

Sample input:
10. 8
2 20 is. 4. 3. 1. 5. 6. 7. 8. 9

Output Sample:
8
ideas:
first sequence of ascending order, from left to right scan sequence, its total number of each of a [i], finds the first one a [i + 1] -a [ n-1] binary than a [i] * p is the number of position j, ji is the case for the position i satisfies a [j] <= a [ i] * p farthest length
C ++ code:

#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn=100010;
int n,p,a[maxn];
int binary(int i,long long x){
	if(a[n-1]<=x){
		return n;
	}
	int l=i+1,r=n-1,mid;
	while(l<r){
		mid=(l+r)/2;
		if(a[mid]<=x){
			l=mid+1;
		}
		else{
			r=mid;
		}
	}
	return l;
} 
int main(){
	scanf("%d %d",&n,&p);
	for(int i=0;i<n;i++){
		scanf("%d",&a[i]);
	}
	sort(a,a+n);
	int ans=1;
	for(int i=0;i<n;i++){
		int j=binary(i,(long long )a[i]*p);
		ans=max(ans,j-i);
	}
	printf("%d\n",ans);
}
Published 39 original articles · won praise 2 · Views 3331

Guess you like

Origin blog.csdn.net/u014424618/article/details/104678495