Not Only Algorithm,不仅仅是算法,关注数学、算法、数据结构、程序员笔试面试以及一切涉及计算机编程之美的内容 。。
你的位置:NoAlGo博客 » 题解 » 

PAT 1085. 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

解答

#include <cstdio>
#include <algorithm>
using namespace std;

const int mx = 100000 + 10;  
int n, p, a[mx];

int main()
{
	scanf("%d%d", &n, &p); 
	for (int i = 0; i < n; i++) scanf("%d", a+i);
	sort(a, a+n);

	int ans = 0, i = 0, j = 0;
	while (j < n)
	{
		if (a[i] >= double(a[j]) / p) ans = max(ans, j-i+1), j++; //乘法可能溢出
		else i++;
	}
	printf("%d\n", ans);

	return 0;
}
上一篇: 下一篇:

我的博客

NoAlGo头像编程这件小事牵扯到太多的知识,很容易知其然而不知其所以然,但真正了不起的程序员对自己程序的每一个字节都了如指掌,要立足基础理论,努力提升自我的专业修养。

站内搜索

最新评论