Another Longest Increasing Subsequence Problem

Time Limit: 20 Sec

Memory Limit: 256 MB

题目连接

http://acm.hust.edu.cn/vjudge/problem/visitOriginUrl.action?id=19929

Description

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

An increasing sequence A1..An is a sequence such that for every i < jAi < Aj.

subsequence of a sequence is a sequence that appears in the same relative order, but not necessarily contiguous.

A pair of integers (x1, y1) is less than (x2, y2) iff x1 < x2 and y1 < y2.

Input

The first line of input contains an integer N (2 ≤ N ≤ 100000).

The following N lines consist of N pairs of integers (xi, yi) (-109 ≤ xi, yi ≤ 109).

Output

The output contains an integer: the length of the longest increasing subsequence of the given sequence.

Sample Input

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

Sample Output

3

HINT

题意

求三维偏序最长链

题解:

CDQ分治

树套树会TLE(反正我的会TLE。。。。

代码:

#include<iostream>
#include<stdio.h>
#include<map>
#include<vector>
#include<algorithm>
using namespace std;
const int maxn = +;
inline long long read()
{
long long x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
struct node
{
int x,y,z;
}p[maxn];
int n;
map<int,int> H;
vector<int> Q;
void Li()
{
for(int i=;i<=n;i++)
Q.push_back(p[i].z);
sort(Q.begin(),Q.end());
for(int i=;i<=n;i++)
p[i].z=lower_bound(Q.begin(),Q.end(),p[i].z)-Q.begin()+;
}
bool cmpx(node A,node B)
{
return A.x<B.x;
}
bool cmpy(node A,node B)
{
return A.y<B.y;
}
int dp[maxn];
int d[maxn];
int lowbit(int x)
{
return x&(-x);
}
void updata(int x,int val)
{
for(int i=x;i<n+;i+=lowbit(i))
d[i]=max(d[i],val);
}
int query(int x)
{
int res = ;
for(int i=x;i;i-=lowbit(i))
res=max(res,d[i]);
return res;
}
void init(int x)
{
for(int i=x;i<n+;i+=lowbit(i))
d[i]=;
}
void solve(int L,int R){
int m=(L+R)>>;
sort(p+L,p+m+,cmpy);
sort(p+m+,p+R+,cmpy);
int j=L;
for(int i=m+;i<=R;i++){
for(;j<=m&&p[j].y<p[i].y;j++)
updata(p[j].z,dp[p[j].x]);
int tmp=query(p[i].z-)+;
dp[p[i].x]=max(dp[p[i].x],tmp);
}
for(int i=L;i<=m;i++)init(p[i].z);
sort(p+m+,p+R+,cmpx);
} void CDQ(int L,int R){
if(L==R)return;
int m=(L+R)>>;
CDQ(L,m);
solve(L,R);
CDQ(m+,R);
}
int main()
{
scanf("%d",&n);
for(int i=;i<=n;i++)
{
p[i].y=read(),p[i].z=read();
p[i].x = i;
dp[i]=;
}
Li();
CDQ(,n);
int Ans = ;
for(int i=;i<=n;i++)
Ans=max(Ans,dp[i]);
printf("%d\n",Ans);
}

SPOJ LIS2 Another Longest Increasing Subsequence Problem 三维偏序最长链 CDQ分治的更多相关文章

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

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

  2. SPOJ - LIS2 Another Longest Increasing Subsequence Problem

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

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

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

  4. [BZOJ2225][SPOJ2371]LIS2 - Another Longest Increasing Subsequence Problem:CDQ分治+树状数组+DP

    分析 这回试了一下三级标题,不知道效果怎么样? 回到正题,二维最长上升子序列......嗯,我会树套树. 考虑\(CDQ\)分治,算法流程: 先递归进入左子区间. 将左,右子区间按\(x\)排序. 归 ...

  5. 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 ...

  6. 洛谷 P3810 【模板】三维偏序(陌上花开) (cdq分治模板)

    在solve(L,R)中,需要先分治solve两个子区间,再计算左边区间修改对右边区间询问的贡献. 注意,计算额外的贡献时,两子区间各自内部的顺序变得不再重要(不管怎么样左边区间的都发生在右边之前), ...

  7. P3810 【模板】三维偏序(陌上花开)(CDQ分治)

    题目背景 这是一道模板题 可以使用bitset,CDQ分治,K-DTree等方式解决. 题目描述 有 nn 个元素,第 ii 个元素有 a_iai​.b_ibi​.c_ici​ 三个属性,设 f(i) ...

  8. luogu P3810 三维偏序(陌上花开)cdq分治

    题目链接 思路 对一维排序后,使用$cdq$分治,以类似归并排序的方法处理的二维,对于满足$a[i].b \leq a[j].b$的点对,用树状数组维护$a[i].c$的数量.当遇到$a[i].b&g ...

  9. 三维偏序(陌上花开) CDQ分治

    十分巧妙. Code: #include <cstdio> #include <algorithm> #include <cstring> #define setI ...

随机推荐

  1. Android SDK Manager 更新代理配置 ,蛋碎了

    启动 Android SDK Manager ,打开主界面,依次选择「Tools」.「Options...」,弹出『Android SDK Manager - Settings』窗口: 在『Andro ...

  2. MySQL基础之第10章 查询数据

    10.1.基本查询语句 SELECT 属性列表 FROM 表名和视图列表[WHERE条件表达式1][GROUPBY 属性名1 [HAVING条件表达式2]][ORDERBY 属性名2[ASC|DESC ...

  3. 【九度OJ】题目1078-二叉树遍历

    题目 这道题和后面的两道题,题目1201和题目1009,主要内容是对递归的实现,在逻辑上,递归是容易理解的,这种实现方式和我们思考的方式是相吻合的.但是在转换为计算机语言时,要明确告知计算机应该从哪里 ...

  4. bzoj 4006 [JLOI2015]管道连接(斯坦纳树+状压DP)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=4006 [题意] 给定n点m边的图,连接边(u,v)需要花费w,问满足使k个点中同颜色的 ...

  5. 【暑假】[基本数据结构]根据in_order与post_order构树

    Tree Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Des ...

  6. 用原生javascript模拟经典FC游戏公路争霸

    #用原生javascript模拟经典FC游戏公路争霸 前几天看了园子里面的随笔 [原生javascript开发仿微信打飞机小游戏](http://www.cnblogs.com/Mr-Nobody/p ...

  7. 转-CMMI在中国之混乱-CMMI比ISO9000会更惨

    CMMI在中国之混乱-CMMI比ISO9000会更惨 自己接触CMM/CMMI已经有8年时间了,现在静心回顾一下,觉得CMMI在中国的命运会比ISO9000还悲惨. 一组现象或许让你我对此结论有更深入 ...

  8. Codeforces Round #364 (Div.2) C:They Are Everywhere(双指针/尺取法)

    题目链接: http://codeforces.com/contest/701/problem/C 题意: 给出一个长度为n的字符串,要我们找出最小的子字符串包含所有的不同字符. 分析: 1.尺取法, ...

  9. emacs资源

    当clone github时若连接不上,可以使用http代理,形如:export http_proxy=61.172.249.94:80一年成为emacs高手:      https://github ...

  10. 【转】Maven实战(二)---多模块开发---缺少Jar包

    原博文出于:http://blog.csdn.net/liutengteng130/article/details/41611755    感谢! Maven里面的Jar包经常出现Missing的情况 ...