题目 4990: [Usaco2017 Feb]Why Did the Cow Cross the Road II

链接

http://www.lydsy.com/JudgeOnline/problem.php?id=4990

题面

上下有两个长度为n、位置对应的序列A、B,

其中数的范围均为1~n。若abs(A[i]-B[j]) <= 4,

则A[i]与B[j]间可以连一条边。现要求在边与边不相交的情况下的最大的连边数量。

n <= 10^5。

输入

The first line of input contains N (1≤N≤100,0000).

The next N lines describe the order, by breed ID, of fields on one side of the road;

each breed ID is an integer in the range 1…N

The last N lines describe the order, by breed ID, of the fields on the other side of the road.

Each breed ID appears exactly once in each ordering.

输出

Please output the maximum number of disjoint "friendly crosswalks" Farmer John can draw across the road.

样例输入

6

1

2

3

4

5

6

6

5

4

3

2

1

样例输出

5

题解

考虑二维dp[i][j]表示考虑到<a[i],b[j]>的最大数目

dp[i][j]=max(dp[i-1][j],dp[i][j-1])

如果ai和bj能够连接,dp[i][j]=max(dp[i-1][j-1]+1)

显然第一维可以优化,然后用个线段树求前缀最大值即可。

代码

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e6+7;
int a[maxn],b[maxn],c[maxn],n,dp[maxn];
struct node{int l,r,x;}t[maxn*4];
void build(int x,int l,int r)
{
t[x].l=l,t[x].r=r;
if(l==r){t[x].x=0;return;}
int mid=(l+r)/2;
build(x<<1,l,mid);
build(x<<1|1,mid+1,r);
t[x].x=max(t[x<<1].x,t[x<<1|1].x);
}
void update(int x,int l,int r,int v){
int L=t[x].l,R=t[x].r;
if(l==L&&R==r){
t[x].x=v;
return;
}
int mid=(L+R)/2;
if(mid>=l)update(x<<1,l,r,v);
if(mid<r)update(x<<1|1,l,r,v);
t[x].x=max(t[x<<1].x,t[x<<1|1].x);
}
int query(int x,int l,int r)
{
int L=t[x].l,R=t[x].r;
if(l<=L&&R<=r)return t[x].x;
int ans=0,mid=(L+R)/2;
if(mid>=l)ans=max(ans,query(x<<1,l,r));
if(mid<r)ans=max(ans,query(x<<1|1,l,r));
return ans;
}
int main(){
scanf("%d",&n);
build(1,1,n);
for(int i=1;i<=n;i++){
scanf("%d",&a[i]);
}
for(int i=1;i<=n;i++){
scanf("%d",&b[i]);
}
for(int i=1;i<=n;i++){
c[b[i]]=i;
}
for(int i=1;i<=n;i++){
for(int j=-4;j<=4;j++){
if(a[i]+j>0&&a[i]+j<=n){
dp[c[a[i]+j]]=max(dp[c[a[i]+j]],query(1,1,c[a[i]+j]-1)+1);
}
}
for(int j=-4;j<=4;j++){
if(a[i]+j>0&&a[i]+j<=n){
update(1,c[a[i]+j],c[a[i]+j],dp[c[a[i]+j]]);
}
}
}
cout<<query(1,1,n)<<endl;
}

