1085. Perfect Sequence (25)

本文介绍了一种解决特定类型序列问题的有效算法。通过使用双指针技术和辅助数组记录最大合法子序列长度,实现了对输入序列的有效遍历与计算。

刚开始就是排好序按照从头开始遍历,结果我用脚趾头想都超时了,果然一个三分的点超时了

后来细细想想,这种类型题目,大体都是哪些方法,什么辅助数组、双指针

果然这题设置大小两个动点,用辅助的数组记录每个位置对应的最大子序列元素个数

#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; 
} 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值