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. Grunt--Less

    摘要: 之前介绍了自动构建工具Grunt,其中有一个模块是"grunt-contrib-less",下面是配置Grunt自动编译less文件. 安装: Grunt是基于node,功 ...

  2. Eclipse------用Tomcat运行项目后出现:严重: Error configuring application listener of class org.springframework.web.context.ContextLoaderListener

    Eclipse中Tomcat运行项目后出现: 严重: Error configuring application listener of class org.springframework.web.c ...

  3. Tomcat------如何查看80端口是否被占用

    1.Window + R,打开“运行”窗口,输入cmd 2.输入netstat -nao,回车,一般来说80端口会被PID为4的程序占用 3.启动任务管理器,点击“查看”->“选择列” 4.勾选 ...

  4. centos下快速安装JDK

    Linux系统自带了jdk(当然,如果没有,可以忽略这个步骤),但还是1.4的老版本,所以需要先卸载,然后在安装1.6,卸载步骤如下: [root@localhost ~]# rpm -qa | gr ...

  5. vue中使用动态echart图表

    <template> <div class="block"> <div class="title">展会实时人流里统计< ...

  6. [转]JAVA并发编程学习笔记之Unsafe类

    1.通过Unsafe类可以分配内存,可以释放内存:类中提供的3个本地方法allocateMemory.reallocateMemory.freeMemory分别用于分配内存,扩充内存和释放内存,与C语 ...

  7. HTML 格式化

    格式化标签: <!DOCTYPE HTML> <html> <body> <b> This text is bold </b> # < ...

  8. Selenium 延时等待

    在 Selenium 中, get() 方法会在网页框架加载结束后结束执行,此时如果获取 page_source ,可能并不是浏览器完全加载完成的页面: 如果某些页面有额外的 Ajax 请求,我们在网 ...

  9. Kafka一些常见资源汇总

    终于下定决心写一点普及类的东西.很多同学对Kafka的使用很感兴趣.如果你想参与到Kafka的项目开发中,很多资源是你必须要提前准备好的.本文罗列了一些常用的Kafka资源,希望对这些develope ...

  10. MVC项目的简单总结

    不能说是庆幸吧,正赶上公司要开发一个小小的MVC项目,于是这样任务就只有我自己承担,在这个项目中我才彻底感觉到自己之前有关MVC知识的无知,以为接触了皮毛就感觉能做项目,只要有心动脑思考就能完成一个完 ...