PAT_A1085#Perfect Sequence

Source:

PAT A1085 Perfect Sequence (25 分)

Description:

Given a sequence of positive integers and another positive integer p. The sequence is said to be a perfect sequence if Mm×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 (≤) is the number of integers in the sequence, and p (≤) is the parameter. In the second line there are N positive integers, each is no greater than 1.

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

Keys:

  • 双指针

Code:

 1 #include<cstdio>
 2 #include<algorithm>
 3 using namespace std;
 4 const int M=1e5+10;
 5 typedef long long LL;
 6 
 7 int main()
 8 {
 9 #ifdef ONLINE_JUDGE
10 #else
11     freopen("Test.txt", "r", stdin);
12 #endif // ONLINE_JUDGE
13 
14     LL n,p,s[M];
15     scanf("%lld%lld", &n,&p);
16     for(LL i=0; i<n; i++)
17         scanf("%lld", &s[i]);
18     sort(s,s+n);
19     LL i=0,j=1,step=1;
20     while(j < n)
21     {
22         while(j<n && s[j]<=s[i]*p)
23             j++;
24         if(j-i > step)
25             step = j-i;
26         i++;
27         j=i+step;
28     }
29     printf("%lld", step);
30 
31     return 0;
32 }

 

posted @ 2019-08-27 17:01  林東雨  阅读(154)  评论(0编辑  收藏  举报