[BZOJ2225][SPOJ2371]LIS2 - Another Longest Increasing Subsequence Problem:CDQ分治+树状数组+DP
分析
这回试了一下三级标题,不知道效果怎么样?
回到正题,二维最长上升子序列......嗯,我会树套树。
考虑\(CDQ\)分治,算法流程:
先递归进入左子区间。
将左,右子区间按\(x\)排序。
归并处理左右子区间,在过程中使用树状数组加速\(DP\)。
还原右区间,清空树状数组。
递归进入右子区间。
代码
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cctype>
#include <algorithm>
#define lowbit(x) ((x)&(-(x)))
#define rin(i,a,b) for(int i=(a);i<=(b);i++)
#define rec(i,a,b) for(int i=(a);i>=(b);i--)
#define trav(i,a) for(int i=head[(a)];i;i=e[i].nxt)
using std::cin;
using std::cout;
using std::endl;
typedef long long LL;
inline int read(){
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=(x<<3)+(x<<1)+ch-'0';ch=getchar();}
return x*f;
}
const int MAXN=100005;
int n,b[MAXN];
int siz;
int bit[MAXN];
struct Node{
int x,y,pos,f;
Node(){
f=1;
}
friend bool operator < (Node x,Node y){
return x.x==y.x?x.y<y.y:x.x<y.x;
}
}a[MAXN],c[MAXN];
inline void Add(int x,int kk){
for(int i=x;i<=siz;i+=lowbit(i)) bit[i]=(kk==-1?0:std::max(bit[i],kk));
}
inline int Ask(int x){
int ret=0;
for(int i=x;i;i-=lowbit(i)) ret=std::max(ret,bit[i]);
return ret;
}
inline bool cmp(Node x,Node y){
return x.pos<y.pos;
}
void cdq(int l,int r){
if(l>=r) return;
int mid=((l+r)>>1);
cdq(l,mid);
std::sort(a+l,a+mid+1);
std::sort(a+mid+1,a+r+1);
int lptr=l;
rin(rptr,mid+1,r){
while(lptr<=mid&&a[lptr].x<a[rptr].x){
Add(a[lptr].y,a[lptr].f);
lptr++;
}
a[rptr].f=std::max(a[rptr].f,Ask(a[rptr].y-1)+1);
}
rin(i,l,lptr-1) Add(a[i].y,-1);
rin(i,mid+1,r) c[i]=a[i];
rin(i,mid+1,r) a[c[i].pos]=c[i];
// std::sort(a+mid+1,a+r+1,cmp);
cdq(mid+1,r);
}
int main(){
n=read();
rin(i,1,n){
a[i].x=read();
b[i]=a[i].y=read();
a[i].pos=i;
}
std::sort(b+1,b+n+1);
siz=std::unique(b+1,b+n+1)-b-1;
rin(i,1,n) a[i].y=std::lower_bound(b+1,b+n+1,a[i].y)-b;
cdq(1,n);
int ans=1;
rin(i,1,n) ans=std::max(ans,a[i].f);
printf("%d\n",ans);
return 0;
}
[BZOJ2225][SPOJ2371]LIS2 - Another Longest Increasing Subsequence Problem:CDQ分治+树状数组+DP的更多相关文章
- 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 ...
- SPOJ LIS2 Another Longest Increasing Subsequence Problem 三维偏序最长链 CDQ分治
Another Longest Increasing Subsequence Problem Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://a ...
- 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 /****************** ...
- 【bzoj2225】[Spoj 2371]Another Longest Increasing CDQ分治+树状数组
题目描述 给定N个数对(xi, yi),求最长上升子序列的长度.上升序列定义为{(xi, yi)}满足对i<j有xi<xj且yi<yj. 样例输入 8 1 3 3 2 1 1 4 5 ...
- HDU 5618:Jam's problem again(CDQ分治+树状数组处理三维偏序)
http://acm.hdu.edu.cn/showproblem.php?pid=5618 题意:…… 思路:和NEUOJ那题一样的.重新写了遍理解了一下,算作处理三维偏序的模板了. #includ ...
- SPOJ Another Longest Increasing Subsequence Problem 三维最长链
SPOJ Another Longest Increasing Subsequence Problem 传送门:https://www.spoj.com/problems/LIS2/en/ 题意: 给 ...
- BZOJ_2225_[Spoj 2371]Another Longest Increasing_CDQ 分治+树状数组
BZOJ_2225_[Spoj 2371]Another Longest Increasing_CDQ 分治+树状数组 Description 给定N个数对(xi, yi),求最长上升子 ...
- hdu 3030 Increasing Speed Limits (离散化+树状数组+DP思想)
Increasing Speed Limits Time Limit: 2000/10000 MS (Java/Others) Memory Limit: 32768/32768 K (Java ...
随机推荐
- spring扩展点之PropertyPlaceholderConfigurer
原理机制讲解 https://leokongwq.github.io/2016/12/28/spring-PropertyPlaceholderConfigurer.html 使用时多个配置讲解 ht ...
- 【监控笔记】【1.2】监控事件系列——SQL Trace(默认跟踪与自定义跟踪)
目录: [1.1]概念与使用 [1.2]跟踪的基本操作 --[1.2.1]查看默认跟踪是否在运行 --[1.2.2]开启默认跟踪 --[1.2.3]关闭默认跟踪 --[1.2.4]查看跟踪文件/查看跟 ...
- springboot swagger教程😀
传送门开启:https://www.ibm.com/developerworks/cn/java/j-using-swagger-in-a-spring-boot-project/index.html
- ubuntu 配置 tftp 服务器
一. 安装 tftp 1.1. 安装 tftp 所需的软件. a. 安装 tftp-hpa,tftpd-hpa,前者是客户端,后者是服务程序, 在终端下输入 sudo apt-get install ...
- Structs2下的MyFirstTest
1.这是<Struts2-权威指南>第二章的例子 2.博文主要说明在eclipse下如何创建一个struts2项目 3.实现功能:在login.jsp输入用户名和密码,若用户名为scott ...
- vscode配置golang
https://www.cnblogs.com/Leo_wl/p/8242628.html https://www.cnblogs.com/angelyan/p/10400789.html 主要看了这 ...
- idea无法使用注解@Data解决方法
@Data相关依赖 <dependency> <groupId>org.projectlombok</groupId> <artifactId>lomb ...
- Java的volatile
1.同步 同synchronized相比(synchronized通常称为重量级锁),volatile更轻量级 如图,如果变量没有volatile关键字,那么A线程对该变量的改变存储在内存A,B变量不 ...
- java封装小实例
封装是java语言的一个重要的特性,通过把对象的属性和操作方法封装在同一个类中,对外只提供公共方法对这些数据进行set和get,同时封装也能对方法进行封装.总之封装能够有效地隐藏内部的代码细节,从而使 ...
- 继续死磕python
一.数据运算 算术运算 比较运算 赋值运算 逻辑运算 成员运算 身份运算 位运算 其中左右移运算是逻辑左右移即缺失位补0,而算数右移缺失补符号位(注意逻辑运算都是补码运算即都取补码再运算,然后结果也是 ...