UVa 10534 Wavio Sequence (最长递增子序列 DP 二分)
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 二分)的更多相关文章
- LIS UVA 10534 Wavio Sequence
题目传送门 题意:找对称的,形如:123454321 子序列的最长长度 分析:LIS的nlogn的做法,首先从前扫到尾,记录每个位置的最长上升子序列,从后扫到头同理.因为是对称的,所以取较小值*2-1 ...
- uva 10534 Wavio Sequence LIS
// uva 10534 Wavio Sequence // // 能够将题目转化为经典的LIS. // 从左往右LIS记作d[i],从右往左LIS记作p[i]; // 则最后当中的min(d[i], ...
- UVA10534:Wavio Sequence(最长递增和递减序列 n*logn)(LIS)好题
题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=68553#problem/B 题目要求: Wavio是一个整数序列,具有以下特性 ...
- UVA 10534 Wavio Sequence
题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=17&p ...
- POJ-2533最长上升子序列(DP+二分)(优化版)
Longest Ordered Subsequence Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 41944 Acc ...
- UVa 10534 Wavio Sequence (LIS+暴力)
题意:给定一个序列,求一个最长子序列,使得序列长度为奇数,并且前一半严格递增,后一半严格递减. 析:先正向和逆向分别求一次LIS,然后再枚举中间的那个数,找得最长的那个序列. 代码如下: #pragm ...
- 最长递增子序列-dp问题
Longest Increasing Subsequence The longest increasing subsequence problem is to find a subsequence o ...
- 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 ...
- uva 103(最长递增子序列) Stacking Boxes
大意是有一些n维的物体,他的边也是n条,如果将一个物体的边按任意顺序排列,只要有一种排列满足一一对应小于另一物体的边,就可以将这个物体嵌套进去另一个物体中,文最多能连续嵌套几个物体. 所求的最多的连续 ...
随机推荐
- tomcat不打印日志
commons-logging.jar导入这个包到tomcat lib下 2.修改tomcat的bin目录下面的catalina.bat文件 只需修改:set CLASSPATH=%CLASSP ...
- CREATE FUNCTION - 定义一个新函数
SYNOPSIS CREATE [ OR REPLACE ] FUNCTION name ( [ argtype [, ...] ] ) RETURNS rettype { LANGUAGE lang ...
- 修改phpadmin中的默认超时时间
登录后1440秒未活动后总是自动退出,一天还要登录多次,终于有时间来解决这个问题了,感觉是session超时,结果在网上search了下,找到解决办法啦,哈哈哈,在此做个笔记: phpmyadmin在 ...
- gifsicle for linux ----------gif 图像处理
1.gifsicle 在linux 中的使用下载gifsicle yum install gifsicle 若发现没有此包 ,更新epel第三方软件库 sudo yum install epel-re ...
- Mysql--查询相关语句总结
一.查询各个部门的最高工资及姓名,其中薪资字段是字符串类型: 优化前: SELECT *FROM (SELECT a.`deptno`, a.`sal`, a.`ename` FROM emp a O ...
- java中mkdir()和mkdirs()区别
mkdirs()可以建立多级文件夹 mkdir()只会建立一级的文件夹 例如: new File("/file/one/two").mkdirs(); 可建立/file/one/t ...
- cacheStorage缓存及离线开发
案例地址:https://zhangxinxu.github.io/https-demo/cache/start.html 我们直接看一个例子吧,如下HTML和JS代码: <h3>一些提示 ...
- nginx网站标准配置
#nginx开启的进程数worker_processes 4; #4核CPU #定义全局错误日志定义类型,[debug|info|notice|warn|crit]error_log ...
- Go:工厂模式
Go的结构体没有构造函数,通常可以使用工厂模式来解决这个问题. 一个结构体的声明是这样的: package model type Student struct { Name string } 因为 S ...
- assert.notDeepStrictEqual()详解
assert.notDeepStrictEqual(actual, expected[, message]) 深度地严格不相等比较测试,与 assert.deepStrictEqual() 相反. c ...