##题面
Description
给定N个数对(xi, yi),求最长上升子序列的长度。上升序列定义为{(xi, yi)}满足对i<j有xi<xj且yi<yj。

Input
Output
Sample Input
8

1 3

3 2

1 1

4 5

6 3

9 9

8 7

7 6
Sample Output
3
HINT

数据范围100000

##解题思路
  CDQ分治。其实跟模板题的思路差不多,就是用树状数组维护$dp$最大值。注意一下更新的顺序,就是要从左边往右边更新,所以应该先向左分治,再处理,再向右分治。

##代码

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm> using namespace std;
const int MAXN = 100005; inline int rd(){
int x=0,f=1;char ch=getchar();
while(!isdigit(ch)) {f=ch=='-'?0:1;ch=getchar();}
while(isdigit(ch)) {x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}
return f?x:-x;
} int n,dp[MAXN],f[MAXN],cpy[MAXN],u,ans;
struct Data{
int id,x,y;
friend bool operator<(const Data A,const Data B){
return A.x<B.x;
}
}data[MAXN],tmp[MAXN]; inline void add(int x,int k){
for(;x<=u;x+=x&-x) f[x]=max(f[x],k);
} inline int query(int x){
int ret=0;
for(;x;x-=x&-x) ret=max(ret,f[x]);
return ret;
} inline void clear(int x){
for(;x<=u;x+=x&-x) f[x]=-1;
} inline bool cmp(Data A,Data B){
return A.id<B.id;
} void cdq(int l,int r){
if(l==r) return ;int mid=(l+r)>>1;cdq(l,mid);
int L=l,R=mid+1,o=l;
sort(data+mid+1,data+r+1);
while(L<=mid && R<=r){
if(data[L].x<data[R].x) add(data[L].y,dp[data[L].id]),L++;
else dp[data[R].id]=max(dp[data[R].id],query(data[R].y-1)+1),R++;
}
while(R<=r) {dp[data[R].id]=max(dp[data[R].id],query(data[R].y-1)+1);R++;}
for(int i=l;i<=mid;i++) clear(data[i].y);sort(data+mid+1,data+r+1,cmp);
cdq(mid+1,r);L=l;R=mid+1;
while(L<=mid && R<=r) {
if(data[L].x<data[R].x) tmp[o++]=data[L++];
else tmp[o++]=data[R++];
}
while(L<=mid) tmp[o++]=data[L++];
while(R<=r) tmp[o++]=data[R++];
for(int i=l;i<=r;i++) data[i]=tmp[i];
} int main(){
memset(f,-1,sizeof(f));n=rd();
for(int i=1;i<=n;i++) data[i].x=rd(),data[i].y=cpy[i]=rd(),data[i].id=i,dp[i]=1;
sort(cpy+1,cpy+1+n);u=unique(cpy+1,cpy+1+n)-cpy-1;
for(int i=1;i<=n;i++) data[i].y=lower_bound(cpy+1,cpy+1+u,data[i].y)-cpy;cdq(1,n);
for(int i=1;i<=n;i++)
ans=max(ans,dp[i]);
printf("%d\n",ans);
return 0;
}

BZOJ 2225: [Spoj 2371]Another Longest Increasing (CDQ分治+dp)的更多相关文章

  1. BZOJ 2225 [Spoj 2371]Another Longest Increasing(CDQ分治)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2225 [题目大意] 给定N个数对(xi,yi),求最长上升子序列的长度. 上升序列定义 ...

  2. bzoj 2225 [Spoj 2371]Another Longest Increasing

    这道题 连续上升的三元组 且已经按照第一维排好序了. 直接上CDQ分治即可 当然也是可以2-Dtree解决这个 问题 但是感觉nlog^2 比nsqrt(n)要快一些.. 算是复习一发CDQ分治吧 也 ...

  3. 【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 ...

  4. BZOJ2225: [Spoj 2371]Another Longest Increasing CDQ分治,3维LIS

    Code: #include <cstdio> #include <algorithm> #include <cstring> #define maxn 20000 ...

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

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

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

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

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

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

  8. SPOJ - LIS2 Another Longest Increasing Subsequence Problem

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

  9. HPU第三次积分赛-D:Longest Increasing Subsequence(DP)

    Longest Increasing Subsequence 描述 给出一组长度为n的序列,a1​,a2​,a3​,a4​...an​, 求出这个序列长度为k的严格递增子序列的个数 输入 第一行输入T ...

随机推荐

  1. 修改Centos中的ll命令(以 K 为单位显示文件大小)

    修改CentOS ll命令:以K 为单位显示文件大小 1.编辑 .bashrc 文件:vim /root/.bashrc 2.找到 alias ll 行修改为(如果没有直接添加该行):alias ll ...

  2. c# 转16进制

    1.byte[] 转换16进制字符串 1.1 BitConverter方式 var str = DateTime.Now.ToString(); var encode = Encoding.UTF8; ...

  3. Android中父View和子view的点击事件的执行过程

    Android中的事件类型分为按键事件和屏幕触摸事件,Touch事件是屏幕触摸事件的基础事件,有必要对它进行深入的了解.  一个最简单的屏幕触摸动作触发了一系列Touch事件:ACTION_DOWN- ...

  4. BZOJ 2655: calc(拉格朗日插值)

    传送门 解题思路 首先比较容易能想到\(dp\),设\(f[i][j]\)表示前\(j\)个数,每个数\(<=i\)的答案,那么有转移方程:\(f[i][j]=f[i-1][j-1]*i*j+f ...

  5. Ext——xtype各组件类型

    Ext.form.TextField的 xtype类型

  6. Springboot开篇

    1.Spring -boot-starter-web:用于构建web 应用模块,加入后包含spring mvc框架,默认内嵌tomcat容器 2.spring-boot-starter-jpa:用于构 ...

  7. 用 Flask 来写个轻博客 (1) — 创建项目

    目录 目录 前言 扩展阅读 部署开发环境 创建 Github 项目 前言 一步一步的实现一个 Flask 轻博客项目启动,最新的代码会上传到 Github. 扩展阅读 欢迎使用 Flask - vir ...

  8. POJ 1673 EXOCENTER OF A TRIANGLE(解三角形重心)

    题目链接:http://poj.org/problem?id=1673 AC代码: #include<cstdio> #include<cmath> #include<a ...

  9. 剑指offer——60二叉树的深度

    题目描述 输入一棵二叉树,求该树的深度.从根结点到叶结点依次经过的结点(含根.叶结点)形成树的一条路径,最长路径的长度为树的深度.   题解: 简单的深度遍历即可.   class Solution ...

  10. pycharm中django同步数据库问题

    一.Django数据同步过程中遇到的问题: 1.raise ImproperlyConfigured('mysqlclient 1.3.13 or newer is required; you hav ...