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.

Example

Input:
8
1 3
3 2
1 1
4 5
6 3
9 9
8 7
7 6 Output:
3

题意;求最长的序列LIS,满足i<j,xi<xj ;yi<yj。

思路:裸题,cqd分治,计算每个[L,Mid]区间对[Mid+1,R]区间的贡献。

有两个注意点:

     第一:由于时间紧,只有300ms,不能写结构体的排序; 这里借鉴了别人的一维排序(ORZ,强的啊)。

     第二:注意规避x1=x2,y1<y2的时候不能用 1去更新2。(和求逆序对那题一样,只有把y坐标的放左边即可)。

#include<bits/stdc++.h>
using namespace std;
const int maxn=;
int p[maxn],a[maxn],b[maxn],dp[maxn],Mx[maxn],tot,ans;
bool cmp(int x,int y){ if(a[x]==a[y]) return x>y; return a[x]<a[y]; }
void solve(int L,int R)
{
if(L==R){ dp[L]=max(dp[L],); return ;}
int Mid=(L+R)/;
solve(L,Mid);
for(int i=L;i<=R;i++) p[i]=i;
sort(p+L,p+R+,cmp);
for(int i=L;i<=R;i++){
if(p[i]<=Mid) for(int j=b[p[i]];j<=tot;j+=(-j)&j) Mx[j]=max(Mx[j],dp[p[i]]);
else for(int j=b[p[i]]-;j;j-=(-j)&j) dp[p[i]]=max(dp[p[i]],Mx[j]+);
}
for(int i=L;i<=R;i++)
if(p[i]<=Mid) for(int j=b[p[i]];j<=tot;j+=(-j)&j) Mx[j]=;
solve(Mid+,R);
}
int main()
{
int N,i,fcy=;
scanf("%d",&N);
for(i=;i<=N;i++) scanf("%d%d",&a[i],&b[i]),p[i]=b[i];
sort(p+,p+N+);
tot=unique(p+,p+N+)-(p+);
for(i=;i<=N;i++) b[i]=lower_bound(p+,p+tot+,b[i])-p;
solve(,N);
for(i=;i<=N;i++) fcy=max(fcy,dp[i]);
printf("%d\n",fcy);
return ;
}

SPOJ:Another Longest Increasing Subsequence Problem(CDQ分治求三维偏序)的更多相关文章

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

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

  2. hdu5618(cdq分治求三维偏序)

    题意:给n(n<=100000)个点,坐标为(xi,yi,zi)(1<=xi,yi,zi<=100000),定义一个点A比一个点B小,当且仅当xA<=xB,yA<=yB, ...

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

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

  4. 并不对劲的cdq分治解三维偏序

    为了反驳隔壁很对劲的太刀流,并不对劲的片手流决定与之针锋相对,先一步发表cdq分治解三维偏序. 很对劲的太刀流在这里->  参照一.二维偏序的方法,会发现一位偏序就是直接排序,可以看成通过排序使 ...

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

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

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

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

  7. SPOJ - LIS2 Another Longest Increasing Subsequence Problem

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

  8. cdq分治解决三维偏序

    问题背景 在三维坐标系中有n个点,坐标为(xi,yi,zi). 定义一个点A比一个点B小,当且仅当xA<=xB,yA<=yB,zA<=zB.问对于每个点,有多少个点比它小.(n< ...

  9. 【算法学习】【洛谷】cdq分治 & P3810 三维偏序

    cdq是何许人也?请参看这篇:https://wenku.baidu.com/view/3b913556fd0a79563d1e7245.html. 在这篇论文中,cdq提出了对修改/询问型问题(Mo ...

随机推荐

  1. Windows Server 2008R2 设置SMTP邮件转发服务

    最近因业务需求在Windows Server 2008R2server上设置SMTP转发服务,主要是在业务审批过程中邮件通知相关人员审批情况, 1.在server上加入服务 2.打开服务 3.新建一个 ...

  2. 记一次CDH修改IP

    因机房服务器搬迁,需要修改CDH ip ,集群中有6台服务器. 其中配置了ldap,其中卡在了ldap中的坑太深,所以记录一下. 一.服务器IP等地址修改 1.首先在安装cloudera-manage ...

  3. Android API Guides---Services

    服务 在该文献 基础 声明在清单服务 创建一个启动的服务 扩展IntentService类 扩展服务类 启动服务 停止服务 创建绑定服务 将通知发送给用户 执行在前台服务 管理服务生命周期 实施生命周 ...

  4. mysql 控制台环境下查询中文数据乱码,插入、更新中文数据不成功

    mysql 控制台环境下查询中文数据乱码,插入.更新中文数据不成功         登录mysql密码是加入编码参数--default-character-set,中文用gbk mysql -uroo ...

  5. Fighting regressions with git bisect---within git bisect algorithm

    https://www.kernel.org/pub/software/scm/git/docs/git-bisect-lk2009.html Fighting regressions with gi ...

  6. 传统的Java虚拟机和Android的Dalvik虚拟机及其ART模式

    Java虚拟机的解释执行引擎称为“基于栈的执行引擎”,其中所指的“栈”就是操作数栈.因此我们也称Java虚拟机是基于栈的,这点不同于Android虚拟机,Android虚拟机是基于寄存器的. 基于栈的 ...

  7. Nginx简单了解

    1.静态HTTP服务器 首先,Nginx是一个HTTP服务器,可以将服务器上的静态文件(如HTML.图片)通过HTTP协议展现给客户端. 配置: server { listen80; # 端口号 lo ...

  8. MongoDB的一些操作技巧

    去年三月底入职上海的一家互联网公司,由于项目使用的是MongoDB数据库所以有机会接触了MongoDB.在项目的开发过程中使用系统原有的一些方法查询MongoDB感觉很费力,用起来也不爽,所以私下里就 ...

  9. iOS用户是否打开APP通知开关跳转到系统的设置界面

    1.检测用户是否打开推送通知  /** 系统通知是否打开 @return 是否打开 */ //检测通知是否打开iOS8以后有所变化 所以需要适配iOS7 + (BOOL)openThePushNoti ...

  10. Operation not permitted - /usr/bin/pod

    问题描述:执行sudo gem install cocoapods, 提示出错:While executing gem ... (Errno::EPERM)     Operation not per ...