题目 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. Android 敏感权限申请

    动态权限管理是Android6.0(Build.VERSION_CODES.M = Api23)推出的,提醒用户当前APP所需要的权限,防止滥用.这些权限一般分为三种:(1)普通权限:直接manife ...

  2. Windows Docker 使用笔记

    1.设置共享盘 2.设置加速器.国内拉取docker镜像会出现卡顿甚至拉不下来的问题,原因在于大陆沿海的一道墙,在docker设置中添加镜像代理(registry-mirrors)PS:镜像加速器地址 ...

  3. JDK 自带压缩解压流

    代码如下 package com.test.java.zip; import java.io.BufferedInputStream; import java.io.BufferedOutputStr ...

  4. [转]笔记本怎么设置WIfi热点

    https://jingyan.baidu.com/article/335530da4f774019cb41c3eb.html 随着手机的发展,流量的消耗也是大大地增加.虽然很多手机支持wifi,但是 ...

  5. 【转】使用Jasob混淆javascript代码

    在平常的web开发中,我们时常需要写一些js的类库,当我们发布自己产品的时候,不得不把源代码分发出去:但是这样就会泄露自己的代码.今天使用了一下Jasob感觉不错: 使用Jasob,我们的JavaSc ...

  6. 没有IDE的日子

    没有QT Creator,没有VS2008,没有Eclipse,也没有KDevelop,忘掉一切IDE. 好吧,现在我只有Vim了,可我跟Vim不熟. Vim魅力四射,光芒万丈,高高在上,她就是传说中 ...

  7. 【转】通过 INotifyPropertyChanged 实现观察者模式

    通过 INotifyPropertyChanged 实现观察者模式 原博客地址 http://frankdzu.blog.sohu.com/117654536.html 普通观察者模式存在的问题 我们 ...

  8. net core体系-web应用程序-4asp.net core2.0 项目实战(1)-6项目缓冲方案

    Asp.Net Core2.0下使用memcached缓存. Memcached目前微软暂未支持,暂只支持Redis,由于项目历史原因,先用博客园开源项目EnyimMemcachedCore,后续用到 ...

  9. HDU2853 Assignment KM

    原文链接http://www.cnblogs.com/zhouzhendong/p/8284105.html 题目传送门 - HDU2853 题意概括 (来自谷歌翻译) 题解 这是一道好题. 我们首先 ...

  10. beautiful number 数位dp

    题意:  求高位往低位递减且  高位%低位==0(相邻) 数字数量 唯一要注意的就是前导零!!!!!!(正因为这个前导零   一开始的pre设置为0       ) 比如  11  10 09 08 ...