PAT1085. Perfect Sequence

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 }

时间: 2024-08-06 16:05:53

PAT1085. Perfect Sequence的相关文章

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(Advanced Level)Practice--1085(Perfect Sequence)

Pat1085代码 题目描述: 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 s

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

A1085. 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 pa

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

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

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

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