1551: Longest Increasing Subsequence Again

Time Limit: 2 Sec  Memory Limit: 256 MB
Submit: 267  Solved: 112
[Submit][Status][Web Board]

Description

Give
you a numeric sequence. If you can demolish arbitrary amount of
numbers, what is the length of the longest increasing sequence, which is
made up of consecutive numbers? It sounds like
Longest Increasing Subsequence at first sight. So, there is another
limitation: the numbers you deleted must be consecutive.

Input

There are several test cases.
For each test case, the first line of input contains the length of
sequence N(1≤N≤10^4). The second line contains the elements of
sequence——N positive integers not larger than 10^4.

Output

For each the case,
output one integer per line, denoting the length of the longest
increasing sequence of consecutive numbers, which is achievable by
demolishing some(may be zero) consecutive numbers.

Sample Input

  1. 7
  2. 1 7 3 5 9 4 8
  3. 6
  4. 2 3 100 4 4 5

Sample Output

  1. 4
  2. 4
  3.  
  4. 题意:给出一个序列,删除任意一段连续的数(也可以不删除),删完后最长严格递增子段(序列必须是连续的)最长。
    题解:对付这种题的能力不行啊,看了题解做的.
    分析:dp[i][0]代表未删除时以i结尾最长连续上升子序列长度,
    dp[i][1]代表删除时以i结尾最长连续上升子序列长度
    不删除时很好处理,递推过去就好了,所以可以预处理 dp[i][0]
    dp[i][1]可以从dp[i][0]转移过来
    1.dp[i][1] = dp[i-1][1]+1 (a[i]>a[i-1])
    2.dp[i][1] = dp[j][0]+1 (a[i]>a[j]&&i>j) 这里找dp[j][0]可以利用线段树维护.
    这里的更新过程和查询与此题有异曲同工之妙 http://www.cnblogs.com/liyinggang/p/5656485.html
  1. /**分析:dp[i][0]代表未删除时以i结尾最长连续上升子序列长度,
  2. dp[i][1]代表删除时以i结尾最长连续上升子序列长度
  3. 不删除时很好处理,递推过去就好了,所以可以预处理 dp[i][0]
  4. dp[i][1]可以从dp[i][0]转移过来
  5. 1.dp[i][1] = dp[i-1][1]+1 (a[i]>a[i-1])
  6. 2.dp[i][1] = dp[j][0]+1 (a[i]>a[j]&&i>j)
  7. */
  8. #include <iostream>
  9. #include <cstdio>
  10. #include <cstring>
  11. #include <cmath>
  12. #include <algorithm>
  13. #include <vector>
  14. using namespace std;
  15. #define N 10005
  16. int dp[N][],a[N],b[N];
  17. int tree[N<<],MAX;
  18. void pushup(int idx)
  19. {
  20. tree[idx] = max(tree[idx<<],tree[idx<<|]);
  21. }
  22. void build(int l,int r,int idx)
  23. {
  24. if(l==r)
  25. {
  26. tree[idx] = ;
  27. return;
  28. }
  29. int mid = (l+r)>>;
  30. build(l,mid,idx<<);
  31. build(mid+,r,idx<<|);
  32. pushup(idx);
  33. }
  34. void update(int pos,int v,int l,int r,int idx)
  35. {
  36. if(l==r)
  37. {
  38. tree[idx] = max(tree[idx],v);
  39. return;
  40. }
  41. int mid = (l+r)>>;
  42. if(pos<=mid) update(pos,v,l,mid,idx<<);
  43. else update(pos,v,mid+,r,idx<<|);
  44. pushup(idx);
  45. }
  46. void query(int l,int r,int L,int R,int idx)
  47. {
  48. if(l>=L&&R>=r)
  49. {
  50. MAX = max(MAX,tree[idx]);
  51. return;
  52. }
  53. int mid = (l+r)>>;
  54. if(mid>=L) query(l,mid,L,R,idx<<);
  55. if(mid<R) query(mid+,r,L,R,idx<<|);
  56. }
  57.  
  58. int main()
  59. {
  60. int n;
  61. while(scanf("%d",&n)!=EOF)
  62. {
  63. for(int i=; i<=n; i++)
  64. {
  65. scanf("%d",&a[i]);
  66. b[i] = a[i];
  67. }
  68. sort(b+,b++n);
  69. int cnt = unique(b+,b++n)-b;
  70. for(int i=; i<=n; i++) a[i] = lower_bound(b+,b+cnt,a[i]) - b;
  71. for(int i=; i<=n; i++)
  72. {
  73. dp[i][] = ;
  74. if (i > && a[i]>a[i - ])
  75. dp[i][] = dp[i - ][] + ;
  76. }
  77. build(,n,);
  78. int ans = -;
  79. for(int i=; i<=n; i++)
  80. {
  81. dp[i][] = ;
  82. if(i>&&a[i]>)
  83. {
  84. if(a[i]>a[i-]) dp[i][] = dp[i-][]+;
  85. MAX = -;
  86. query(,n,,a[i]-,); /**找到最大的 dp[j][0] 满足 a[j]<a[i] && j<i,有必要解释一下这里
  87. 为什么是更新1~a[i]-1呢?因为我们找的是按照大小排名来的数,而不是,比如说 1 3 5 6 4
  88. 假设我找的是 a[5] ,所以我应该忽略中间的 5 6 ,直接找 1 3 .而我们的枚举顺序也保证了 j<i 这个条件.
  89. 个人觉得很巧妙.
  90. */
  91. dp[i][] = max(dp[i][],MAX+);
  92. }
  93. update(a[i],dp[i][],,n,);/**
  94. 更新过程同样巧妙,也是更新的按照大小排名来的顺序,不要与下标弄混了
  95. */
  96. ans = max(ans,max(dp[i][],dp[i][]));
  97. }
  98. printf("%d\n",ans);
  99. }
  100. return ;
  101. }

