首页 > 学院 > 开发设计 > 正文

1085. Perfect Sequence (25)

2019-11-14 12:08:22
字体:
来源:转载
供稿:网友

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

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

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

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


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表