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.
An increasing sequence A1..An is a sequence such that for every i < j, Ai < Aj.
A 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分治求三维偏序)的更多相关文章
- SPOJ Another Longest Increasing Subsequence Problem 三维最长链
SPOJ Another Longest Increasing Subsequence Problem 传送门:https://www.spoj.com/problems/LIS2/en/ 题意: 给 ...
- hdu5618(cdq分治求三维偏序)
题意:给n(n<=100000)个点,坐标为(xi,yi,zi)(1<=xi,yi,zi<=100000),定义一个点A比一个点B小,当且仅当xA<=xB,yA<=yB, ...
- SPOJ LIS2 Another Longest Increasing Subsequence Problem 三维偏序最长链 CDQ分治
Another Longest Increasing Subsequence Problem Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://a ...
- 并不对劲的cdq分治解三维偏序
为了反驳隔壁很对劲的太刀流,并不对劲的片手流决定与之针锋相对,先一步发表cdq分治解三维偏序. 很对劲的太刀流在这里-> 参照一.二维偏序的方法,会发现一位偏序就是直接排序,可以看成通过排序使 ...
- SPOJ LIS2 - Another Longest Increasing Subsequence Problem(CDQ分治优化DP)
题目链接 LIS2 经典的三维偏序问题. 考虑$cdq$分治. 不过这题的顺序应该是 $cdq(l, mid)$ $solve(l, r)$ $cdq(mid+1, r)$ 因为有个$DP$. #i ...
- [BZOJ2225][SPOJ2371]LIS2 - Another Longest Increasing Subsequence Problem:CDQ分治+树状数组+DP
分析 这回试了一下三级标题,不知道效果怎么样? 回到正题,二维最长上升子序列......嗯,我会树套树. 考虑\(CDQ\)分治,算法流程: 先递归进入左子区间. 将左,右子区间按\(x\)排序. 归 ...
- SPOJ - LIS2 Another Longest Increasing Subsequence Problem
cdq分治,dp(i)表示以i为结尾的最长LIS,那么dp的递推是依赖于左边的. 因此在分治的时候需要利用左边的子问题来递推右边. (345ms? 区间树TLE /****************** ...
- cdq分治解决三维偏序
问题背景 在三维坐标系中有n个点,坐标为(xi,yi,zi). 定义一个点A比一个点B小,当且仅当xA<=xB,yA<=yB,zA<=zB.问对于每个点,有多少个点比它小.(n< ...
- 【算法学习】【洛谷】cdq分治 & P3810 三维偏序
cdq是何许人也?请参看这篇:https://wenku.baidu.com/view/3b913556fd0a79563d1e7245.html. 在这篇论文中,cdq提出了对修改/询问型问题(Mo ...
随机推荐
- cocos2d-x-3.x bringToFront & sendToBack实现
void Node::bringToFront(void) { auto parent = this->getParent(); if (parent != nullptr && ...
- python 大小写转换方法(全)
http://blog.csdn.net/liuxincumt/article/details/7945337 python大小写转换(全)
- Cocos2d-x 3.1.1 学习日志5--cocos2d-x3.1.1打飞机的实现
近期学习了cocos2dx3.1.1的一些功能,认为和曾经版本号改的太多了. 所以就做了一个小项目--打飞机来练习练习,在这里我仅仅讲飞机实现的步骤,至于代码.回复5次就可以获得coco2d-x3.1 ...
- mqtt client python example
This is a simple example showing how to use the [Paho MQTT Python client](https://eclipse.org/paho/c ...
- QC3.0快充技术详解
QC3.0 智能手机的电池容量愈来愈大,除了省电能力外,充电速度更成为用户愈来愈重视的特点.高通(Qualcomm)的 Quick Charge 快充技术已成为业界的典范之一,继 Quick Char ...
- 实例讲解SVN分支和合并问题(转)
本节向大家简单描述一下SVN分支和合并方面的知识,在学习SVN的过程中SVN分支和合并时经常遇到的问题,在这里和大家分享一下,希望本文对大家有用. 关于主线同SVN分支合并的概念及如何使用的误区此问题 ...
- 使用Python处理CSV文件的一些代码示例
笔记:使用Python处理CSV文件的一些代码示例,来自于<Python数据分析基础>一书,有删改 # 读写CSV文件,不使用CSV模块,仅使用基础Python # 20181110 wa ...
- kubernetes调度之资源配额
系列目录 当多个用户或者开发团队共享一个有固定节点的的kubernetes集群时,一个团队或者一个用户使用的资源超过他应当使用的资源是需要关注的问题,资源配额是管理员用来解决这个问题的一个工具. 资源 ...
- Install Server Backup Manager on CentOS, RHE, and Fedora
Skip to end of metadata Added by Internal, last edited by Internal on Aug 25, 2014 Go to start of me ...
- Markov Decision Processes
为了实现某篇论文中的算法,得先学习下马尔可夫决策过程~ 1. https://leonardoaraujosantos.gitbooks.io/artificial-inteligence/conte ...