分析

这回试了一下三级标题,不知道效果怎么样?

回到正题,二维最长上升子序列......嗯,我会树套树。

考虑\(CDQ\)分治,算法流程:

  1. 先递归进入左子区间。

  2. 将左,右子区间按\(x\)排序。

  3. 归并处理左右子区间,在过程中使用树状数组加速\(DP\)。

  4. 还原右区间,清空树状数组。

  5. 递归进入右子区间。

代码

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstdlib>
  4. #include <cstring>
  5. #include <cmath>
  6. #include <cctype>
  7. #include <algorithm>
  8. #define lowbit(x) ((x)&(-(x)))
  9. #define rin(i,a,b) for(int i=(a);i<=(b);i++)
  10. #define rec(i,a,b) for(int i=(a);i>=(b);i--)
  11. #define trav(i,a) for(int i=head[(a)];i;i=e[i].nxt)
  12. using std::cin;
  13. using std::cout;
  14. using std::endl;
  15. typedef long long LL;
  16. inline int read(){
  17. int x=0,f=1;char ch=getchar();
  18. while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
  19. while(ch>='0'&&ch<='9'){x=(x<<3)+(x<<1)+ch-'0';ch=getchar();}
  20. return x*f;
  21. }
  22. const int MAXN=100005;
  23. int n,b[MAXN];
  24. int siz;
  25. int bit[MAXN];
  26. struct Node{
  27. int x,y,pos,f;
  28. Node(){
  29. f=1;
  30. }
  31. friend bool operator < (Node x,Node y){
  32. return x.x==y.x?x.y<y.y:x.x<y.x;
  33. }
  34. }a[MAXN],c[MAXN];
  35. inline void Add(int x,int kk){
  36. for(int i=x;i<=siz;i+=lowbit(i)) bit[i]=(kk==-1?0:std::max(bit[i],kk));
  37. }
  38. inline int Ask(int x){
  39. int ret=0;
  40. for(int i=x;i;i-=lowbit(i)) ret=std::max(ret,bit[i]);
  41. return ret;
  42. }
  43. inline bool cmp(Node x,Node y){
  44. return x.pos<y.pos;
  45. }
  46. void cdq(int l,int r){
  47. if(l>=r) return;
  48. int mid=((l+r)>>1);
  49. cdq(l,mid);
  50. std::sort(a+l,a+mid+1);
  51. std::sort(a+mid+1,a+r+1);
  52. int lptr=l;
  53. rin(rptr,mid+1,r){
  54. while(lptr<=mid&&a[lptr].x<a[rptr].x){
  55. Add(a[lptr].y,a[lptr].f);
  56. lptr++;
  57. }
  58. a[rptr].f=std::max(a[rptr].f,Ask(a[rptr].y-1)+1);
  59. }
  60. rin(i,l,lptr-1) Add(a[i].y,-1);
  61. rin(i,mid+1,r) c[i]=a[i];
  62. rin(i,mid+1,r) a[c[i].pos]=c[i];
  63. // std::sort(a+mid+1,a+r+1,cmp);
  64. cdq(mid+1,r);
  65. }
  66. int main(){
  67. n=read();
  68. rin(i,1,n){
  69. a[i].x=read();
  70. b[i]=a[i].y=read();
  71. a[i].pos=i;
  72. }
  73. std::sort(b+1,b+n+1);
  74. siz=std::unique(b+1,b+n+1)-b-1;
  75. rin(i,1,n) a[i].y=std::lower_bound(b+1,b+n+1,a[i].y)-b;
  76. cdq(1,n);
  77. int ans=1;
  78. rin(i,1,n) ans=std::max(ans,a[i].f);
  79. printf("%d\n",ans);
  80. return 0;
  81. }

