前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >HOJ 2985 Wavio Sequence(最长递增子序列以及其O(n*logn)算法)

HOJ 2985 Wavio Sequence(最长递增子序列以及其O(n*logn)算法)

作者头像
ShenduCC
发布2018-04-26 11:22:35
5610
发布2018-04-26 11:22:35
举报
文章被收录于专栏:算法修养

Wavio Sequence My Tags (Edit) Source : UVA Time limit : 1 sec Memory limit : 32 M Submitted : 296, Accepted : 123 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 multiple 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 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 Sample Output 9 9 1

解法是将数组正着和倒着分别求一下最长递增子序列,然后遍历i,如果i左边的数组和右边的数组的最长子序列相等,就是符合条件的。如果求最长递增子序列用O(N^2)会超时,所以必须用效率高的算法。。关于O(n*logn)算法请参考以下博客 http://blog.csdn.net/dacc123/article/details/50571844 贴上代码

代码语言:javascript
复制
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <algorithm>

using namespace std;
int a[10005];
int b[10005];
int c[10005];
int d[10005];
int dp[10005];
int bp[10005];
int n;
int ans;
int search(int num,int l,int r,int *dp)
{
    int mid;
    while(l<=r)
    {
        mid=(l+r)/2;
        if(num>dp[mid])
            l=mid+1;
        else
            r=mid-1;
    }
    return l;
}
int main()
{
    while(scanf("%d",&n)!=EOF)
    {
        ans=0;
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
            b[n-i+1]=a[i];
        }
        //memset(dp,0,sizeof(dp));
        dp[1]=a[1];
        c[1]=1;
        int len=1;
        for(int i=2;i<=n;i++)
        {
            if(a[i]>dp[len])
                dp[++len]=a[i];
            else
            {
                int pos=search(a[i],1,len,dp);
                dp[pos]=a[i];
            }
            c[i]=len;
        }
        bp[1]=b[1];
        d[1]=1;
        int len2=1;
        for(int i=2;i<=n;i++)
        {
            if(b[i]>bp[len2])
                bp[++len2]=b[i];
            else
            {
                int pos=search(b[i],1,len2,bp);
                bp[pos]=b[i];
            }
            d[i]=len2;
        }
        for(int i=1;i<=n;i++)
        {
            if(c[i]==d[n-i+1])
                ans=max(ans,c[i]*2-1);
        }
        printf("%d\n",ans);

    }
    return 0;
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2016-02-29 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档