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

  1. 10
  1. 1 2 3 4 5 4 3 2 1 10
  1. 19
  1. 1 2 3 2 1 2 3 4 3 2 1 5 4 1 2 3 2 2 1
  1. 5
  1. 1 2 3 4 5
  2.  

Sample Output

  1. 9
  1. 9
  1. 1

  1. 解题:最长上升子序列加强版。单调队列优化!!!重点。先顺着求最长上升子序列,再逆着求最长上升子序列。
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <cmath>
  5. #include <algorithm>
  6. #include <climits>
  7. #include <vector>
  8. #include <queue>
  9. #include <cstdlib>
  10. #include <string>
  11. #include <set>
  12. #include <stack>
  13. #define LL long long
  14. #define pii pair<int,int>
  15. #define INF 0x3f3f3f3f
  16. using namespace std;
  17. const int maxn = ;
  18. int dp1[maxn],dp2[maxn],d[maxn],q[maxn];
  19. int bsearch(int lt,int rt,int val) {
  20. while(lt <= rt) {
  21. int mid = (lt+rt)>>;
  22. if(q[mid] < val) lt = mid+;//严格上升单调取少于符号,上升的取少于等于
  23. else rt = mid-;
  24. }
  25. return lt;
  26. }
  27. int main() {
  28. int n,i,j,head,tail;
  29. while(~scanf("%d",&n)) {
  30. for(i = ; i < n; i++)
  31. scanf("%d",d+i);
  32. head = tail = ;
  33. for(i = ; i < n; i++) {
  34. if(head == tail) {
  35. q[head++] = d[i];
  36. dp1[i] = head-tail;
  37. }else if(d[i] > q[head-]){
  38. q[head++] = d[i];
  39. dp1[i] = head-tail;
  40. }else{
  41. int it = bsearch(tail,head-,d[i]);
  42. dp1[i] = it - tail + ;
  43. q[it] = d[i];
  44. }
  45. }
  46. head = tail = ;
  47. for(i = n-; i >= ; i--) {
  48. if(head == tail) {
  49. q[head++] = d[i];
  50. dp2[i] = head-tail;
  51. }else if(d[i] > q[head-]){
  52. q[head++] = d[i];
  53. dp2[i] = head-tail;
  54. }else{
  55. int it = bsearch(tail,head-,d[i]);
  56. dp2[i] = it - tail + ;
  57. q[it] = d[i];
  58. }
  59. }
  60. int ans = ;
  61. for(i = ; i < n; i++){
  62. ans = max(ans,min(dp1[i],dp2[i])*-);
  63. }
  64. printf("%d\n",ans);
  65. }
  66. return ;
  67. }
  1.  

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. centos7的systemd

    系统启动流程 POST --> Boot Sequence --> Bootloader --> kernel+initramfs(initrd) --> rootfs --& ...

  2. Linux 进程间通讯方式 pipe()函数 (转载)

    转自:http://blog.csdn.net/ta893115871/article/details/7478779 Linux 进程间通讯方式有以下几种: 1->管道(pipe)和有名管道( ...

  3. 在sql语句中使用关键字

    背景 开发过程中遇到了遇到了一句sql语句一直报错,看了一下字段名和表名都对应上了,但是还是一直报错 sql语句如下: update table set using = ""hh ...

  4. 第四代增强 源代码增强(ABAP Source Code Enhancements)

    显式代码增强的创建 se38打开你要增强的程序 进入编辑状态 在菜单栏选择: Edit->Enhancement Opreations->Create option. 此时弹出Create ...

  5. ssm lodop打印图片不显示

    在打印预览的时候图片就是不显示 最终解决方案就是修改过滤器

  6. MVC、MVP和MVVM的图示

    一.MVC MVC模式的意思是,软件可以分成三个部分. 视图(View):用户界面. 控制器(Controller):业务逻辑 模型(Model):数据保存 各部分之间的通信方式如下. View 传送 ...

  7. 莫队算法/二分查找 FZU 2072 Count

    题目传送门 题意:问区间内x的出现的次数分析:莫队算法:用一个cnt记录x的次数就可以了.还有二分查找的方法 代码: #include <cstdio> #include <algo ...

  8. 模拟 URAL 1149 Sinus Dances

    题目传送门 /* 模拟:找到规律分别输出就可以了,简单但是蛮有意思的 */ #include <cstdio> #include <algorithm> #include &l ...

  9. python使用mysql connection获取数据感知不到数据变化问题

    在做数据同步校验的时候,需要从mysql fetch数据和hbase的数据进行对比,发现即使mysql数据变化了,类似下面的代码返回的值还是之前的数据.抽取的代码大概如下: import MySQL ...

  10. Scala-基础-运算符

    import junit.framework.TestCase /** * 运算符 */ class Demo3 extends TestCase { def test_+ { var x = 10; ...