给一个序列A,求一个最长子序列,长度为2k+1
满足前k+1个数递增,后k个递减
LIS板子题,正反各求一次,枚举中间的交点
#include<iostream> #include<cstring> #include <algorithm> using namespace std; const int N=1e5; int st[N],f[N],g[N]; int n,a[N]; void dp(){ int i,j,l; memset(st,0,sizeof st); st[1]=a[1],l=1; f[1]=1; for(i=2;i<=n;i++){ if(a[i]>st[l]) f[i]=++l,st[l]=a[i]; j=lower_bound(st+1,st+1+l,a[i])-st; f[i]=j; st[j]=a[i]; } memset(st,0,sizeof st); st[1]=a[n],l=1; g[n]=1; for(i=n-1;i>0;i--){ if(a[i]>st[l]) g[i]=++l,st[l]=a[i]; j=lower_bound(st+1,st+1+l,a[i])-st; g[i]=j; st[j]=a[i]; } } int main(){ int i,j; while(cin>>n){ for(i=1;i<=n;i++) cin>>a[i]; dp(); int ans=0; for(i=1;i<=n;i++){ ans=max(ans,min(f[i],g[i])*2-1); } cout<<ans<<endl; } }
标签:10534,int,memset,bound,st,uva,include From: https://www.cnblogs.com/towboa/p/16834887.html