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. [Swift通天遁地]一、超级工具-(16)使用JTAppleCalendar制作美观的日历

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...

  2. ASP.NET 知识点总结(七)

    1.new修饰符是起什么作用new 修饰符用于声明类或类的成员,表示隐藏了基类中同名的成员.而new 操作符用于实例化一个类型new 修饰符只能用于继承类,一般用于弥补基类设计的不足new 修饰符和 ...

  3. UnicodeEncodeError: ‘ascii’ codec can’t encode character u’\u8888′ in position 0: ordinal not in range(168)

    python保存文件UnicodeEncodeError以及reload(sys)后print失效问题 在将字符串写入文件时,执行f.write(str),后台总是报错:UnicodeEncodeEr ...

  4. 贪心+优先队列 HDOJ 5360 Hiking

    题目传送门 /* 题意:求邀请顺序使得去爬山的人最多,每个人有去的条件 贪心+优先队列:首先按照l和r从小到大排序,每一次将当前人数相同的被邀请者入队,那么只要能当前人数比最多人数条件小,该人能 被邀 ...

  5. 【SpringMVC框架】非注解的处理器映射器和适配器

    参考来源:     http://blog.csdn.net/acmman/article/details/46968939 处理器映射器就是根据URL来找Handler,处理器适配器就是按照它要求的 ...

  6. xcode 制作静态库文件(.a)

    参考: http://www.jb51.net/article/37853.htm 摘要: 1. 获取.a文件的信息              lipo -info /Users/pjk1129/De ...

  7. 25 C#类的继承

    继承是面向对象编程的一个重要特性.任何类都可以从另一个类中继承,这就是说,这个类拥有它继承的类的所有成员.在OOP 中,被继承的类称为父类(也称为基类).注意,C#中的对象仅能直接派生于一个基类,当然 ...

  8. Python在云端编程之IPython notebook

    Python在云端编程之IPython notebook 如果本地编程考虑到Python版本,机器位数,编译环境,科学栈安装等等繁琐的事,弄得你焦头烂额,不如移步云端,省去这些繁琐过程,在云端编程是很 ...

  9. HTML form without CSRF protection,HTML表单没有CSRF保护

    HTML form without CSRF protection =HTML表单没有CSRF保护 CSRF是伪造客户端请求的一种攻击,CSRF的英文全称是Cross Site Request For ...

  10. Linux系统调用--getrusage函数详解

    Linux系统调用--getrusage函数详解 功能描述:     获得进程的相关资源信息.如:用户开销时间,系统开销时间,接收的信号量等等;   用法:    #include <sys/t ...