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. Oracle中HWM与数据库性能的探讨

    Oracle中HWM与数据库性能的探讨 一.什么是高水位 HWM(high water mark),高水标记,这个概念在segment的存储内容中是比较重要的.简单来说,HWM就是一个segment中 ...

  2. 【JSP】<meta>标签用法

    转载自:http://blog.sina.com.cn/s/blog_65c74cce0102v39z.html  非常感谢这位博主,急着用,改日再细细品味重新整理这篇博文. http-equiv M ...

  3. Storm入门教程 第五章 一致性事务【转】

    Storm是一个分布式的流处理系统,利用anchor和ack机制保证所有tuple都被成功处理.如果tuple出错,则可以被重传,但是如何保证出错的tuple只被处理一次呢?Storm提供了一套事务性 ...

  4. 翻译【ElasticSearch Server】第一章:开始使用ElasticSearch集群(3)

    运行ElasticSearch(Running ElasticSearch) 让我们运行我们的第一个实例.转到bin目录并从命令行运行以下命令: ./elasticsearch –f (Linux o ...

  5. HDU 4630-No Pain No Game(线段树+离线处理)

    题意: 给你n个数的序列a,q个询问,每个询问给l,r,求在下标i在[l,r]的区间任意两个数的最大公约数中的最大值 分析: 有了hdu3333经验,我们从左向右扫序列,如果当前数的约数在前面出现过, ...

  6. HDU 2227-Find the nondecreasing subsequences(dp+BIT优化)

    题意: 给你一个序列a[],求它的不降子序列的个数 分析: dp[i]表示以i结尾不降子序列的个数,dp[i]=sum(dp[j])+1(j<i&&a[j]<=a[i]); ...

  7. CSS选择器的兼容性

    CSS 1 CSS2.1 CSS3 :hover 在IE6中只有a元素可用.E:empty 貌似在webkit核心浏览器中有些小bug.如果这个bug依然存在,不太确定如何测试.IE6不支持.clas ...

  8. cocos2d-x 添加背景音乐和音效-SimpleAudioEngine

    首先,要想使用音效,需要启用音效引擎库CocosDenshion中的SimpleAudioEngine类, #include "SimpleAudioEngine.h" Cocos ...

  9. linux挂载问题解决

    1. 挂载光盘 </pre></p><p><pre name="code" class="plain">[roo ...

  10. Top 5 Free Screen Recording Softwares For Windows

    [转]Top 5 Free Screen Recording Softwares For Windows 该文章是转过来的,因为这里介绍了好几款免费的录制视频的软件.我自己需要使用,也许大家也有需求. ...