[Usaco2017 Feb]Why Did the Cow Cross the RoadII

Posted evenbao

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[Usaco2017 Feb]Why Did the Cow Cross the RoadII相关的知识,希望对你有一定的参考价值。

[题目链接]

          https://www.lydsy.com/JudgeOnline/problem.php?id=4993

[算法]

         动态规划

          转移类似于求LCS

[代码]

        

#include<bits/stdc++.h>
using namespace std;
#define MAXN 1010

int n;
int a[MAXN] , b[MAXN];
int f[MAXN][MAXN];

template <typename T> inline void chkmax(T &x,T y) { x = max(x,y); }
template <typename T> inline void chkmin(T &x,T y) { x = min(x,y); }
template <typename T> inline void read(T &x)
{
    T f = 1; x = 0;
    char c = getchar();
    for (; !isdigit(c); c = getchar()) if (c == -) f = -f;
    for (; isdigit(c); c = getchar()) x = (x << 3) + (x << 1) + c - 0;
    x *= f;
}
int main()
{
        
        read(n);
        for (int i = 1; i <= n; i++) read(a[i]);
        for (int i = 1; i <= n; i++) read(b[i]);
        for (int i = 1; i <= n; i++)
        {
                for (int j = 1; j <= n; j++)
                {
                        if (abs(a[i] - b[j]) <= 4)
                                f[i][j] = f[i - 1][j - 1] + 1;
                        else f[i][j] = max(f[i][j - 1] , f[i - 1][j]);                
                }
        }
        printf("%d
",f[n][n]);
        
        return 0;
    
}

 

以上是关于[Usaco2017 Feb]Why Did the Cow Cross the RoadII的主要内容,如果未能解决你的问题,请参考以下文章

[BZOJ4989] [Usaco2017 Feb]Why Did the Cow Cross the Road(树状数组)

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

BZOJ4989 [Usaco2017 Feb]Why Did the Cow Cross the Road 树状数组 逆序对

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

bzoj4994[Usaco2017 Feb]Why Did the Cow Cross the Road III 树状数组

洛谷 P3663 [USACO17FEB]Why Did the Cow Cross the Road III S