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 思路:利用two pointer的思想可以减少很多时间,这种思想很宝贵。
1 #include <iostream> 2 #include <cstdio> 3 #include <algorithm> 4 using namespace std; 5 #define MAX 100010 6 long long data[MAX]; 7 int main(int argc, char *argv[]) 8 { 9 int N; 10 long long p; 11 scanf("%d%lld",&N,&p); 12 for(int i=0;i<N;i++) 13 { 14 scanf("%lld",&data[i]); 15 } 16 sort(data,data+N); 17 //two point两个指针的思想 18 int count=0; 19 int j=0; 20 for(int i=0;i<N;i++) 21 { 22 while(j<N) 23 { 24 if(data[j]<=data[i]*p) 25 { 26 if(j-i+1>count) 27 count=j-i+1; 28 j++; 29 } 30 else 31 break; 32 } 33 } 34 printf("%d\n",count); 35 return 0; 36 }