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 ;
}
 

最新文章

  1. android Animation介绍
  2. Coursera Machine Learning : Regression 评估性能
  3. DuiLib学习笔记2——写一个简单的程序
  4. 2016年12月18日 星期日 --出埃及记 Exodus 21:13
  5. sort如何按指定的列排序
  6. vs2013 上传碰到的问题:“输入的不是有效的 Base-64 字符串 ”
  7. 【模拟】Codeforces 691C Exponential notation
  8. Swift 2.0 封装图片折叠效果
  9. 积累的VC编程小技巧之工具条和状态条
  10. 记一次自己在Linux上倒腾Nginx的经历
  11. HttpClient基本使用
  12. for循环中let与var的区别,块级作用域如何产生与迭代中变量i如何记忆上一步的猜想
  13. Unittest + python
  14. [洛谷 P1559] 运动员最佳匹配问题
  15. Linux后台开发工具箱
  16. Mysql查看连接数相关信息
  17. gravatar全球通用头像设定
  18. kafka学习之-配置详解
  19. MySQL Warning: Using a password on the command line interface can be insecure.解决办法
  20. Openstack Havana的两个排错过程

热门文章

  1. kernel信息及其相关命令
  2. E20171106-hm
  3. oracle创建默认表空间---重要
  4. 清北考前刷题day6下午好
  5. vbnet 进程监控,监控Acad开启的数量,并且添加到开机启动
  6. Joseph UVA 1452 Jump
  7. fiddler不同代理模式的区别
  8. sql server 行转列 要注意的问题 pivot
  9. JS获取到时间转换成字符串类型
  10. Fiddler抓取Intellij Idea中执行的web网络请求