UVA-10534 (LIS)

题意:

给定一个长为n的序列,求一个最长子序列,使得该序列的长度为2*k+1,前k+1个数严格递增,后k+1个数严格单调递减;

思路:

可以先求该序列最长单调递增和方向单调递增的最长序列,然后枚举那第k+1个数更新答案就好了;

AC代码:

/************************************************ 
┆  ┏┓   ┏┓ ┆    
┆┏┛┻━━━┛┻┓ ┆ 
┆┃       ┃ ┆ 
┆┃   ━   ┃ ┆ 
┆┃ ┳┛ ┗┳ ┃ ┆ 
┆┃       ┃ ┆  
┆┃   ┻   ┃ ┆ 
┆┗━┓    ┏━┛ ┆ 
┆  ┃    ┃  ┆       
┆  ┃    ┗━━━┓ ┆ 
┆  ┃  AC代马   ┣┓┆ 
┆  ┃           ┏┛┆ 
┆  ┗┓┓┏━┳┓┏┛ ┆ 
┆   ┃┫┫ ┃┫┫ ┆ 
┆   ┗┻┛ ┗┻┛ ┆       
************************************************ */  


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <bits/stdc++.h>
#include <stack>

using namespace std;

#define For(i,j,n) for(int i=j;i<=n;i++)
#define mst(ss,b) memset(ss,b,sizeof(ss));

typedef  long long LL;

template<class T> void read(T&num) {
    char CH; bool F=false;
    for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar());
    for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar());
    F && (num=-num);
}
int stk[70], tp;
template<class T> inline void print(T p) {
    if(!p) { puts("0"); return; }
    while(p) stk[++ tp] = p%10, p/=10;
    while(tp) putchar(stk[tp--] + '0');
    putchar('
');
}

const LL mod=20071027;
const double PI=acos(-1.0);
const int inf=1e9;
const int N=1e4+100;
const int maxn=(1<<8);
const double eps=1e-8;

int a[N],g[N],pre[N],nex[N],n;

inline void Init()
{
    For(i,1,n)g[i]=inf;
}
int main()
{       
        while(cin>>n)
        {
            For(i,1,n)read(a[i]);
            Init();
            For(i,1,n)
            {
                int temp=lower_bound(g+1,g+n+1,a[i])-g;
                pre[i]=temp;
                g[temp]=a[i];
            }
            Init();
            for(int i=n;i>0;i--)
            {
                int temp=lower_bound(g+1,g+n+1,a[i])-g;
                nex[i]=temp;
                g[temp]=a[i];
            }
            int ans=0;
            For(i,1,n)
            {
                int temp=min(pre[i],nex[i]);
                ans=max(ans,2*temp-1);
            }
            cout<<ans<<"
";
        }
        return 0;
}

  

原文地址:https://www.cnblogs.com/zhangchengc919/p/5717648.html