Pat 1085. Perfect Sequence (25)

Source: Internet
Author: User

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]
  1. # Include # Include
  2. # Include using namespace std;
  3. Vector Nums; int bSearch (long num, int n)
  4. {Int l = 0, r = n-1, mid;
  5. While (l <= r ){
  6. Mid = (l + r)/2; if (nums [mid]> num)
  7. {R = mid-1;
  8. } Else if (nums [mid] L = mid + 1;} else
  9. {Return mid;
  10. }}
  11. Return l ;}
  12. Int main (){
  13. Long n, p, tmp1, m, index; long I, j, tmpMax = 0, resMax;
  14. Scanf (% lld, & n, & p); for (I = 0; I {Scanf (% lld, & tmp1 );
  15. Nums. push_back (tmp1 );}
  16. Sort (nums. begin (), nums. end (); for (I = 0; I {M = nums [I] * p;
  17. Index = bSearch (m, n); if (nums [n-1] <= m)
  18. {TmpMax = n-1-i + 1;
  19. } Else {
  20. TmpMax = index-I ;}
  21. If (tmpMax> resMax ){
  22. ResMax = tmpMax ;}
  23. } Printf (% lld, resMax );
  24. 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;}
          
         
        
       


     

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.