SPOJ LIS2 Another Longest Increasing Subsequence Problem 三维偏序最长链 CDQ分治
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 < 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.
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分治的更多相关文章
- SPOJ LIS2 - Another Longest Increasing Subsequence Problem(CDQ分治优化DP)
题目链接 LIS2 经典的三维偏序问题. 考虑$cdq$分治. 不过这题的顺序应该是 $cdq(l, mid)$ $solve(l, r)$ $cdq(mid+1, r)$ 因为有个$DP$. #i ...
- SPOJ - LIS2 Another Longest Increasing Subsequence Problem
cdq分治,dp(i)表示以i为结尾的最长LIS,那么dp的递推是依赖于左边的. 因此在分治的时候需要利用左边的子问题来递推右边. (345ms? 区间树TLE /****************** ...
- SPOJ Another Longest Increasing Subsequence Problem 三维最长链
SPOJ Another Longest Increasing Subsequence Problem 传送门:https://www.spoj.com/problems/LIS2/en/ 题意: 给 ...
- [BZOJ2225][SPOJ2371]LIS2 - Another Longest Increasing Subsequence Problem:CDQ分治+树状数组+DP
分析 这回试了一下三级标题,不知道效果怎么样? 回到正题,二维最长上升子序列......嗯,我会树套树. 考虑\(CDQ\)分治,算法流程: 先递归进入左子区间. 将左,右子区间按\(x\)排序. 归 ...
- 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 ...
- 洛谷 P3810 【模板】三维偏序(陌上花开) (cdq分治模板)
在solve(L,R)中,需要先分治solve两个子区间,再计算左边区间修改对右边区间询问的贡献. 注意,计算额外的贡献时,两子区间各自内部的顺序变得不再重要(不管怎么样左边区间的都发生在右边之前), ...
- P3810 【模板】三维偏序(陌上花开)(CDQ分治)
题目背景 这是一道模板题 可以使用bitset,CDQ分治,K-DTree等方式解决. 题目描述 有 nn 个元素,第 ii 个元素有 a_iai.b_ibi.c_ici 三个属性,设 f(i) ...
- luogu P3810 三维偏序(陌上花开)cdq分治
题目链接 思路 对一维排序后,使用$cdq$分治,以类似归并排序的方法处理的二维,对于满足$a[i].b \leq a[j].b$的点对,用树状数组维护$a[i].c$的数量.当遇到$a[i].b&g ...
- 三维偏序(陌上花开) CDQ分治
十分巧妙. Code: #include <cstdio> #include <algorithm> #include <cstring> #define setI ...
随机推荐
- TCP/IP详解学习笔记(5)-IP选路,动态选路,和一些细节
1.静态IP选路 1.1.一个简单的路由表 选路是IP层最重要的一个功能之一.前面的部分已经简单的讲过路由器是通过何种规则来根据IP数据包的IP地址来选择路由.这里就不重复了.首先来看看一个简单的系统 ...
- 编译boost (windows msvc14)
我的环境 OS: WIN10 (x64) IDE: VS2015 (VC14) http://www.boost.org/ 1. 下载 下载boost包, boost_1_62_0.7z 使用ASIO ...
- HTTP协议中的长连接和短连接(keep-alive状态)
什么是长连接 HTTP1.1规定了默认保持长连接(HTTP persistent connection ,也有翻译为持久连接),数据传输完成了保持TCP连接不断开(不发RST包.不四次握手),等待在同 ...
- Cake slicing
题意: n*m的方格中有k个点,现在要把方格分开使得每个点在一个部分,每分一次花费边长的费用,求完成花的最小费用 分析: dp[sx][sy][ex][ey]表示分割起点(sx,sy)终点(ex,ey ...
- 《Python 学习手册4th》 第七章 字符串
''' 时间: 9月5日 - 9月30日 要求: 1. 书本内容总结归纳,整理在博客园笔记上传 2. 完成所有课后习题 注:“#” 后加的是备注内容 (每天看42页内容,可以保证月底看完此书) “重点 ...
- 通过简单的Linux内核启动程序代码窥探操作系统的启动原理
作者:吴乐 山东师范大学 <Linux内核分析> 孟宁 MOOC课程http://mooc.study.163.com/course/USTC-1000029000 一.程序设计与分析 ...
- Linux学习笔记:CentOS安装MySQL
[1]安装版本: 1.1 CentOS-7-x86_64-Everything-1503-01 1.2 MySQL-5.6.27-1.linux_glibc2.5.x86_64.rpm-bund ...
- CSS计算样式的获取
一般来说我们获取CSS的样式的时候会优先采用Elment.style.cssName 这种方法,这种方法类似于对象设置get,set属性获取,例如Elment.style.cssName是获取,Elm ...
- Bmob第三方登录详解
Bmob第三方登录详解 Bmob 第三方登录 简介 本文主要介绍新浪微博,QQ,微信的登录接入以及如何配合BmobSDK中的第三方登录功能实现第三方登录. 在使用之前请先按照快速入门创建好可以调用Bm ...
- redis.conf的配置
daemonize yes : redis server 实例是否以后台方式运行 , no:不以后台方式运行(默认) , yes:以后台方式运行. requirepass 密码 : 密码最好长 ...