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
注意点:1.int*int 的范围必须用long long;
2.if(a[n-1]<=x) return n;
1 #include<bits/stdc++.h> 2 using namespace std; 3 4 const int maxn =100010; 5 6 int a[maxn]; 7 8 int n,p; 9 10 int binary_Search(int i,long long x){ 11 12 if(a[n-1]<=x) 13 return n; 14 15 int left=i+1,right=n-1; 16 17 while(left<right){ 18 int mid = (left+right)/2; 19 20 if(a[mid]>x){ 21 right =mid; 22 }else{ 23 left= mid+1; 24 } 25 } 26 27 return left; 28 29 30 } 31 32 33 int main(){ 34 35 cin>>n>>p; 36 37 for(int i=0;i<n;i++) 38 cin>>a[i]; 39 40 sort(a,a+n); 41 42 int ans=1; 43 44 for(int i=0;i<n;i++){ 45 46 // int j=binary_Search(i,(long long)a[i]*p); 47 48 int j=upper_bound(a+i+1,a+n,(long long)a[i]*p)-a; 49 50 ans=max(ans,j-i); 51 52 // cout<<j<<" "<<i<<endl; 53 } 54 55 56 57 cout<<ans<<endl; 58 59 return 0; 60 61 }