csu 1551(线段树+DP)的更多相关文章

  1. Tsinsen A1219. 采矿(陈许旻) (树链剖分,线段树 + DP)

    [题目链接] http://www.tsinsen.com/A1219 [题意] 给定一棵树,a[u][i]代表u结点分配i人的收益,可以随时改变a[u],查询(u,v)代表在u子树的所有节点,在u- ...

  2. HDU 3016 Man Down (线段树+dp)

    HDU 3016 Man Down (线段树+dp) Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Ja ...

  3. lightoj1085 线段树+dp

    //Accepted 7552 KB 844 ms //dp[i]=sum(dp[j])+1 j<i && a[j]<a[i] //可以用线段树求所用小于a[i]的dp[j ...

  4. [CF 474E] Pillars (线段树+dp)

    题目链接:http://codeforces.com/contest/474/problem/F 意思是给你两个数n和d,下面给你n座山的高度. 一个人任意选择一座山作为起始点,向右跳,但是只能跳到高 ...

  5. HDU-3872 Dragon Ball 线段树+DP

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3872 题意:有n个龙珠按顺序放在一列,每个龙珠有一个type和一个权值,要求你把这n个龙珠分成k个段, ...

  6. HDU4521+线段树+dp

    题意:在一个序列中找出最长的某个序列.找出的序列满足题中的条件. 关键:对于 第 i 个位置上的数,要知道与之相隔至少d的位置上的数的大小.可以利用线段树进行统计,查询.更新的时候利用dp的思想. / ...

  7. Codeforces Round #343 (Div. 2) D - Babaei and Birthday Cake 线段树+DP

    题意:做蛋糕,给出N个半径,和高的圆柱,要求后面的体积比前面大的可以堆在前一个的上面,求最大的体积和. 思路:首先离散化蛋糕体积,以蛋糕数量建树建树,每个节点维护最大值,也就是假如节点i放在最上层情况 ...

  8. Special Subsequence(离散化线段树+dp)

    Special Subsequence Time Limit: 5 Seconds      Memory Limit: 32768 KB There a sequence S with n inte ...

  9. hdu 4117 GRE Words (ac自动机 线段树 dp)

    参考:http://blog.csdn.net/no__stop/article/details/12287843 此题利用了ac自动机fail树的性质,fail指针建立为树,表示父节点是孩子节点的后 ...

随机推荐

  1. BZOJ3451 Tyvj1953 Normal 【期望 + 点分治 + NTT】

    题目链接 BZOJ3451 题解 考虑每个点产生的贡献,即为该点在点分树中的深度期望值 由于期望的线性,最后的答案就是每个点贡献之和 对于点对\((i,j)\),考虑\(j\)成为\(i\)祖先的概率 ...

  2. hadoop(三)HDFS基础使用

    一.HDFS前言 1. 设计思想          分而治之:将大文件,大批量文件,分布式的存放于大量服务器上.以便于采取分而治之的方式对海量数据进行运算分析     2. 在大数据系统架构中的应用  ...

  3. POJ--2752

    原题链接:http://poj.org/problem?id=2752 分析:no! #include<cstdio> #include<cstring> #include&l ...

  4. C++ ------ 互斥锁、原子操作的性能测试

    atomic原子操作:是在新标准C++11,引入了原子操作的概念,并通过这个新的头文件提供了多种原子操作数据类型,例如,atomic_bool,atomic_int等等 测试程序 #include & ...

  5. 前端PHP入门-019-内置函数之数学函数-很重要

    查看帮助文档为主 函数名 描述 实例 输入 输出 abs() 求绝对值 $abs = abs(-4.2); //4.2 数字 绝对值数字 ceil() 进一法取整 echo ceil(9.999); ...

  6. CF851 C 暴力

    给出n个5维下的点,求点a不与其它任意的b,c重合,向量ab,ac的夹角都为钝角,这样的点个数,并打印它们. 转换二维下的求角度的函数为五维的,而且由于要求角度大于90度,在二维情况下最多有4个点,也 ...

  7. 使用FormData提交表单及上传文件

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html> <head ...

  8. 使用CSS3+JQuery打造自定义视频播放器

    简介 HTML5的<video>标签已经被目前大多数主流浏览器所支持,包括还未正式发布的IE9也声明将支持<video>标签,利用浏览器原生特性嵌入视频有很多好处,所以很多开发 ...

  9. 【CodeForces】914 H. Ember and Storm's Tree Game 动态规划+排列组合

    [题目]H. Ember and Storm's Tree Game [题意]Zsnuoの博客 [算法]动态规划+排列组合 [题解]题目本身其实并不难,但是大量干扰因素让题目显得很神秘. 参考:Zsn ...

  10. SDUT 3917

    UMR 现在手里有 n 张康纳的表情,最上面一张是玛吉呀巴库乃.现在 UMR 如果每次把最上面的 m 张牌移到最下面而不改变他们的顺序及朝向,那么至少经过多少次移动玛吉呀巴库乃才会又出现在最上面呢? ...