BZOJ 4993: [Usaco2017 Feb]Why Did the Cow Cross the Road II LCS

4993: [Usaco2017 Feb]Why Did the Cow Cross the Road II

Time Limit: 10 Sec  Memory Limit: 256 MB
Submit: 28  Solved: 19
[Submit][Status][Discuss]

Description

上下有两个长度为n、位置对应的序列A、B,
其中数的范围均为1~n。若abs(A[i]-B[j])<= 4,则A[i]与B[j]间可以连一条边。
现要求在边与边不相交的情况下的最大的连边数量。
n <= 10^3

Input

Output

Sample Input

6
1
2
3
4
5
6
6
5
4
3
2
1

Sample Output

5

裸LCS

刷水有益健康


#include<cmath>
#include<ctime>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<iomanip>
#include<vector>
#include<string>
#include<bitset>
#include<queue>
#include<set>
#include<map>
using namespace std;

typedef long long ll;

inline int read()
{
	int x=0,f=1;char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}
	return x*f;
}
void print(int x)
{if(x<0)x=-x,putchar('-');if(x>=10)print(x/10);putchar(x%10+'0');}

int n,x[1007],y[1007],dp[1007][1007];
int main() {
    
    n=read();
	for(int i=1 ; i<=n ; i++) x[i]=read();
	for(int i=1 ; i<=n ; i++) y[i]=read();
	for(int i=1 ; i<=n ; i++)
	{
		for(int j=1 ; j<=n ; j++)
		{
			if(abs(x[i]-y[j]) <= 4) dp[i][j] = dp[i-1][j-1]+1;
			else dp[i][j] = max(dp[i][j-1],dp[i-1][j]);
		}
	}
	cout << dp[n][n] << '\n';
    return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值