uva 10534 Wavio Sequence

本文介绍了一种算法,用于在给定序列中找到长度为2N-1的最长子序列,其中前N个元素递增,后N个元素递减。通过计算两个最长上升子序列并枚举边界值,得出最优答案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题意:

找一个最长(假设长度为2N-1)的子序列,使得前N个元素递增,后N个元素递减。

和高中一道做过的题目几乎一模一样,noip的合唱队行,只不过这个要求递增和递减的数目相同。

正着求一遍最长上升子序列,再逆着求一遍最长上升子序列,然后枚举每一个数,看起两边延伸的最短的长度乘2减一就好。

由于数据很多,LIS要用nlogn的方法,很简单啦。

//  Created by Chenhongwei in 2015.
//  Copyright (c) 2015 Chenhongwei. All rights reserved.

#include"iostream"
#include"cstdio"
#include"cstdlib"
#include"cstring"
#include"climits"
#include"queue"
#include"cmath"
#include"map"
#include"set"
#include"stack"
#include"vector"
#include"sstream"
#include"algorithm"
using namespace std;
typedef long long ll;
const int inf=1e8;
int a[10010],n;
int f1[10010],f2[10010],g[10010];
int main()
{	
	//ios::sync_with_stdio(false);
	// freopen("in.txt","r",stdin);
	//freopen("out.txt","w",stdout);
	while(scanf("%d",&n)!=EOF)
	{
		for(int i=1;i<=n;i++)
			scanf("%d",&a[i]);
		for(int i=1;i<=n+1;i++)
			g[i]=inf;
		for(int i=1;i<=n;i++)
		{
			int k=lower_bound(g+1,g+n+1,a[i])-g;
			f1[i]=k;
			g[k]=a[i];
		}
		for(int i=1;i<=n+1;i++)
			g[i]=inf;
		for(int i=n;i>=1;i--)
		{
			int k=lower_bound(g+1,g+n+1,a[i])-g;
			f2[i]=k;
			g[k]=a[i];
		}
		int ans=0;
		for(int i=1;i<=n;i++)
			ans=max(ans,min(f1[i],f2[i])*2-1);
		cout<<ans<<endl;
	}
	return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值