刚开始就是排好序按照从头开始遍历,结果我用脚趾头想都超时了,果然一个三分的点超时了
后来细细想想,这种类型题目,大体都是哪些方法,什么辅助数组、双指针
果然这题设置大小两个动点,用辅助的数组记录每个位置对应的最大子序列元素个数
#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; } 
新闻热点
疑难解答