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. IOS开发 AFN和ASI

    做项目有一段时间了,项目过程中处理网络请求难免的,而对于选择第三方来处理网络请求肯定是个明智的选择! AFNetworking和ASIHTTPRequest   这两个第三方该如何选择       我 ...

  2. 解决java.util.concurrent.ExecutionException: org.apache.catalina.LifecycleException

    解决: 工程目录.settings\org.eclipse.wst.common.project.facet.core.xml文件中jst.web的version降低到2.5 <?xml ver ...

  3. Java静态分派与动态分派(二)

    方法调用并不等于方法执行,方法调用阶段唯一的任务就是确定被调用方法的版本(即调用哪一个方法),暂时还不涉及方法内部的具体运行过程. 在程序运行时,进行方法调用是最普遍.最频繁的操作,但是Class文件 ...

  4. 浅谈c#的三个高级参数ref out 和Params C#中is与as的区别分析 “登陆”与“登录”有何区别 经典SQL语句大全(绝对的经典)

    浅谈c#的三个高级参数ref out 和Params   c#的三个高级参数ref out 和Params 前言:在我们学习c#基础的时候,我们会学习到c#的三个高级的参数,分别是out .ref 和 ...

  5. 鸡肋的JdbcRDD

          今天准备将mysql的数据倒腾到RDD.非常早曾经就知道有一个JdbcRDD.就想着使用一下,结果发现却是鸡肋一个.       首先,看看JdbcRDD的定义: * An RDD tha ...

  6. shell选择语句、循环语句

    判断语句:  if 判断条件  then    语句  [elif]    [语句]  ...  [else    语句]  fi    #!/bin/bash  if [ $# -eq 0 ]  t ...

  7. hdu 2059 龟兔赛跑 (dp)

    /* 把起点和终点比作加油站,那总共同拥有n+2个加油站了, 每次都求出从第0个到第j个加油站(j<i)分别在加满油的情况下到第i个加油站的最短时间dp[i], 终于的dp[n+1]就是最优解了 ...

  8. Python代码分析工具

    Python代码分析工具:PyChecker.Pylint - CSDN博客 https://blog.csdn.net/permike/article/details/51026156

  9. 借助ltp语义分析提取特征,之后,文本生成

    """地点-哪里有做-业务-的(正规|靠谱)-公司?地点-做-业务-的(正规|靠谱)-公司(有哪些?|的联系方式是什么?|哪家口碑好值得信赖?)地点-做-业务-(怎么能省 ...

  10. HDU1083 Courses —— 二分图最大匹配

    题目链接:https://vjudge.net/problem/HDU-1083 Courses Time Limit: 20000/10000 MS (Java/Others)    Memory ...