4990: [Usaco2017 Feb]Why Did the Cow Cross the Road II 线段树维护dp的更多相关文章

  1. [BZOJ4993||4990] [Usaco2017 Feb]Why Did the Cow Cross the Road II(DP + 线段树)

    传送门 f[i][j]表示当前第i个,且最后一个位置连接到j 第一维可以省去,能连边的点可以预处理出来,dp可以用线段树优化 #include <cstdio> #include < ...

  2. BZOJ4990 [Usaco2017 Feb]Why Did the Cow Cross the Road II 动态规划 树状数组

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ4990 题意概括 有上下两行长度为 n 的数字序列 A 和序列 B,都是 1 到 n 的排列,若 a ...

  3. BZOJ4993 [Usaco2017 Feb]Why Did the Cow Cross the Road II 动态规划 树状数组

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ4993 题意概括 有上下两行长度为 n 的数字序列 A 和序列 B,都是 1 到 n 的排列,若 a ...

  4. [BZOJ4990][Usaco2017 Feb]Why Did the Cow Cross the Road II dp

    4990: [Usaco2017 Feb]Why Did the Cow Cross the Road II Time Limit: 10 Sec  Memory Limit: 128 MBSubmi ...

  5. [BZOJ4994] [Usaco2017 Feb]Why Did the Cow Cross the Road III(树状数组)

    传送门 1.每个数的左右位置预处理出来,按照左端点排序,因为左端点是从小到大的,我们只需要知道每条线段包含了多少个前面线段的右端点即可,可以用树状数组 2.如果 ai < bj < bi, ...

  6. [Usaco2017 Feb]Why Did the Cow Cross the Road II (Platinum)

    Description Farmer John is continuing to ponder the issue of cows crossing the road through his farm ...

  7. [Usaco2017 Feb]Why Did the Cow Cross the Road II (Gold)

    Description 上下有两个长度为n.位置对应的序列A.B, 其中数的范围均为1~n.若abs(A[i]-B[j])<= 4,则A[i]与B[j]间可以连一条边. 现要求在边与边不相交的情 ...

  8. [BZOJ4990][Usaco2017 Feb]Why Did the Cow Cross the Road II

    Description Farmer John is continuing to ponder the issue of cows crossing the road through his farm ...

  9. 4989: [Usaco2017 Feb]Why Did the Cow Cross the Road

    题面:4989: [Usaco2017 Feb]Why Did the Cow Cross the Road 连接 http://www.lydsy.com/JudgeOnline/problem.p ...

随机推荐

  1. 远程桌面管理工具Remote Desktop Connection Manager

    使用说明:RDCMan安装好后双击打开RDCMan.exe,首次使用需要添加配置文件扩展名为rdg 1.点击File新建配置文件,这里命名为MRU,存放在安装的根路径下 建好之后,MRU会显示在左侧菜 ...

  2. Git 从 master 分支拉新分支开发

    一. 切换到被copy的分支(master),并且从远端拉取最新版本 $git checkout master $git pull 二.从当前分支拉copy开发分支 $git checkout -b ...

  3. POJ 3713 Transferring Sylla【Tarjan求割点】

    题意:给出一个无向图,判断是否任意两点间都存在至少3条互相独立的路,独立指公共顶点只有起点和终点.算法:枚举每个点,删去后用Tarjan判断图中是否存在割点,如果存在则该图不满足三连通性.Tarjan ...

  4. XNginx - nginx 集群可视化管理工具

    之前团队的nginx管理,都是运维同学每次去修改配置文件,然后重启,非常不方便,一直想找一个可以方便管理nginx集群的工具,翻遍web,未寻到可用之物,于是自己设计开发了一个. 效果预览 集群gro ...

  5. Python_socket

    TCP : 可靠传输,不安全,UDP: 安全传输,不可靠 一台机器上有2^16-1=65535个端口(1-1024)保留自己开就1024往上 socket (套接字):也可以理解为它是一个管道,用于描 ...

  6. sendfile

    Sendfile 函数在两个文件描写叙述符之间直接传递数据(全然在内核中操作,传送),从而避免了内核缓冲区数据和用户缓冲区数据之间的拷贝,操作效率非常高,被称之为零拷贝. Sendfile 函数的定义 ...

  7. Codeforces 555C Case of Chocolate 其他

    原文链接https://www.cnblogs.com/zhouzhendong/p/9272797.html 题目传送门 - CF555C 题意 给定一个 $n\times n(n\leq 10^9 ...

  8. JAVA 关键字及其作用解释

    1. 访问控制 1) private 私有的 private 关键字是访问控制修饰符,可以应用于类.方法或字段(在类中声明的变量). 只能在声明 private(内部)类.方法或字段的类中引用这些类. ...

  9. spring security学习

    https://www.cnblogs.com/leihenqianshang/articles/5313159.html

  10. Linux下C语言进程通讯编程

    代码: #include <stdio.h> #include <stdlib.h> #include <sys/shm.h> /*************基本的函 ...