原题:1085. Perfect Sequence (25)
解题思路:
双指针。将数据从小到大排序后,满足条件时不断将右指针右移,不满足时左指针右移,每次都保存最大个数即可。
代码如下:
#include<cstdio>
#include<algorithm>
using namespace std;
typedef long long ll;
const int maxn = 100000 + 5;
ll num[maxn];
int main()
{
ll p;
int n;
while(scanf("%d%lld", &n, &p) == 2)
{
for(int i = 0; i < n; i++) scanf("%lld", &num[i]);
sort(num, num+n);
int l = 0, r = 0, ans = 0;
for(;r < n; r++)
{
while(num[l]*p < num[r] && l < r) l++;
ans = max(ans, r - l + 1);
}
printf("%d\n", ans);
}
return 0;
}