BZOJ4990 [Usaco2017 Feb]Why Did the Cow Cross the Road II 动态规划 树状数组
欢迎访问~原文出处——博客园-zhouzhendong
去博客园看该题解
题目传送门 - BZOJ4990
题意概括
有上下两行长度为 n 的数字序列 A 和序列 B,都是 1 到 n 的排列,若 abs(A[i]-B[j])<=4,则 A[i]和 B[j]间可以连一条边。现求在边与边不相交的情况下的最大连边数量。
题解
我们用dp[i][j]表示枚举到A序列的第i个位置,与B序列的第j个位置匹配,所得到的最大效益,这样显然是要超时的,但是不妨去思考一下。
dp[i][j]=max(dp[i-1][k](1<=k<=j))
于是我们又发现两个厉害的东西:
1. 由于每一个数字连出的边最多只有9种情况( abs(A[i]-B[j])<=4),所以转移的复杂度几乎舍去。
2. 我们发现其实这个东西可以用线段树来维护最大值(当前树状数组也可以的),那么时间复杂度就降成O(n*9 log n)的了。但是线段树的常数太大,被卡了,所以我们用树状数组就可以了。
代码
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cstdlib>
#include <cmath>
using namespace std;
void read(int &x){
x=0;
char ch=getchar();
while (!('0'<=ch&&ch<='9'))
ch=getchar();
while ('0'<=ch&&ch<='9'){
x=x*10+ch-48;
ch=getchar();
}
}
const int N=1e5+5;
int n,a[N],b[N],pos[N],ps[10];
int c[N];
int lb(int x){
return x&-x;
}
void update(int x,int d){
for (;x<=n;x+=lb(x))
c[x]=max(c[x],d);
}
int query(int x){
int ans=0;
for (;x>0;x-=lb(x))
ans=max(ans,c[x]);
return ans;
}
int main(){
read(n);
for (int i=1;i<=n;i++)
read(a[i]);
for (int i=1;i<=n;i++)
read(b[i]),pos[b[i]]=i;
memset(c,0,sizeof c);
for (int i=1;i<=n;i++){
int tot=0;
for (int j=a[i]-4;j<=a[i]+4;j++)
if (1<=j&&j<=n)
ps[++tot]=pos[j];
sort(ps+1,ps+tot+1);
for (int j=tot;j>=1;j--)
update(ps[j],query(ps[j]-1)+1);
}
printf("%d",query(n));
return 0;
}
BZOJ4990 [Usaco2017 Feb]Why Did the Cow Cross the Road II 动态规划 树状数组的更多相关文章
- BZOJ4993 [Usaco2017 Feb]Why Did the Cow Cross the Road II 动态规划 树状数组
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ4993 题意概括 有上下两行长度为 n 的数字序列 A 和序列 B,都是 1 到 n 的排列,若 a ...
- [BZOJ4994] [Usaco2017 Feb]Why Did the Cow Cross the Road III(树状数组)
传送门 1.每个数的左右位置预处理出来,按照左端点排序,因为左端点是从小到大的,我们只需要知道每条线段包含了多少个前面线段的右端点即可,可以用树状数组 2.如果 ai < bj < bi, ...
- [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 ...
- 4990: [Usaco2017 Feb]Why Did the Cow Cross the Road II 线段树维护dp
题目 4990: [Usaco2017 Feb]Why Did the Cow Cross the Road II 链接 http://www.lydsy.com/JudgeOnline/proble ...
- [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 ...
- BZOJ 4990 [USACO17FEB] Why Did the Cow Cross the Road II P (树状数组优化DP)
题目大意:给你两个序列,你可以两个序列的点之间连边 要求:1.只能在点权差值不大于4的点之间连边 2.边和边不能相交 3.每个点只能连一次 设表示第一个序列进行到 i,第二个序列进行到 j,最多连的边 ...
- Why Did the Cow Cross the Road III(树状数组)
Why Did the Cow Cross the Road III 时间限制: 1 Sec 内存限制: 128 MB提交: 65 解决: 28[提交][状态][讨论版] 题目描述 The lay ...
- [BZOJ4993||4990] [Usaco2017 Feb]Why Did the Cow Cross the Road II(DP + 线段树)
传送门 f[i][j]表示当前第i个,且最后一个位置连接到j 第一维可以省去,能连边的点可以预处理出来,dp可以用线段树优化 #include <cstdio> #include < ...
- [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 ...
随机推荐
- C#使用Font Awesome字体
这个类是一个开源类,我做了一些功能优化1.如果没有安装Font Awesome字体,可能需要直接去exe路径下使用对应名称字体.2.可以直接返回\uFxxx类型字体,方便winform按钮使用,不然的 ...
- java程序在windows后台执行的办法
1.新建run.txt文件 2.在文件中输入一下内容: @echo off start javaw -jar xx.jar exit 3.保存,修改文件名为run.bat4.双击即可 5.删除wind ...
- java的一维数组
数组的基础知识: 数组一旦创建,它的的大小是固定的.使用一个数组引用变量,通过下标来访问数组中的元素. 初始化数组的方法: 复制数组的方法: 1.使用循环语句逐个地复制数组的元素 2.使用System ...
- CMake 示例
1.需求 [1].使用第三方动/静太库 [2].本身代码部分编译为动/静态库 [3]多项目管理 原文转自:http://blog.csdn.net/shuyong1999/article/detail ...
- Three.js基础探寻二——正交投影照相机
本篇主要介绍照相机中的正交投影照相机. 第一篇传送门:Three.js基础探寻一 1.照相机 图形学中的照相机定义了三维空间到二维屏幕的投影方式. 针对投影方式照相机分为正交投影照相机和透视投影照相机 ...
- dump_stack的简单使用 【转】
转自:http://blog.chinaunix.net/uid-26403844-id-3361770.html http://blog.csdn.net/ryfjx6/article/detail ...
- xpath路径前可用方法测试
$x("string-length(//h3[@class='t'])") 8 $x("string(//h3[@class='t'])") "XPa ...
- nginx简单介绍
代理服务器:一般是指局域网内部的机器通过代理服务器发送请求到互联网上的服务器,代理服务器一般作用在客户端.应用比如:GoAgent,FQ神器. 一个完整的代理请求过程为: 客户端首先与代理服务器创建连 ...
- 002_HTTP每日分析及翻译
一.Request Headers Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng ...
- Android:自定义Dialog
自定义Dialog:显示SeekBar 效果图: 步骤: //SettingActivity.java button4.setOnClickListener(new View.OnClickListe ...