1030 perfect sequence (25 points)

Given a positive integer sequence, and a positive integer p, let the maximum value of the sequence be m, and the minimum value be m. if M ≤ mp, the s...

Given a positive integer sequence, and a positive integer p, let the maximum value of the sequence be m, and the minimum value be m. if M ≤ mp, the sequence is said to be perfect.

Now, given the parameter p and some positive integers, please choose as many numbers as possible to form a perfect sequence.

Input format:

The first line of the input gives two positive integers, N and p, where n (< 10 5) is the number of positive integers and p (< 10 9) is the given parameter. The second line gives n positive integers, each of which is no more than 10 9.

Output format:

How many numbers can you choose to output in a row can be used to form a perfect sequence.

Input example:

10 8 2 3 20 4 5 1 6 7 8 9

Output example:

8

To points:

#include<cstdio> #include<algorithm> using namespace std; typedef long long LL; const int maxn = 100010; int A[maxn]; int main(){ LL n, p; scanf("%lld%lld", &n, &p); for (int i = 0; i < n; ++i) { scanf("%d", &A[i]); } sort(A, A + n); int i = 0, j =0, count = 1; while(j < n && i< n){ while(j < n&& A[i] * p >= A[j]){ count = max(count, j - i + 1); j++; } i++; } printf("%d\n", count); return 0; }

Reference code two (dichotomy):

#include<cstdio> #include<algorithm> using namespace std; typedef long long LL; const int maxn = 100010; int A[maxn]; int N, p; int binartSearch(int i, LL x){ if(A[N - 1] <= x) return N; int left = i + 1, right = N -1; while(left < right){ int mid = (left + right) / 2; if(A[mid] > x){ right = mid; }else{ left = mid + 1; } } return left; } int main(){ scanf("%d%d", &N, &p); for (int i = 0; i < N; ++i) { scanf("%d", &A[i]); } sort(A, A + N); int ans = 1; for (int j = 0; j < N; ++j) { int num = binartSearch(j, (LL)A[j] * p); ans = max(ans , num - j); } printf("%d\n", ans); return 0; }

Reference code 3:

#include<cstdio> #include<algorithm> using namespace std; typedef long long LL; const int maxn = 100010; int A[maxn]; int N, p; //int binartSearch(int i, LL x){ // if(A[N - 1] <= x) return N; // int left = i + 1, right = N -1; // while(left < right){ // int mid = (left + right) / 2; // if(A[mid] > x){ // right = mid; // }else{ // left = mid + 1; // } // } // return left; //} int main(){ scanf("%d%d", &N, &p); for (int i = 0; i < N; ++i) { scanf("%d", &A[i]); } sort(A, A + N); int ans = 1; for (int j = 0; j < N; ++j) { int num = upper_bound(A + j + 1, A + N,(LL)A[j] * p) - A; ans = max(ans , num - j); } printf("%d\n", ans); return 0; }

4 December 2019, 17:25 | Views: 4344

Add new comment

For adding a comment, please log in
or create account

0 comments