[BZOJ2225][SPOJ2371]LIS2 - Another Longest Increasing Subsequence Problem:CDQ分治+树状数组+DP的更多相关文章

  1. SPOJ:Another Longest Increasing Subsequence Problem(CDQ分治求三维偏序)

    Given a sequence of N pairs of integers, find the length of the longest increasing subsequence of it ...

  2. SPOJ LIS2 Another Longest Increasing Subsequence Problem 三维偏序最长链 CDQ分治

    Another Longest Increasing Subsequence Problem Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://a ...

  3. SPOJ LIS2 - Another Longest Increasing Subsequence Problem(CDQ分治优化DP)

    题目链接  LIS2 经典的三维偏序问题. 考虑$cdq$分治. 不过这题的顺序应该是 $cdq(l, mid)$ $solve(l, r)$ $cdq(mid+1, r)$ 因为有个$DP$. #i ...

  4. SPOJ - LIS2 Another Longest Increasing Subsequence Problem

    cdq分治,dp(i)表示以i为结尾的最长LIS,那么dp的递推是依赖于左边的. 因此在分治的时候需要利用左边的子问题来递推右边. (345ms? 区间树TLE /****************** ...

  5. 【bzoj2225】[Spoj 2371]Another Longest Increasing CDQ分治+树状数组

    题目描述 给定N个数对(xi, yi),求最长上升子序列的长度.上升序列定义为{(xi, yi)}满足对i<j有xi<xj且yi<yj. 样例输入 8 1 3 3 2 1 1 4 5 ...

  6. HDU 5618:Jam's problem again(CDQ分治+树状数组处理三维偏序)

    http://acm.hdu.edu.cn/showproblem.php?pid=5618 题意:…… 思路:和NEUOJ那题一样的.重新写了遍理解了一下,算作处理三维偏序的模板了. #includ ...

  7. SPOJ Another Longest Increasing Subsequence Problem 三维最长链

    SPOJ Another Longest Increasing Subsequence Problem 传送门:https://www.spoj.com/problems/LIS2/en/ 题意: 给 ...

  8. BZOJ_2225_[Spoj 2371]Another Longest Increasing_CDQ 分治+树状数组

    BZOJ_2225_[Spoj 2371]Another Longest Increasing_CDQ 分治+树状数组 Description        给定N个数对(xi, yi),求最长上升子 ...

  9. hdu 3030 Increasing Speed Limits (离散化+树状数组+DP思想)

    Increasing Speed Limits Time Limit: 2000/10000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java ...

随机推荐

  1. 操作Json对象的C#方法

    json对象长这样 { "UniqueName": { "Required": "true", , , "Regex": ...

  2. 第十四周总结 Io之文件流

    I/O相关 输入/输出 流(数据流动) 数据流动的方向 读数据(输入input) 写数据(输出output) 文件流 字符流 数据流 对象流 网络流.... 1.什么叫文件 一种电脑的存储方式 文件有 ...

  3. 【源码解读】cycleGAN(三):数据读取

    源码地址:https://github.com/aitorzip/PyTorch-CycleGAN 数据的读取是比较简单的,cycleGAN对数据没有pair的需求,不同域的两个数据集分别存放于A,B ...

  4. 剑指offer-序列化和反序列化二叉树-树-python

    题目描述 请实现两个函数,分别用来序列化和反序列化二叉树   二叉树的序列化是指:把一棵二叉树按照某种遍历方式的结果以某种格式保存为字符串,从而使得内存中建立起来的二叉树可以持久保存.序列化可以基于先 ...

  5. JDK集合框架源码分析 - 简单概要

    1.类继承体系 在集合框架的类继承体系中,最顶层有两个接口Collection.Map: Collection 表示一组纯数据 Map 表示一组key-value对 Collection的类继承体系: ...

  6. CentOS7.6中 KVM虚拟机内存、CPU调整

    CentOS7.6中 KVM虚拟机内存.CPU调整 一.调小虚拟机内存 调小虚拟机内存可以动态实现,不用关机 1.查看当前内存大小 [root@heyong kvm]# virsh dominfo t ...

  7. 关于ResultSet中getDate\getTime\getTimestamp的区别的记录

    getDate() 返回时间的年月日 getTime() 返回时间的时分秒 getTimestamp () 返回时间的年月日 时分秒

  8. Windows 实用软件

    Useful tool Listary Ditto Winsnap Quick Look Myper Splash GifCam ScreenToGif Free Download Manage Si ...

  9. VB中preserve的用法

    注:本文转载自:http://zhidao.baidu.com/question/161401549.html ReDim 语句用来定义或重定义原来已经用带空圆括号(没有维数下标)的 Private. ...

  10. 神经网络训练技巧:训练参数初始化、Drop out及Batch Normalization

    参数初始化: xavier初始化: https://blog.csdn.net/VictoriaW/article/details/73000632 条件:优秀的初始化应该使得各层的激活值和梯度的方差 ...