刚开始就是排好序按照从头开始遍历,结果我用脚趾头想都超时了,果然一个三分的点超时了
后来细细想想,这种类型题目,大体都是哪些方法,什么辅助数组、双指针
果然这题设置大小两个动点,用辅助的数组记录每个位置对应的最大子序列元素个数
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
typedef long long LL;
vector<LL> s;
int main(){
LL n, p;
cin>>n>>p;
for(LL i = 0; i < n; i++){
LL temp;
scanf("%lld",&temp);
s.push_back(temp);
}
sort(s.begin(),s.end());
int minp = 0;
int maxp = 0;
int num[n] = {0};
while(maxp < s.size()){
if(s[maxp] <= s[minp] * p){
num[maxp] = maxp - minp + 1;
maxp++;
}
else{
minp++;
}
}
LL maxnum = 0;
for(LL i = 0; i < n; i++){
if(maxnum < num[i]){
maxnum = num[i];
}
}
cout<<maxnum;
return 0;
}
本文介绍了一种解决特定类型序列问题的有效算法。通过使用双指针技术和辅助数组记录最大合法子序列长度,实现了对输入序列的有效遍历与计算。

2577

被折叠的 条评论
为什么被折叠?



