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. jq 中.html(),.text()和.val()的总结

    html与.text的方法操作是一样,只是在具体针对处理对象不同 html处理的是元素内容,.text处理的是文本内容 html只能使用在HTML文档中,.text 在XML 和 HTML 文档中都能 ...

  2. Swift Intermediate Language (SIL)

    Swift Intermediate Language (SIL) https://github.com/apple/swift/blob/master/docs/SIL.rst#witness-me ...

  3. SQL表变量与临时表区别 + 非游标临时表遍历

    SQL表变量与临时表区别 + 非游标临时表遍历 分类: SQL Server2009-11-27 17:01 1196人阅读 评论(2) 收藏 举报 sqlinsert存储sql servermicr ...

  4. js 页面图片等元素在普通元素中滚动动态加载技术

    /*! * 2012-01-13 v1.1 偏移值计算修改 position → offset * 2012-09-25 v1.2 增加滚动容器参数, 回调参数 * 2015-11-17 v1.3 只 ...

  5. 亲测可用)html5 file调用手机摄像头

    在切图网一个客户的webapp项目中需要用到 html5调用手机摄像头,找了很多资料,大都是 js调用api  然后怎样怎样,做了几个demo测试发现根本不行, 后来恍然大悟,用html5自带的 in ...

  6. swiper实现响应式全屏自动轮播

    html: <!--轮播 --> <div class="Excellent_swi"> <div class="swiper-contai ...

  7. Visual Studio 2013/2015/2017快捷键(转载)

    本文为转载文章,原文:[心存善念]  [Fonour] 项目相关的快捷键 Ctrl + Shift + B = 生成项目 Ctrl + Alt + L = 显示 Solution Explorer(解 ...

  8. python3.x Day6 socketserver

    socketserver是啥? 简化了编写网络服务器,就是对于socket的再一次封装sockerserver包含4个类可以使用:A=socketserver.TCPServer() #用于TCP/I ...

  9. Python之爬虫-猫眼电影

    Python之爬虫-猫眼电影 #!/usr/bin/env python # coding: utf-8 import json import requests import re import ti ...

  10. PowerShell Tools for Visual Studio 2015

    首先要去下载Visual Studio 2015 RC 版本 https://www.visualstudio.com/en-us/downloads/visual-studio-2015-downl ...