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 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 (≤10?5??) is the number of integers in the sequence, and p (≤10?9??) is the parameter. In the second line there are N positive integers, each is no greater than 10?9??.

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


注意点:1.int*int 的范围必须用long long;    2.if(a[n-1]<=x) return n;
 1 #include<bits/stdc++.h>
 2 using namespace std;
 3
 4 const int maxn =100010;
 5
 6 int a[maxn];
 7
 8 int n,p;
 9
10 int binary_Search(int i,long long x){
11
12     if(a[n-1]<=x)
13         return n;
14
15     int left=i+1,right=n-1;
16
17     while(left<right){
18         int mid = (left+right)/2;
19
20         if(a[mid]>x){
21             right =mid;
22         }else{
23             left= mid+1;
24         }
25     }
26
27     return left;
28
29
30 }
31
32
33 int main(){
34
35     cin>>n>>p;
36
37     for(int i=0;i<n;i++)
38         cin>>a[i];
39
40     sort(a,a+n);
41
42     int ans=1;
43
44     for(int i=0;i<n;i++){
45
46     //    int j=binary_Search(i,(long long)a[i]*p);
47
48         int j=upper_bound(a+i+1,a+n,(long long)a[i]*p)-a;
49
50         ans=max(ans,j-i);
51
52     //    cout<<j<<" "<<i<<endl;
53     }
54
55
56
57     cout<<ans<<endl;
58
59     return 0;
60
61 }


原文地址:https://www.cnblogs.com/moranzju/p/11121176.html

时间: 2024-08-18 18:43:31

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

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

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

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 甲级 1051 Pop Sequence (25 分)(模拟栈,较简单)

1051 Pop Sequence (25 分) Given a stack which can keep M numbers at most. Push N numbers in the order of 1, 2, 3, ..., N and pop randomly. You are supposed to tell if a given sequence of numbers is a possible pop sequence of the stack. For example, if

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