##题面
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. python补充4

    一 如何判断一个对象是不是函数类型 #方法一def func(arg): if callable(arg): print("是函数"+arg()) else: print(arg) ...

  2. 对于异步编程Await和Async的理解

    public class AsyncInSync { /// <summary> /// 同步代码里有异步代码 /// /// /// 结果 /// Main Thread Before ...

  3. IntelliJ IDEA下载地址

    http://www.jetbrains.org/display/IJOS/Download

  4. 如何 修改jsp页面时间格式

    先导入文件 <%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %> &l ...

  5. Shiro学习(10)Session管理

    Shiro提供了完整的企业级会话管理功能,不依赖于底层容器(如web容器tomcat),不管JavaSE还是JavaEE环境都可以使用,提供了会话管理.会话事件监听.会话存储/持久化.容器无关的集群. ...

  6. Android中查看当前Activity是否销毁

    进入到Android-sdk中platform-tools目录 在命令行中执行以下命令 adb shell dumpsys activity>activity.txt 可以将当前的四大组件(Ac ...

  7. NX二次开发-NXOpen::Drawings::DrawingSheet Class Reference

    NX11+VS2013 #include <NXOpen/Section.hxx> #include <NXOpen/SectionCollection.hxx> #inclu ...

  8. [7.22NOIP模拟测试7]方程的解 题解(扩展欧几里得)

    Orz 送分比较慷慨的一道题,疯狂特判能拿不少分. 对于$a>0,b>0$的情况: 用exgcd求出方程通解,然后通过操作得到最小正整数解和最大正整数解 他们以及他们之间的解满足等差数列性 ...

  9. 一个类似indexOf()的功能的函数

    之前面试的时候遇到了这样的一道题,不过写的时候有些细节没注意到,现在重新写了一下. 写一个类似indexOf()的功能的函数 var str = "dafdfgvdahjfbhyuyvtur ...

  10. 二分法查找--Python

    二分查找算法,最常规的应用就是在一个有序数组中找特定的数.一般分为四步走: 1. 判定条件为low小于high,low=0, high=size-1 2. mid=(low+high) / 2 3. ...