BZOJ 2225: [Spoj 2371]Another Longest Increasing (CDQ分治+dp)
##题面
Description
给定N个数对(xi, yi),求最长上升子序列的长度。上升序列定义为{(xi, yi)}满足对i<j有xi<xj且yi<yj。
Input
Output
Sample Input
8
1 3
3 2
1 1
4 5
6 3
9 9
8 7
7 6
Sample Output
3
HINT
数据范围100000
##解题思路
CDQ分治。其实跟模板题的思路差不多,就是用树状数组维护$dp$最大值。注意一下更新的顺序,就是要从左边往右边更新,所以应该先向左分治,再处理,再向右分治。
##代码
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
const int MAXN = 100005;
inline int rd(){
int x=0,f=1;char ch=getchar();
while(!isdigit(ch)) {f=ch=='-'?0:1;ch=getchar();}
while(isdigit(ch)) {x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}
return f?x:-x;
}
int n,dp[MAXN],f[MAXN],cpy[MAXN],u,ans;
struct Data{
int id,x,y;
friend bool operator<(const Data A,const Data B){
return A.x<B.x;
}
}data[MAXN],tmp[MAXN];
inline void add(int x,int k){
for(;x<=u;x+=x&-x) f[x]=max(f[x],k);
}
inline int query(int x){
int ret=0;
for(;x;x-=x&-x) ret=max(ret,f[x]);
return ret;
}
inline void clear(int x){
for(;x<=u;x+=x&-x) f[x]=-1;
}
inline bool cmp(Data A,Data B){
return A.id<B.id;
}
void cdq(int l,int r){
if(l==r) return ;int mid=(l+r)>>1;cdq(l,mid);
int L=l,R=mid+1,o=l;
sort(data+mid+1,data+r+1);
while(L<=mid && R<=r){
if(data[L].x<data[R].x) add(data[L].y,dp[data[L].id]),L++;
else dp[data[R].id]=max(dp[data[R].id],query(data[R].y-1)+1),R++;
}
while(R<=r) {dp[data[R].id]=max(dp[data[R].id],query(data[R].y-1)+1);R++;}
for(int i=l;i<=mid;i++) clear(data[i].y);sort(data+mid+1,data+r+1,cmp);
cdq(mid+1,r);L=l;R=mid+1;
while(L<=mid && R<=r) {
if(data[L].x<data[R].x) tmp[o++]=data[L++];
else tmp[o++]=data[R++];
}
while(L<=mid) tmp[o++]=data[L++];
while(R<=r) tmp[o++]=data[R++];
for(int i=l;i<=r;i++) data[i]=tmp[i];
}
int main(){
memset(f,-1,sizeof(f));n=rd();
for(int i=1;i<=n;i++) data[i].x=rd(),data[i].y=cpy[i]=rd(),data[i].id=i,dp[i]=1;
sort(cpy+1,cpy+1+n);u=unique(cpy+1,cpy+1+n)-cpy-1;
for(int i=1;i<=n;i++) data[i].y=lower_bound(cpy+1,cpy+1+u,data[i].y)-cpy;cdq(1,n);
for(int i=1;i<=n;i++)
ans=max(ans,dp[i]);
printf("%d\n",ans);
return 0;
}
BZOJ 2225: [Spoj 2371]Another Longest Increasing (CDQ分治+dp)的更多相关文章
- BZOJ 2225 [Spoj 2371]Another Longest Increasing(CDQ分治)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2225 [题目大意] 给定N个数对(xi,yi),求最长上升子序列的长度. 上升序列定义 ...
- bzoj 2225 [Spoj 2371]Another Longest Increasing
这道题 连续上升的三元组 且已经按照第一维排好序了. 直接上CDQ分治即可 当然也是可以2-Dtree解决这个 问题 但是感觉nlog^2 比nsqrt(n)要快一些.. 算是复习一发CDQ分治吧 也 ...
- 【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 ...
- BZOJ2225: [Spoj 2371]Another Longest Increasing CDQ分治,3维LIS
Code: #include <cstdio> #include <algorithm> #include <cstring> #define maxn 20000 ...
- BZOJ_2225_[Spoj 2371]Another Longest Increasing_CDQ 分治+树状数组
BZOJ_2225_[Spoj 2371]Another Longest Increasing_CDQ 分治+树状数组 Description 给定N个数对(xi, yi),求最长上升子 ...
- 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 /****************** ...
- HPU第三次积分赛-D:Longest Increasing Subsequence(DP)
Longest Increasing Subsequence 描述 给出一组长度为n的序列,a1,a2,a3,a4...an, 求出这个序列长度为k的严格递增子序列的个数 输入 第一行输入T ...
随机推荐
- String类型的时间大小比较
不多废话,上代码 (String 的CompareTo方法比较仅仅限于同位数的字符串比较,格式.位数不一样比较结果会错误,原因是CompareTo比较源码是ASCII的比较) 代码一 packag ...
- leetcode-161周赛-1250-检查好数组
题目描述: 唯一的结论是如果数组中所有数的最大公约数为 1,则存在解,否则不存在.所以只需要计算所有数最大公约数即可,时间复杂度O(nlog(m)),其中 m 为数字大小. class Solutio ...
- [NOI2016]区间 题解(决策单调性+线段树优化)
4653: [Noi2016]区间 Time Limit: 60 Sec Memory Limit: 256 MBSubmit: 1593 Solved: 869[Submit][Status][ ...
- (抓)ubuntu下安装mysql --- 我主要参考的文章
转:http://cycnet.blog.51cto.com/117809/812625 现在的软件越来越好安装,尤其是在ubuntu下安装软件,更是没有技巧,只需要在联网的情况下使用apt-get ...
- JAVA的IO流下载音乐
public class DownloadMusic { private static int count = 1; public static void main(String[] args) th ...
- 用 Flask 来写个轻博客 (3) — (M)VC_连接 MySQL 和 SQLAlchemy
目录 目录 前文列表 扩展阅读 前言 Models 模型 SQLAlchemy 安装 SQLAlchemy 安装 Mysql 建立 SQLAlchemy 和 Mysql 的连接 前文列表 用 Flas ...
- linux进阶之路(二):linux文件目录
Linux的目录结构: Linux文件系统采用级层式的目录结构,最上层是根目录"/",在此目录下再创建其他目录. 树状的文件目录.再Linux世界,一切皆文件. /etc 所有系统 ...
- Docker、Kubernetes(k8s)与OpenShift之间的关系
openshift是基于容器计数搭建的一个云平台.这里的容器技术即包括Docker和Kunbernetes.如下图所示,OPenshift底层以Docker作为容器引擎驱动,以Kubernetes作为 ...
- 通过MyEclipse操作数据库,执行sql语句使我们不用切换多个工具,直接工作,方便快捷
通过MyEclipse操作数据库,执行sql语句使我们不用切换多个工具,直接工作,方便快捷.效果如下: 步骤1:通过MyEclipse中的window->show View->ot ...
- js 中typeof 检测数据类型的时候需要注意的小细节
博客搬迁给你带来的不便,敬请谅解! http://www.suanliutudousi.com/2017/10/26/typeof-%E6%A3%80%E6%B5%8B%E6%95%B0%E6%8D% ...