Description

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

其中数的范围均为1~n。若abs(A[i]-B[j])<= 4,则A[i]与B[j]间可以连一条边。

现要求在边与边不相交的情况下的最大的连边数量。

n <= 10^3

Sample Input

6

1

2

3

4

5

6

6

5

4

3

2

1

Sample Output

5


网上有题解说求最长公共上升序列,一脸懵逼,反正我只会DP。设f[i][j]表示A序列选到第i个,B序列选到第j个的最大连线数,转移就十分明显了

#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define inf 0x7f7f7f7f
using namespace std;
typedef long long ll;
typedef unsigned int ui;
typedef unsigned long long ull;
inline int read(){
int x=0,f=1;char ch=getchar();
for (;ch<'0'||ch>'9';ch=getchar()) if (ch=='-') f=-1;
for (;ch>='0'&&ch<='9';ch=getchar()) x=(x<<1)+(x<<3)+ch-'0';
return x*f;
}
inline void print(int x){
if (x>=10) print(x/10);
putchar(x%10+'0');
}
const int N=1e3;
int f[N+10][N+10],A[N+10],B[N+10];
int main(){
int n=read();
for (int i=1;i<=n;i++) A[i]=read();
for (int i=1;i<=n;i++) B[i]=read();
for (int i=1;i<=n;i++)
for (int j=1;j<=n;j++){
f[i][j]=max(f[i-1][j],f[i][j-1]); //要么不选
if (abs(A[i]-B[j])<=4) f[i][j]=max(f[i][j],f[i-1][j-1]+1); //可以的话就选
}
printf("%d\n",f[n][n]);
return 0;
}

[Usaco2017 Feb]Why Did the Cow Cross the Road II (Gold)的更多相关文章

  1. 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 ...

  2. [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 ...

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

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

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

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

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

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

  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 III (Gold)

    Description 给定长度为2N的序列,1~N各处现过2次,i第一次出现位置记为ai,第二次记为bi,求满足ai < aj < bi < bj的对数 Sample Input ...

  8. [Usaco2017 Feb]Why Did the Cow Cross the Road I (Gold)

    Description 有一幅n*n的方格图,n <=100,每个点上有一个值. 从(1,1)出发,走到(n,n),只能走上下左右. 每走一步花费t,每走三步需要花费走完三步后到达格子的值. 求 ...

  9. [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 ...

随机推荐

  1. python解析xml文件之xml.etree.cElementTree和xml.etree.ElementTree区别和基本使用

    1.解析速度:ElementTree在 Python 标准库中有两种实现.一种是纯 Python 实现例如 xml.etree.ElementTree ,另外一种是速度快一点的 xml.etree.c ...

  2. 使用Python实现一个简单的项目监控

    在公司里做的一个接口系统,主要是对接第三方的系统接口,所以,这个系统里会和很多其他公司的项目交互.随之而来一个很蛋疼的问题,这么多公司的接口,不同公司接口的稳定性差别很大,访问量大的时候,有的不怎么行 ...

  3. hi3531 SDK已编译文件系统制作jffs2文件系统镜像并解决这个问题 .

    一, 安装SDK 1.Hi3531 SDK包位置 在"Hi3531_V100R001***/01.software/board"文件夹下,您能够看到一个 Hi3531_SDK_Vx ...

  4. 自动填充输入框 Asp .Net Mvc

    1 效果 当在一个文本框中输入时,可以自动查找相关选项,然后加载出来以供参考   2 前台代码   <link href="~/Content/themes/base/jquery-u ...

  5. Selenium系列之--04 常见元素操作总结

    一.Selenium总共有八种定位方法  By.id()  通过id定位 By.name()  通过name 定位 By.xpath() 通过xpath定位 By.className() 通过clas ...

  6. Blocks实现代理传值

    一.RootViewController: #import "RootViewController.h" #import "SecondViewController.h& ...

  7. CSS的两个class选择器紧挨在一起

    有一段HTML代码: <a class="glyphicons white no-js cogwheel" href="#" target="_ ...

  8. 2016/05/25 Ajax 跨域 转

    起因: 起因是这样的,为了复用,减少重复开发,单独开发了一个用户权限管理系统,共其他系统获取认证与授权信息,暂且称之为A系统:调用A系统以B为例.在B系统 中用ajax调用A系统系统的接口(数据格式为 ...

  9. HDU 5274 Dylans loves tree 树链剖分+线段树

    Dylans loves tree Problem Description Dylans is given a tree with N nodes. All nodes have a value A[ ...

  10. html页面内锚点定位及跳转方法总结

    1.最简单的方法是锚点用<a>标签,在href属性中写入DIV的id.如下: <!DOCTYPE html><html><head><style& ...