Pat 1085. Perfect Sequence (25)
Pat 1085. Perfect Sequence (25) Time Limit 300 MS
The memory limit is 32000 kB.
Code length limit: 16000 B
Criterion author 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 your 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 82 3 20 4 5 1 6 7 8 9
Sample Output:
8
In terms of time, we will look into a binary search. There is also a long because multiplication may exceed int
[Cpp]
- # Include # Include
- # Include using namespace std;
- Vector Nums; int bSearch (long num, int n)
- {Int l = 0, r = n-1, mid;
- While (l <= r ){
- Mid = (l + r)/2; if (nums [mid]> num)
- {R = mid-1;
- } Else if (nums [mid] L = mid + 1;} else
- {Return mid;
- }}
- Return l ;}
- Int main (){
- Long n, p, tmp1, m, index; long I, j, tmpMax = 0, resMax;
- Scanf (% lld, & n, & p); for (I = 0; I {Scanf (% lld, & tmp1 );
- Nums. push_back (tmp1 );}
- Sort (nums. begin (), nums. end (); for (I = 0; I {M = nums [I] * p;
- Index = bSearch (m, n); if (nums [n-1] <= m)
- {TmpMax = n-1-i + 1;
- } Else {
- TmpMax = index-I ;}
- If (tmpMax> resMax ){
- ResMax = tmpMax ;}
- } Printf (% lld, resMax );
- Return 0 ;}
#include
#include
#include using namespace std;vector
nums;int bSearch(long long num,int n){ int l = 0,r = n-1,mid; while(l<=r) { mid = (l+r)/2; if(nums[mid]>num) { r = mid-1; }else if(nums[mid]
resMax) { resMax = tmpMax; } } printf(%lld,resMax); return 0;}