PAT - Grade B 1030 Perfect Sequence

1030. Perfect Sequence (25)

time limit
300 ms
memory limit
65536 kB
code length limit
8000 B
Judgment procedure
Standard
author
CAO, Peng

Given a sequence of positive integers and a positive integer p, let the maximum value in this sequence be M and the minimum value be m, if M <= m * p, the sequence is called a perfect sequence.

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

Input format:

The first line of input gives two positive integers N and p, where N (<= 10 5 ) is the number of positive integers entered and p (<= 10 9 ) is the given parameter. The second line gives N positive integers, each not exceeding 10 9 .

Output format:

Output the maximum number of numbers you can choose in a line that can be used to form a perfect sequence.

Input sample:
10 8
2 3 20 4 5 1 6 7 8 9
Sample output:
8
Idea: Arrange directly from small to large and then calculate from the beginning
#include<cstdio>
#include<algorithm>
#define inf 0x3f3f3f3f
using namespace std;

int main(){
	double arr[100005];
	int n;
	double p;
//	cin >> n >> p;
    scanf("%d %lf", &n, &p);
	for(int i = 0; i < n; i++) scanf("%lf", &arr[i]);
	sort(arr, arr + n);
	double max = -1, min = inf, cnt = 0;
	for(int i = 0; i < n; i++){
		for(int j = i + cnt; j < n; j++){
			if(arr[j] > arr[i] * p) break;//Otherwise jump out and start counting from the cnt of the next number if this number also matches then ++ Otherwise continue
			else cnt++; //+1 when the minimum value is encountered * p is greater than the current number 
		}
	}
	printf("%.0f", cnt);
	return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325755109&siteId=291194637