pat 1085. Perfect Sequence (25)

时间限制

300 ms

内存限制

32000 kB

代码长度限制

16000 B

判题程序

Standard

作者

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 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


这道题目,在时间上,就考察一个二分查找。还有就是一个long long因为乘法有可能超过int

[cpp] view plaincopyprint?

  1. #include <stdio.h>
  2. #include <vector>
  3. #include <algorithm>
  4. using namespace std;
  5. vector<long long> nums;
  6. int bSearch(long
    long num,int n)
  7. {
  8. int l = 0,r = n-1,mid;
  9. while(l<=r)
  10. {
  11. mid = (l+r)/2;
  12. if(nums[mid]>num)
  13. {
  14. r = mid-1;
  15. }else if(nums[mid]<num)
  16. {
  17. l = mid+1;
  18. }else
  19. {
  20. return mid;
  21. }
  22. }
  23. return l;
  24. }
  25. int main()
  26. {
  27. long long n,p,tmp1,m,index;
  28. long long i,j,tmpMax=0,resMax;
  29. scanf("%lld%lld",&n,&p);
  30. for(i=0;i<n;i++)
  31. {
  32. scanf("%lld",&tmp1);
  33. nums.push_back(tmp1);
  34. }
  35. sort(nums.begin(),nums.end());
  36. for(i=0;i<n;i++)
  37. {
  38. m = nums[i]*p;
  39. index = bSearch(m, n);
  40. if(nums[n-1]<=m)
  41. {
  42. tmpMax = n-1-i+1;
  43. }else
  44. {
  45. tmpMax = index-i;
  46. }
  47. if(tmpMax>resMax)
  48. {
  49. resMax = tmpMax;
  50. }
  51. }
  52. printf("%lld\n",resMax);
  53. return 0;
  54. }
#include <stdio.h>
#include <vector>
#include <algorithm>
using namespace std;
vector<long long> 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]<num)
        {
            l = mid+1;
        }else
        {
            return mid;
        }
    }
    return l;
}
int main()
{
    long long n,p,tmp1,m,index;
    long long i,j,tmpMax=0,resMax;
    scanf("%lld%lld",&n,&p);
    for(i=0;i<n;i++)
    {
        scanf("%lld",&tmp1);
        nums.push_back(tmp1);
    }
    sort(nums.begin(),nums.end());
    for(i=0;i<n;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\n",resMax);
    return 0;
}
时间: 2024-12-22 07:30:44

pat 1085. Perfect Sequence (25)的相关文章

1085. Perfect Sequence (25)【二分查找】——PAT (Advanced Level) Practise

题目信息 1085. Perfect Sequence (25) 时间限制300 ms 内存限制65536 kB 代码长度限制16000 B 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 minim

1085. Perfect Sequence (25)-PAT甲级真题

1085. Perfect Sequence (25) 时间限制 300 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 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

1085. Perfect Sequence (25)

自己想的比较好的一个算法,时间大大节省 时间限制 300 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 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

1085 Perfect Sequence (25 分)

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

PAT (Advanced Level) 1085. Perfect Sequence (25)

可以用双指针(尺取法),也可以枚举起点,二分终点. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #include<map> #include<queue> #include<stack> #include<algorithm> using namespace std; int n; long long k; long l

PAT 1085. Perfect Sequence

还好只是要求长度,如果是完整序列的话估计得跪 #include <cstdio> #include <climits> #include <cstdlib> #include <algorithm> using namespace std; int main() { int N,p; scanf("%d%d", &N, &p); if (N < 1) { printf("%d\n", 0); re

1085. Perfect Sequence (25)-水题

#include <iostream> #include <cstdio> #include <algorithm> #include <string.h> #include <cmath> using namespace std; const int maxn=100000+5; long long a[maxn]; int main() { int n,p; scanf("%d %d",&n,&p); in

pat1085. Perfect Sequence (25)

1085. Perfect Sequence (25) 时间限制 300 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 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

PAT Perfect Sequence (25)

题目描述 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