1030 完美数列
/**
* 1.解题思路:数据结构采用vector,p为long long类型,当 m*p 时有可能会
* 超过int类型的范围,所以定义p为long long 类型,在乘法运算
* 时进行类型的自动转换。
* 首先将输入的数列进行从小到大的排序,然后定义两个变量i,j,
* i 从 0-n ,j 从 i + result到n
* j之所以从 i + result 是因为假设 在i = 0 的第一轮循环中,
* j = i+result 满足 v[j] <= v[i] * p ,此时, result = temp = j - i + 1 = 1
* 也就是目前最多可以有1个数组成完美数列,当在 i = 0 的下一次循环是,则就 j = i + result+1
* 的位置开始比较,若再次满足条件,则目前最多可以有1个数组成完美数列 ....
* 每次计算temp若大于result则更新result,最后输出result的值,即可以选择的最大个数用它们组成一个完美数列
*
* 2.参考博客:https://www.liuchuo.net/archives/500
*/
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main() {
int n;
long long p;
scanf("%d%lld", &n, &p);
vector<int> v(n);
for (int i = 0; i < n; i++)
cin >> v[i];
sort(v.begin(), v.end());
int result = 0, temp = 0;
for (int i = 0; i < n; i++) {
for (int j = i + result; j < n; j++) {
if (v[j] <= v[i] * p) {
temp = j - i + 1;
if (temp > result)
result = temp;
} else {
break;
}
}
}
cout << result;
return 0;
}