UVA - 10534 Wavio Sequence (二分法最长上升子序列)

大体题意:

给你一个长度为n 的数组,要求求出一个长度为奇数并且最长的上升子序列(不一定连续),要求前k + 1项严格递增,后k+1项严格递减!

输出最大长度?

思路:

最长上升子序列,

因为n 最多是10000,我们必须用n log n 级别的复杂度!

我们可以先预处理,求出到每一个位置i  的最长上升子序列的长度!

在倒着求,倒着枚举求出每一项的最长上升子序列长度! (这样反过来就是最长下降子序列)

然后枚举每一个位置!求出左边最长长度,和右边最长长度  取个最小值 就是当前的最长上升子序列!

不断更新答案即可!

详细见代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = 10000 + 10;
int a[maxn];
int lcs[maxn];
int dp[maxn],dp2[maxn];
int main(){
	int n;
	while(scanf("%d",&n) == 1){
		for (int i = 1; i <= n; ++i)scanf("%d",&a[i]);
		int p = 1;
		lcs[1] = a[1];
		dp[1] = 1;
		for (int i = 2; i <= n; ++i){
			if (a[i] > lcs[p]){
				lcs[++p] = a[i];
				dp[i] = p;
			}
			else {
				int m = lower_bound(lcs,lcs+p,a[i]) - lcs;
				lcs[m] = a[i];
				dp[i] = p;
			}
		}
		lcs[1] = a[n];
		dp2[n] = 1;
		p = 1;
		for (int i = n-1; i >= 1; --i){
			if (a[i] > lcs[p]){
				lcs[++p] = a[i];
				dp2[i] = p;
			}else {
				int m = lower_bound(lcs,lcs+p,a[i]) - lcs;
				lcs[m] = a[i];
				dp2[i] = p;
			}
			
		}
		int ans = -1;
		for (int i = 1; i <= n; ++i){
			int t = min(dp[i],dp2[i]);
			ans = max(ans,t);
		}
		printf("%d\n",2*ans-1);
	}
	
	
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值