UVa 10534 Wavio Sequence ( DP 二分 最长递增子序列 )

题意  求一个序列a某一位的最长递增序列(lis)和最长递减序列(lds)中最小值的最大值

开始直接用DP写了   然后就超时了  后来看到别人说要用二分把时间复杂度优化到O(n*logn)   果然如此   用一个栈s保存长度为i的LIS的最小尾部s[i]  top为栈顶即当前LIS的长度  初始s[1]=a[1]  top=1 遍历整个序列  当a[i]>s[top]时  a[i]入栈
in[i]=top   否则 在栈中查找(二分)第一个大于等于a[i]的下标pos  并替换  这样就增加了LIS增长的潜力 in[i]=pos;

in[i]表示以a[i]为尾部的LIS的长度   求lds的过程也是类似

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N = 10005;
int a[N], in[N], de[N], s[N], m, n, top, pos;

int BinSearch (int k, int le, int ri)
{
    while (le <= ri)
    {
        m = (le + ri) >> 1;
        if (s[m] >= k) ri = m - 1;
        else le = m + 1;
    }
    return ri + 1;
}

void lis()
{
    memset (s, 0, sizeof (s));
    memset (in, 0, sizeof (in));
    s[1] = a[1];
    in[1] = top = 1;
    for (int i = 2; i <= n; ++i)
    {
        if (s[top] < a[i])
        {
            s[++top] = a[i];
            in[i] = top;
        }
        else
        {
            pos = BinSearch (a[i], 1, top);
            s[pos] = a[i];
            in[i] = pos;
        }
    }
}

void lds()
{
    memset (s, 0, sizeof (s));
    memset (de, 0, sizeof (de));
    s[1] = a[n];
    de[n] = top = 1;
    for (int i = n - 1; i >= 1; --i)
    {
        if (s[top] < a[i])
        {
            s[++top] = a[i];
            de[i] = top;
        }
        else
        {
            pos = BinSearch (a[i], 1, top);
            s[pos] = a[i];
            de[i] = pos;
        }
    }
}

int main()
{
    while (scanf ("%d", &n) != EOF)
    {
        for (int i = 1; i <= n; ++i)
            scanf ("%d", &a[i]);
        int ans = 1;
        lis();
        lds();
        for (int i = 1; i <= n; ++i)
        {
            if (min (de[i], in[i]) > ans)
                ans = min (de[i], in[i]);
        }
        printf ("%d\n", ans * 2 - 1);
    }
    return 0;
}

还有超时的DP版

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=10005;
int a[N],c[N],d[N],n;

int dpde(int i)
{
    if(d[i]) return d[i];
    d[i]=1;
    for(int j=i;j<=n;++j)
    {
        if(a[i]>a[j])
            d[i]=max(d[i],dpde(j)+1);
    }
    return d[i];
}

int dpin(int i)
{
    if(c[i]) return c[i];
    c[i]=1;
    for(int j=i;j>=1;--j)
    {
        if(a[i]>a[j])
            c[i]=max(c[i],dpin(j)+1);
    }
    return c[i];
}

int main()
{
    while(scanf("%d",&n)!=EOF)
    {
        for(int i=1;i<=n;++i)
            scanf("%d",&a[i]);
        int ans=1;
        memset(d,0,sizeof(d));
        memset(c,0,sizeof(c));

        for(int i=1;i<=n;++i)
        {
            if(min(dpde(i),dpin(i))>ans)
                ans=min(d[i],c[i]);
        }

        printf("%d\n",ans*2-1);
    }
    return 0;
}

Wavio Sequence 

Wavio is a sequence of integers. It has some interesting properties.

·  Wavio is of odd length i.e. L = 2*n + 1.

·  The first (n+1) integers of Wavio sequence makes a strictly increasing sequence.

·  The last (n+1) integers of Wavio sequence makes a strictly decreasing sequence.

·  No two adjacent integers are same in a Wavio sequence.

For example 1, 2, 3, 4, 5, 4, 3, 2, 0 is an Wavio sequence of length 9. But 1, 2, 3, 4, 5, 4, 3, 2, 2 is not a valid wavio
sequence. In this problem, you will be given a sequence of integers. You have to find out the length of the longest Wavio sequence which is a subsequence of the given sequence. Consider, the given sequence as :

1 2 3 2 1 2 3 4 3 2 1 5 4 1 2 3 2 2 1.

Here the longest Wavio sequence is : 1 2 3 4 5 4 3 2 1. So, the output will be 9.

Input

The input file contains less than 75 test cases. The description of each test case is given below: Input is terminated by end of file.

Each set starts with a postive integer, N(1<=N<=10000). In next few lines there will be N integers.

Output

For each set of input print the length of longest wavio sequence in a line.

Sample Input                                   Output for Sample Input

