bzoj 2225 [Spoj 2371]Another Longest Increasing
这道题 连续上升的三元组 且已经按照第一维排好序了。
直接上CDQ分治即可 当然也是可以2-Dtree解决这个 问题 但是感觉nlog^2 比nsqrt(n)要快一些。。
算是复习一发CDQ分治吧 也好久没写了。
原来最长三元上升序列 不是裸的CDQ分治。。我以为是 没细想 最后还是细想了一下实现方式。
首先CDQ左边 然后对于右边此时x是无序的 考虑排序 在外面排序没用好吧。。
好吧可能有用但是太过繁琐那种写法 这里推荐暴力sort。。归并没用 因为归并此时复杂度还是nlogn的。
统计完左边对右边的贡献后 再桶排序复原。再CDQ右边 回来的时候可以进行归并不需要再sort了节省常数。。
复杂度nlog^2。
const int MAXN=100010;
int n,top,ans;
struct wy
{
int x,y;
int id;
inline int friend operator <(wy a,wy b){return a.x==b.x?a.id<b.id:a.x<b.x;}
}t[MAXN],ql[MAXN];
int b[MAXN],f[MAXN],c[MAXN];
inline void discrete()
{
sort(b+1,b+1+n);
rep(1,n,i)if(i==1||b[i]!=b[i-1])b[++top]=b[i];
rep(1,n,i)y(i)=lower_bound(b+1,b+1+top,y(i))-b;
}
inline void add(int x,int y)
{
if(y==-1)
{
while(x<=top)
{
c[x]=0;
x+=x&(-x);
}
return;
}
while(x<=top)
{
c[x]=max(c[x],y);
x+=x&(-x);
}
}
inline int ask(int x)
{
int cnt=0;
while(x)
{
cnt=max(cnt,c[x]);
x-=x&(-x);
}
return cnt;
}
inline void CDQ(int l,int r)
{
if(l==r){++f[id(l)];return;}
int mid=(l+r)>>1;
CDQ(l,mid);
sort(t+mid+1,t+r+1);
int i=l,j=mid+1;
for(int k=l;k<=r+1;++k)
{
if(j>r)
{
for(int w=i-1;w>=l;--w)add(y(w),-1);
break;
}
if((i<=mid)&&x(i)<x(j))add(y(i),f[id(i)]),++i;
else f[id(j)]=max(f[id(j)],ask(y(j)-1)),++j;
}
for(int k=mid+1;k<=r;++k)ql[id(k)]=t[k];
for(int k=mid+1;k<=r;++k)t[k]=ql[k];
CDQ(mid+1,r);
i=l;j=mid+1;
for(int k=l;k<=r;++k)
{
if(i<=mid&&x(i)<x(j)||j>r)ql[k]=t[i],++i;
else ql[k]=t[j],++j;
}
for(int k=l;k<=r;++k)t[k]=ql[k];
}
int main()
{
freopen("1.in","r",stdin);
get(n);
rep(1,n,i)get(x(i)),b[i]=get(y(i)),id(i)=i;
discrete();
CDQ(1,n);
for(int i=1;i<=n;++i)ans=max(ans,f[i]);
printf("%d\n",ans);
return 0;
}
这个树状数组清空的时候注意不要暴力清空 再来一遍序列 清成0即可。
bzoj 2225 [Spoj 2371]Another Longest Increasing的更多相关文章
- 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分治+dp)
题面 Description 给定N个数对(xi, yi),求最长上升子序列的长度.上升序列定义为{(xi, yi)}满足对i<j有xi<xj且yi<yj. Input Output ...
- 【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(i)表示以i为结尾的最长LIS,那么dp的递推是依赖于左边的. 因此在分治的时候需要利用左边的子问题来递推右边. (345ms? 区间树TLE /****************** ...
- SPOJ LIS2 - Another Longest Increasing Subsequence Problem(CDQ分治优化DP)
题目链接 LIS2 经典的三维偏序问题. 考虑$cdq$分治. 不过这题的顺序应该是 $cdq(l, mid)$ $solve(l, r)$ $cdq(mid+1, r)$ 因为有个$DP$. #i ...
- SPOJ Another Longest Increasing Subsequence Problem 三维最长链
SPOJ Another Longest Increasing Subsequence Problem 传送门:https://www.spoj.com/problems/LIS2/en/ 题意: 给 ...
随机推荐
- 「疫期集训day9」七月
我们暂时被击退了,可恶的德国佬----我们集结了英国人,意大利人,荷兰人,澳大利亚人,来自世界各地,我们万众一心,还能失守亚眠?----亚眠中坚守的协约国士兵 日常考爆,T1貌似全场就我傻乎乎的推式子 ...
- CF796C Bank Hacking 题解
洛谷链接 题目 Although Inzane successfully found his beloved bone, Zane, his owner, has yet to return. To ...
- Docker Compose部署 EFK(Elasticsearch + Fluentd + Kibana)收集日志
简述 本文用于记录如何使用Docker Compose部署 EFK(Elasticsearch + Fluentd + Kibana) 收集Docker容器日志,使用EFK,可以无侵入代码,获得灵活, ...
- APP开发---后台设计
想了好久才准备写博客的,希望能对我自己和大家都有帮助! 话不多说,直接正题! ------------------------------------------------------------- ...
- [SpringBoot] 使用yaml文件实现多配置
SpringBoot 使用yaml文件实现多配置 SpringBoot利用yaml文件实现多配置有两种方式: 单个yml中编写多个配置(Multi-profile YAML Documents) 编写 ...
- 【转】Hbuilder配置Avalon、Vue指令提示
转载自CSDN http://blog.csdn.net/jianggujin/article/details/71419828 我本人是一名Java后端开发,偶尔也会研究一下前端内容,因为Hbuil ...
- java 基本语法(四) 流程控制(一) 分支结构
if(条件表达式){ 执行表达式} 结构二:二选一if(条件表达式){ 执行表达式1}else{ 执行表达式2} 结构三:n选一if(条件表达式){ 执行表达式1}else if(条件表达式){ 执行 ...
- JSON基础使用详解
JSON:JavaScript对象表示法 1.1 JSON说明 曾经一段时间,XML是互联网传递数据的统一标准,但是业界一直不乏质疑XML的人士,他们都认为XML过于繁琐,冗长:提取数据也过于麻烦 2 ...
- 使用Red5-Pro Android官方Demo拆解分析(一)
一.配置文件 1.导入库文件jniLibs到main文件夹下 2.导入red5streaming.jar 3.在build里到入其他的包,代码如下: dependencies { implementa ...
- noi linux gedit 配置(c++环境)
基本配置 方法一 查看所有命令: gsettings list-recursively | grep -i gedit 命令解释 gsettings set org.gnome.gedit.prefe ...