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

贴上代码

#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;
}

HOJ 2985 Wavio Sequence(最长递增子序列以及其O(n*logn)算法)的更多相关文章

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

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

  2. UVA10534:Wavio Sequence(最长递增和递减序列 n*logn)(LIS)好题

    题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=68553#problem/B 题目要求: Wavio是一个整数序列,具有以下特性 ...

  3. 2.16 最长递增子序列 LIS

    [本文链接] http://www.cnblogs.com/hellogiser/p/dp-of-LIS.html [分析] 思路一:设序列为A,对序列进行排序后得到B,那么A的最长递增子序列LIS就 ...

  4. Longest Increasing Subsequences(最长递增子序列)的两种DP实现

    一.本文内容 最长递增子序列的两种动态规划算法实现,O(n^2)及O(nlogn).     二.问题描述 最长递增子序列:给定一个序列,从该序列找出最长的 升序/递增 子序列. 特点:1.子序列不要 ...

  5. HDU-1160-FatMouse's Speed(DP, 最长递增子序列)

    链接: https://vjudge.net/problem/HDU-1160 题意: FatMouse believes that the fatter a mouse is, the faster ...

  6. (转载)最长递增子序列 O(NlogN)算法

    原博文:传送门 最长递增子序列(Longest Increasing Subsequence) 下面我们简记为 LIS. 定义d[k]:长度为k的上升子序列的最末元素,若有多个长度为k的上升子序列,则 ...

  7. 最长公共子序列(LCS)和最长递增子序列(LIS)的求解

    一.最长公共子序列 经典的动态规划问题,大概的陈述如下: 给定两个序列a1,a2,a3,a4,a5,a6......和b1,b2,b3,b4,b5,b6.......,要求这样的序列使得c同时是这两个 ...

  8. 最长递增子序列 O(NlogN)算法

    转自:点击打开链接 最长递增子序列,Longest Increasing Subsequence 下面我们简记为 LIS. 排序+LCS算法 以及 DP算法就忽略了,这两个太容易理解了. 假设存在一个 ...

  9. 51nod 1134 最长递增子序列

    题目链接:51nod 1134 最长递增子序列 #include<cstdio> #include<cstring> #include<algorithm> usi ...

随机推荐

  1. SpringBoot------使用Fastjson解析Json数据

    方法一: 1.在pom.xml文件下添加依赖包 <dependency> <groupId>com.alibaba</groupId> <artifactId ...

  2. Java求解汉诺塔问题

    汉诺塔问题的描述如下:有3根柱子A.B和C,在A上从上往下按照从小到大的顺序放着一些圆盘,以B为中介,把盘子全部移动到C上.移动过程中,要求任意盘子的下面要么没有盘子,要么只能有比它大的盘子.编程实现 ...

  3. LeetCode_Maximum Subarray | Maximum Product Subarray

    Maximum Subarray 一.题目描写叙述 就是求一个数组的最大子序列 二.思路及代码 首先我们想到暴力破解 public class Solution { public int maxSub ...

  4. 新版本的body-parser中间件和morgan中间件引用问题:body-parser deprecated bodyParser和morgan deprecated morgan(options)

    引用新版本的body-parser中间件和morgan中间件时,报如下问题: Fri, 09 Jan 2015 06:32:04 GMT morgan deprecated morgan(option ...

  5. Python爬虫学习笔记-2.Requests库

    Requests是Python的一个优雅而简单的HTTP库,它比Pyhton内置的urllib库,更加强大. 0X01 基本使用 安装 Requests,只要在你的终端中运行这个简单命令即可: pip ...

  6. Tomcat漏洞利用与安全加固实例分析

    Tomcat中间件经常遇到的漏洞: 1.Tomcat默认存在一个管理后台,默认的管理地址是http://IP或域名:端口号/manager/html 2.Axis2默认口令安全漏洞,默认的管理地址是h ...

  7. java里面的getAttribute和findAttribute的区别

    findAttribute: abstract Object findAttribute(String name) Searches for the named attribute in page, ...

  8. java多线程例子(生成者和消费者)

    Info.cs 商品 public class Info { boolean flag=false; private String name="张三"; private int a ...

  9. 利用pdb获取未导出符号

      BOOL InitSymHandler(HANDLE hProc)   {   CHAR SymPath[MAX_PATH], CurDir[MAX_PATH];       GetCurrent ...

  10. WP8.1学习系列(第十一章)——中心控件Hub开发指南

    在本文中 先决条件 什么是中心控件? 添加中心控件 将分区添加到中心 添加交互式分区头用于导航 将展示磁贴添加到中心 使用窄应用中的垂直中心 借助中心使用语义式缩放视图 摘要和后续步骤 重要的 API ...