10
1 2 3 4 5 4 3 2 1 10
19
1 2 3 2 1 2 3 4 3 2 1 5 4 1 2 3 2 2 1
5
1 2 3 4 5
 
9
9
1

 


Problemsetter: Md. Kamruzzaman, Member of Elite Problemsetters‘ Panel

时间: 2024-10-10 01:17:26

UVa 10534 Wavio Sequence ( DP 二分 最长递增子序列 )的相关文章

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

Wavio Sequence 题目: 题目大意: 题目是给一个序列,然后再其序列中找一个子序列,这个子序列符合前一半是递增的序列,后半部分是递减的序列,并且是这个序列中所有符合条件的子序列中最长的,输出其长度. 思路分析: 题目读懂以后,解法就迎刃而出了,很显然,正着求一个最长上升子序列,倒着求一个最长上升子序列.然后从这两个序列中找重合的位置最符合题意的,不过在这道题中,需要标记到每一位的最长上升子序列,因为每一位都可能成为符合题意的子序列的中间那一位置.这就需要用两个数组来标记,一个标记正方

【UVA】10534 - Wavio Sequence(LIS最长上升子序列)

这题一看10000的数据量就知道必须用nlog(n)的时间复杂度. 所以特意去看了最长上升子序列的nlog(n)的算法. 如果有2个位置,该位置上的元素为A[i]和A[j],并且他们满足以下条件: 1.dp[i] = dp[j]    (dp[x]代表以x结尾的最长上升子序列长度) 2.A[i] < A[j] 3.i < j 那么毫无疑问,选择dp[i] 一定优于选择dp[j] 那么我们可以利用g[i]表示长度为i的序列的最后一个元素的最小值. 每次拿到一个A[i],在g[i]里面寻找到一个元

LIS UVA 10534 Wavio Sequence

题目传送门 1 /* 2 LIS:应用,nlogn的做法,首先从前扫到尾,记录每个位置的最长上升子序列,从后扫到头同理. 3 因为是对称的,所以取较小值*2-1再取最大值 4 */ 5 /************************************************ 6 * Author :Running_Time 7 * Created Time :2015-8-5 21:38:32 8 * File Name :UVA_10534.cpp 9 ***************

uva 10534 Wavio Sequence LIS

// uva 10534 Wavio Sequence // // 可以将题目转化为经典的LIS. // 从左往右LIS记作d[i],从右往左LIS记作p[i]; // 则最后其中的min(d[i],p[i])就是这个波动序列的一半 // 在这最后的min(d[i],p[i]) * 2 + 1 的最大值就是我们所要求的答案 // // 这题开始想的最后的答案是d[i]==p[i]的时候求最大. // 但是这样是不对的,例如n=4, // 1,3,1,0 // 最长的应该是3,但是我的答案是1,明

动态规划(DP),最长递增子序列(LIS)

题目链接:http://poj.org/problem?id=2533 解题报告: 状态转移方程: dp[i]表示以a[i]为结尾的LIS长度 状态转移方程: dp[0]=1; dp[i]=max(dp[k])+1,(k<i),(a[k]<a[i]) #include <stdio.h> #define MAX 1005 int a[MAX];///存数据 int dp[MAX];///dp[i]表示以a[i]为结尾的最长递增子序列(LIS)的长度 int main() { int

UVa 10534 Wavio Sequence (最长递增子序列 DP 二分)

Wavio Sequence  Wavio is a sequence of integers. It has some interesting properties. ·  Wavio is of odd length i.e. L = 2*n + 1. ·  The first (n+1) integers of Wavio sequence makes a strictly increasing sequence. ·  The last (n+1) integers of Wavio s

UVA 10534 Wavio Sequence

题解: 根据最长上升子序列的nlog(n)做法,求出以每个点结尾的最长上升子序列. 然后反过来再求一次 然后取最小值 * 2 - 1 即可 代码: #include<bits/stdc++.h> using namespace std; #define pb push_back #define mp make_pair #define se second #define fs first #define lson l,m,rt<<1 #define rson m+1,r,rt<

dp求最长递增子序列并输出

1 import java.util.ArrayList; 2 import java.util.Arrays; 3 import java.util.List; 4 5 /** 6 * Created on 2016/4/26. 7 */ 8 public class Testdp { 9 10 public static void main(String[] args) { 11 new Testdp().getLIS(); 12 } 13 14 public void getLIS() {

LIS(最长递增子序列)和LCS(最长公共子序列)的总结

最长公共子序列(LCS):O(n^2) 两个for循环让两个字符串按位的匹配:i in range(1, len1) j in range(1, len2) s1[i - 1] == s2[j - 1], dp[i][j] = dp[i - 1][j -1] + 1; s1[i - 1] != s2[j - 1], dp[i][j] = max (dp[i - 1][j], dp[i][j - 1]); 初始化:dp[i][0] = dp[0][j] = 0; 伪代码: dp[maxn1][ma