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条,如果将一个物体的边按任意顺序排列,只要有一种排列满足一一对应小于另一物体的边,就可以将这个物体嵌套进去另一个物体中,文最多能连续嵌套几个物体. 所求的最多的连续 ...
随机推荐
- 企业面试之LeetCode刷题心得
谈起刷LeetCode的心得,想要先扯点别的,说实话我是比较自虐的人,大学时候本专业从来不好好上,一直觊觎着别人的专业,因为自己文科生,总觉得没有项技术在身出门找工作都没有底气,然后看什么炫学什么,简 ...
- MFC中使用post提交form-data上传文件
已经有将近6年时间没写过MFC了,想想以前我也是写VC++入门程序开发的,那时候写协议栈.搞语音编码.做视频压缩和实时数据传输,相比现在更多偏业务的开发,那时候搞得都是非常技术的东西.眨眼间,MFC已 ...
- Poi 写入图片进入excel
public static void cacheWritePicture(BufferedImage bufferImg, Sheet sheet, Workbook wb, int width, i ...
- 15数据库与ADO.Net
数据库与ADO.Net 数据库与ADO.Net 8.1 数据库基本概念 数据库提供了一种将信息集合在一起的方法.数据库应用系统主要由三部分组成:数据库管理系统(DBMS),是针对所有应用的,例如A ...
- Mysql读写分离与主从数据库设置方案
Mysql读写分离与主从数据库设置方案 亿仁网 18-10-0711:31 Mysql无非四个功能:增,删,改,读.而将增删改和读分离操作.这样有利于提高系统性能.下面是非常直观的操作: 1.配置: ...
- Javascript创建对象几种方法解析
Javascript创建对象几种方法解析 Javascript面向对象编程一直是面试中的重点,将自己的理解整理如下,主要参考<Javascript高级程序设计 第三版>,欢迎批评指正. 通 ...
- Python飞机大战实例有感——pygame如何实现“切歌”以及多曲重奏?
目录 pygame如何实现"切歌"以及多曲重奏? 一.pygame实现切歌 初始化路径 尝试一 尝试二 尝试三 成功 总结 二.如何在python多线程顺序执行的情况下实现音乐和音 ...
- Tomcat:Error parsing HTTP request header Note: further occurrences of HTTP header parsing errors will be logged at DEBUG level.
可能原因一: 在本地tomcat启动正常并且访问正常的项目放在服务器上tomcat报以上错误. 本地tomcat为7.0.68,服务器上为7.0.86 错误原因:服务器tomcat版本过高. 解决办法 ...
- Python 软件开发目录规范
目录规范: ATM #工程文件夹 ------| bin #用来存放可执行文件的 |---- start.py conf #用来存放配置信息的 |---- settings.py lib ...
- centos6 文件管理
一.文件属性 权限位: - 表示文件 d 表示目录 l 表示软连接 b 表示接口存储设备文件 c 表示串行端口设备 文件的时间属性 [root@web02 ~]# ll /etc/passwd ### ...