##题面
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. 并查集(Disjoint Set Union,DSU)

    定义: 并查集是一种用来管理元素分组情况的数据结构. 作用: 查询元素a和元素b是否属于同一组 合并元素a和元素b所在的组 优化方法: 1.路径压缩 2.添加高度属性 拓展延伸: 分组并查集 带权并查 ...

  2. vue生命周期-标注

    作者:FlyDragon 出处:http://www.cnblogs.com/fly_dragon/ 关于作者:专注于微软平台项目架构.管理和企业解决方案.如有问题或建议,请多多赐教! 本文版权归作者 ...

  3. 分布式项目中增加品牌前端页面出现Uncaught Error: [$injector:modulerr] bug后的原因以及改正方式

    分布式查询品牌页面时 controller路径正确访问,比如输入 http://localhost:8081/brand/findPage.do?page=3&rows=6 是可以正常显示数据 ...

  4. python3 schedule定时任务

    import time import schedule # cd C:\Python36-32\Scripts pip install schedule # py文件名字不能叫schedule,否则会 ...

  5. Django项目:CRM(客户关系管理系统)--84--74PerfectCRM实现CRM权限和权限组限制访问URL

    #models.py # ————————01PerfectCRM基本配置ADMIN———————— from django.db import models # Create your models ...

  6. KMP算法 (字符串的匹配)

    视频参考 对于正常的字符串模式匹配,主串长度为m,子串为n,时间复杂度会到达O(m*n),而如果用KMP算法,复杂度将会减少线型时间O(m+n). 设主串为ptr="ababaaababaa ...

  7. git回退单个文件

    git原理 Git的版本库里存了很多东西,其中最重要的就是称为stage(或者叫index)的暂存区,还有Git为我们自动创建的第一个分支master,以及指向master的一个指针叫HEAD. gi ...

  8. CSS实现背景图片固定

    body { background-image:url('bg.jpg'); background-repeat: no-repeat; background-attachment: fixed; / ...

  9. axios请求中的参数(params)与路径变量

    1.axios的参数(params) import axios from 'axios' export function getDiscList() { const url = '/api/getDi ...

  10. this和super差异

    <三> this和super差异 1)super(参数):调用基类中的某一个构造函数(应该为构造函数中的第一条语句) 2)this(参数):调用本类中另一种形成的构造函数(应该为构造函数中 ...