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

题意  求一个序列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;
}

还有TLE的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;
}



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

  1. LIS UVA 10534 Wavio Sequence

    题目传送门 题意:找对称的,形如:123454321 子序列的最长长度 分析:LIS的nlogn的做法,首先从前扫到尾,记录每个位置的最长上升子序列,从后扫到头同理.因为是对称的,所以取较小值*2-1 ...

  2. uva 10534 Wavio Sequence LIS

    // uva 10534 Wavio Sequence // // 能够将题目转化为经典的LIS. // 从左往右LIS记作d[i],从右往左LIS记作p[i]; // 则最后当中的min(d[i], ...

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

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

  4. UVA 10534 Wavio Sequence

    题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=17&p ...

  5. POJ-2533最长上升子序列(DP+二分)(优化版)

    Longest Ordered Subsequence Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 41944   Acc ...

  6. UVa 10534 Wavio Sequence (LIS+暴力)

    题意:给定一个序列,求一个最长子序列,使得序列长度为奇数,并且前一半严格递增,后一半严格递减. 析:先正向和逆向分别求一次LIS,然后再枚举中间的那个数,找得最长的那个序列. 代码如下: #pragm ...

  7. 最长递增子序列-dp问题

    Longest Increasing Subsequence The longest increasing subsequence problem is to find a subsequence o ...

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

    Wavio Sequence My Tags (Edit) Source : UVA Time limit : 1 sec Memory limit : 32 M Submitted : 296, A ...

  9. uva 103(最长递增子序列) Stacking Boxes

    大意是有一些n维的物体,他的边也是n条,如果将一个物体的边按任意顺序排列,只要有一种排列满足一一对应小于另一物体的边,就可以将这个物体嵌套进去另一个物体中,文最多能连续嵌套几个物体. 所求的最多的连续 ...

随机推荐

  1. 【译】x86程序员手册38-10.2实在址模式下的软件初始化

    10.2 Software Initialization for Real-Address Mode   实地址模式的软件初始化 In real-address mode a few structur ...

  2. 洛谷 P1569 [USACO11FEB]属牛的抗议Generic Cow Prote…

    题目描述 Farmer John's N (1 <= N <= 100,000) cows are lined up in a row and numbered 1..N. The cow ...

  3. mysql 常用的语句

    1.查出当前的字段,放到一个字段中:GROUP_CONCAT(distinct b.patent_ip) ,如 (select GROUP_CONCAT(distinct b.patent_ip) f ...

  4. hibernate cascade属性

    cascade属性是存在于set标签中,用来做级联删除和保存. 它的值有以下几种: 1)默认值是none,不做级联动作: 2)save-update:级联保存 3)delete:级联删除 4)all: ...

  5. CSS三栏布局的四种方法

    总括: 不管是三栏布局还是两栏布局都是我们在平时项目里经常使用的,也许你不知道什么事三栏布局什么是两栏布局但实际已经在用,或许你知道三栏布局的一种或两种方法,但实际操作中也只会依赖那某一种方法,本文具 ...

  6. 学习 Qt 编程的好书精品推荐!

    最近一段时间,准备开始搞Qt方面的东西,想找几本书看看.网上介绍QT的书籍也有很多,不想浪费时间,所以想找几本精品的书籍来看.花了半天的时间找了几本非常不错的,这里面整理好之后推荐给大家! 下面介绍的 ...

  7. sphinx配置

    配置文件 ## 数据源src1 source src1 { ## 说明数据源的类型.数据源的类型可以是:mysql,pgsql,mssql,xmlpipe,odbc,python ## 有人会奇怪,p ...

  8. AD转换器的主要指标

    AD转换器的主要指标如下: (1)分辨率(Resolution).指数字量变化一个最小量时模拟信号的变化量,定义为满刻度与2n的比值.分辨率又称精度,通常以数字信号的位数来表示.定义满刻度于2^n的比 ...

  9. LeetCode 188. Best Time to Buy and Sell Stock IV (stock problem)

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  10. 【07】Ajax status和statusText状态对照表

    Ajax status和statusText状态对照表   XMLHttpRequest 对象的 status 和 statusText 属性保存有服务器返回的 http 状态码,不同的是,statu ...