UVA10534:Wavio Sequence(最长递增和递减序列 n*logn)(LIS)好题
题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=68553#problem/B
题目要求:
Wavio是一个整数序列,具有以下特性:
1、Wavio序列的长度是奇数, 即 L = 2 * n + 1;
2、Wavio序列前 n+1 个整数是递增序列
3、Wavio序列后 n+1 个整数是递减序列
如示例 1 2 3 4 5 4 3 2 1 10
最长的 Wavio序列 为 1 2 3 4 5 4 3 2 1 ,所以答案为9
题目解析:
这题做了一中午,第一次做完之后果断TLE了,第一次的做法是对于序列(1,n)暴力求解,先求出a[i]的最长子序列,再求以a[i]为开始的最长递减序列,注意求递增递减
的二分的边界写法。这时候遍历一遍max(min(a[i]的最长,a[i]的最短)*2-1),即为所求结果,不幸直接TLE了。
之后一想,可以先求出这个序列的最长子序列,并记录每一个数最长子序列。
同理,再倒序求出序列的最长子序列,并记录每一个数最长子序列。
这时候在遍历一遍每个数,结果即为max(min(a[i]的最长增长子序列,a[i]的最长递减子序列)*2-1);理由不言而喻。
A了一中午,值得纪念。
AC的:
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <algorithm>
#include <math.h>
using namespace std;
int n,a[],d[],w[],ad[],ad2[],sum,len,l2;
int er(int q[],int l,int r,int key)//好好研究二分
{
int mid;
while(l<=r)
{
mid=(l+r)/;
if(q[mid]==key)
{
return mid;
}
else if(q[mid]>key)
{
r=mid-;
}
else l=mid+;
}
return l;
}
int main()
{
int we;
while(scanf("%d",&n)!=EOF)
{
for(int i=; i<=n; i++)
{
scanf("%d",&a[i]);
}
sum=;
len=;
d[len]=a[];
ad[]=;
for(int i=; i<=n; i++)
{
if(a[i]>d[len])
{
d[++len]=a[i];
ad[i]=len;
}
else
{
we=er(d,,len,a[i]);
d[we]=a[i];
ad[i]=we;
}
}
l2=;
w[l2]=a[n];
ad2[n]=;
for(int i=n-; i>=; i--)
{
if(a[i]>w[l2])
{
w[++l2]=a[i];
ad2[i]=l2;
}
else
{
we=er(w,,l2,a[i]);
w[we]=a[i];
ad2[i]=we;
}
}
for(int i=;i<=n;i++)
{
sum=max(sum,(min(ad[i],ad2[i])*-));
}
printf("%d\n",sum);
}
return ;
}
TLE的:
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <algorithm>
#include <math.h>
using namespace std;
int n,a[],d[],w[],sum,len,l2;
int er(int q[],int l,int r,int key)//好好研究二分
{
int mid;
while(l<=r)
{
mid=(l+r)/;
if(q[mid]==key)
{
return mid;
}
else if(q[mid]>key)
{
r=mid-;
}
else l=mid+;
}
return l;
}
int er2(int q[],int l,int r,int key)//好好研究二分
{
int mid;
while(l<=r)
{
mid=(l+r)/;
if(q[mid]==key)
{
return mid;
}
else if(q[mid]>key)
{
l=mid+;
}
else r=mid-;
}
return l;
}
int main()
{
int we,wei;
while(scanf("%d",&n)!=EOF)
{
for(int i=; i<=n; i++)
{
scanf("%d",&a[i]);
}
sum=;
len=;
d[len]=a[];
for(int i=; i<=n; i++)
{
if(a[i]>d[len])
{
d[++len]=a[i];
l2=;
w[l2]=a[i];
for(int j=i+; j<=n; j++)
{
if(a[j]<w[l2])
{
w[++l2]=a[j];
if(l2==len)
{
break;
}
}
else
{
wei=er2(w,,l2,a[j]);
w[wei]=a[j];
}
}
if(l2<=len) sum=max(sum,(*l2-));
//printf("sum==%d\n",sum);
}
else
{
we=er(d,,len,a[i]);
d[we]=a[i];
if(len<=) continue;
l2=;
w[l2]=a[i];
for(int j=i+; j<=n; j++)
{
if(a[j]<w[l2])
{
w[++l2]=a[j];
if(l2==we)
{
break;
}
}
else
{
wei=er2(w,,l2,a[j]);
w[wei]=a[j];
}
}
if(l2<=we) sum=max(sum,(*l2-));
//printf("sum==%d\n",sum);
}
}
printf("%d\n",sum);
}
return ;
}
UVA10534:Wavio Sequence(最长递增和递减序列 n*logn)(LIS)好题的更多相关文章
- UVa 10534 Wavio Sequence (最长递增子序列 DP 二分)
Wavio Sequence Wavio is a sequence of integers. It has some interesting properties. · Wavio is of ...
- UVa10534 - Wavio Sequence(LIS)
题目大意 给定一个长度为n的整数序列,求个最长子序列(不一定连续),使得该序列的长度为奇数2k+1,前k+1个数严格递增,后k+1个数严格递减.注意,严格递增意味着该序列中的两个相邻数不能相同.n&l ...
- Longest Increasing Subsequences(最长递增子序列)的两种DP实现
一.本文内容 最长递增子序列的两种动态规划算法实现,O(n^2)及O(nlogn). 二.问题描述 最长递增子序列:给定一个序列,从该序列找出最长的 升序/递增 子序列. 特点:1.子序列不要 ...
- Luogu 3402 最长公共子序列(二分,最长递增子序列)
Luogu 3402 最长公共子序列(二分,最长递增子序列) Description 经过长时间的摸索和练习,DJL终于学会了怎么求LCS.Johann感觉DJL孺子可教,就给他布置了一个课后作业: ...
- 一个数组求其最长递增子序列(LIS)
一个数组求其最长递增子序列(LIS) 例如数组{3, 1, 4, 2, 3, 9, 4, 6}的LIS是{1, 2, 3, 4, 6},长度为5,假设数组长度为N,求数组的LIS的长度, 需要一个额外 ...
- 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 ...
- 2.16 最长递增子序列 LIS
[本文链接] http://www.cnblogs.com/hellogiser/p/dp-of-LIS.html [分析] 思路一:设序列为A,对序列进行排序后得到B,那么A的最长递增子序列LIS就 ...
- COGS731 [网络流24题] 最长递增子序列(最大流)
给定正整数序列x1,..., xn (n<=500).(1)计算其最长递增子序列的长度s.(2)计算从给定的序列中最多可取出多少个长度为s的递增子序列.(3)如果允许在取出的序列中多次使用x1和 ...
- POJ 2533 Longest Ordered Subsequence 最长递增序列
Description A numeric sequence of ai is ordered if a1 < a2 < ... < aN. Let the subsequenc ...
随机推荐
- 数据库 proc编程六
#define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <stdlib.h> #include <stri ...
- e2fsprogs 移植
e2fsprogs是用维护ext2,ext3和ext4文件系统的工具程序集.检测和修复文件系统,需要用到其中的fsck, ext2fs等工具, 由于开发板上没有,重新制作文件系统又比较麻烦.所以就需要 ...
- E-R图到数据库表
数据库E-R图相关 日 17:39 E-R数据模型所採用的三个主要概念是:实体集.联系集和属性 实体集:具有同样类型及同样性质(或属性)的实体集合 属性:简单属性和符合属性:单值属性和多值属性:nul ...
- MySQL之explain 的type列 & Extra列
explain 可以分析 select 语句的执行,即 MySQL 的“执行计划. 一.type 列 MySQL 在表里找到所需行的方式.包括(由左至右,由最差到最好): | All | inde ...
- 《Programming with Objective-C》第七章 Values and Collections
1.平台相关的数据类型 These types, like NSInteger and NSUInteger, are defined differently depending on the tar ...
- ios开发之--系统控件显示中文
虽然一直知道X-code肯定提供有语言本地化的设置地方,但是一直也做个记录,有些时候的汉化,还是需要使用代码去控制,键盘的右下角.navagiton的return使用代码修改,调用系统相机时,也是出现 ...
- 你所不知道的CSS
1.用FONT-SIZE:0来清除间距 inline-block的元素之间会受空白区域的影响,也就是元素之间差不多会有一个字符的间隙.如果在同一行内有4个25%相同宽度的元素,会导致最后一个元素掉下来 ...
- iOS实现截屏 并合适保存
本文转载至:http://blog.csdn.net/zeng11088/article/details/8664510 分类: UIImageView2013-03-12 16:42 122人阅读 ...
- python 绘制3D散点图
import scipy.io as sio from mpl_toolkits.mplot3d import Axes3D import matplotlib.pyplot as plt a = [ ...
- css从中挖去一个圆
始终居中: width: 300px; position: fixed; /*在可视区域的上下左右居中*/ top: calc(50vh - 200px); left: calc(50vw - 150 ...