Wavio Sequence

Time Limit: 3000ms
Memory Limit: 131072KB

This problem will be judged on UVA. Original ID: 10534
64-bit integer IO format: %lld      Java class name: Main

 

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

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

Sample Output

9
9
1

解题:最长上升子序列加强版。单调队列优化!!!重点。先顺着求最长上升子序列,再逆着求最长上升子序列。
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <climits>
#include <vector>
#include <queue>
#include <cstdlib>
#include <string>
#include <set>
#include <stack>
#define LL long long
#define pii pair<int,int>
#define INF 0x3f3f3f3f
using namespace std;
const int maxn = ;
int dp1[maxn],dp2[maxn],d[maxn],q[maxn];
int bsearch(int lt,int rt,int val) {
while(lt <= rt) {
int mid = (lt+rt)>>;
if(q[mid] < val) lt = mid+;//严格上升单调取少于符号,上升的取少于等于
else rt = mid-;
}
return lt;
}
int main() {
int n,i,j,head,tail;
while(~scanf("%d",&n)) {
for(i = ; i < n; i++)
scanf("%d",d+i);
head = tail = ;
for(i = ; i < n; i++) {
if(head == tail) {
q[head++] = d[i];
dp1[i] = head-tail;
}else if(d[i] > q[head-]){
q[head++] = d[i];
dp1[i] = head-tail;
}else{
int it = bsearch(tail,head-,d[i]);
dp1[i] = it - tail + ;
q[it] = d[i];
}
}
head = tail = ;
for(i = n-; i >= ; i--) {
if(head == tail) {
q[head++] = d[i];
dp2[i] = head-tail;
}else if(d[i] > q[head-]){
q[head++] = d[i];
dp2[i] = head-tail;
}else{
int it = bsearch(tail,head-,d[i]);
dp2[i] = it - tail + ;
q[it] = d[i];
}
}
int ans = ;
for(i = ; i < n; i++){
ans = max(ans,min(dp1[i],dp2[i])*-);
}
printf("%d\n",ans);
}
return ;
}
 

BNUOJ 14381 Wavio Sequence的更多相关文章

  1. UVA 10534 三 Wavio Sequence

    Wavio Sequence Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Sta ...

  2. 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 ...

  3. UVa 10534 Wavio Sequence (最长递增子序列 DP 二分)

    Wavio Sequence  Wavio is a sequence of integers. It has some interesting properties. ·  Wavio is of ...

  4. LIS UVA 10534 Wavio Sequence

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

  5. uva 10534 Wavio Sequence LIS

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

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

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

  7. BNUOJ 1260 Brackets Sequence

    Brackets Sequence Time Limit: 1000ms Memory Limit: 65536KB This problem will be judged on PKU. Origi ...

  8. UVa10534 - Wavio Sequence(LIS)

    题目大意 给定一个长度为n的整数序列,求个最长子序列(不一定连续),使得该序列的长度为奇数2k+1,前k+1个数严格递增,后k+1个数严格递减.注意,严格递增意味着该序列中的两个相邻数不能相同.n&l ...

  9. 1421 - Wavio Sequence

    题目大意:求一个序列中 先严格递增后严格递减的子序列的数目(要求这个子序列对称). 题目思路:正一遍DP,反一遍DP,因为n<=1e5,dp要把时间压缩到nlogn #include<st ...

随机推荐

  1. npm install 安装软件,出现 operation not permitted, mkdir 'C:\Program Files\nodejs\node_cache'

    问题如下图: 解决办法: 在开始菜单栏里打开cmd的时,右击选择“以管理员身份运行”.然后再在打开的cmd里运动install就没问题了. 这个问题应该是当时安装依赖时,我们是以管理员身份运行的:所以 ...

  2. Java实现日期时间对象的使用

    利用类对象计算日期 在利用Java语言进行信息系统开发中,经常需要对日期进行计算和转换,比如,设置了某活动的开始日期和结束日期,系统需要判断当前是否是该活动时间,在Java开发的信息系统中,通常日期以 ...

  3. Mybatis Generator插件升级版

    一.目的: 1. *mapper.java 文件名称 改为*DAO.java2. mapper以及mapper.xml 重复执行,只会覆盖原模板方法,不会覆盖自定义方法3. 实体类添加中文注释 二.步 ...

  4. 基于itchat实现微信群消息同步机器人

    原始网址:http://www.jianshu.com/p/7aeadca0c9bd# 最近 全栈数据工程师养成攻略 的微信群已经将近500人,开了二群之后为了打通不同微信群之间的消息,花了点时间做了 ...

  5. 234 Palindrome Linked List 回文链表

    请检查一个链表是否为回文链表. 进阶:你能在 O(n) 的时间和 O(1) 的额外空间中做到吗? 详见:https://leetcode.com/problems/palindrome-linked- ...

  6. C#手机充值系统开发(基于聚合数据)

    说是手机充值系统有点装了,其实就是调用了聚合数据的支付接口,其实挺简单的事 但是我发现博客园竟然没有类似文章,我就个出头鸟把我的代码贡献出来吧 首先说准备工作: 去聚合数据申请账号-添加手机支付的认证 ...

  7. 关于.Net中Process的使用方法和各种用途汇总(一):Process用法简介

    简介: .Net中Process类功能十分强大.它可以接受程序路径启动程序,接受文件路径使用默认程序打开文件,接受超链接自动使用默认浏览器打开链接,或者打开指定文件夹等等功能. 想要使用Process ...

  8. 10步成为专业iOS开发者——新手向,从零起步

    想在移动开发大餐中分一杯羹,体验最火最炫的技术?小伙子,很有眼光嘛 毫无疑问,移动开发在目前和未来几年内都会盛极一时.无数开发职位虚位以待.各大公司都在寻找各种层次的程序员——新手级.入门级.中级.老 ...

  9. 01-Entity FrameWork如何控制数据的变化

    在Entity Framework所有操作数据就是更新EF容器中的实体状态 public enum EntityState { Added = , Deleted = , Detached = , M ...

  10. Java反射机制实战——字段篇

    首先,我们来认识几个类. Class(java.lang.Class) Class对象是一个特殊对象,每一个类都有一个Class对象,用来创建该类的“常规”对象.可以通过对象的getClass()方法 ...