poj 3056 The Bavarian Beer Party (区间dp)

题目要求 求最大匹配,但是不能敬酒的人手交叉。要注意的是枚举的长度每次长度都是两倍增长。

状态方程:

dp[i][j] = dp[i+1][j-1] + (a[i]==b[j])  如果区间两边可以间就那么就从中间往外推

dp[i][j] = max { dp[i][k] +dp[k+1][j] }

#include<iostream>
#include<math.h>
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<string>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<stack>
#define B(x) (1<<(x))
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned ui;
const int oo = 0x3f3f3f3f;
//const ll OO = 0x3f3f3f3f3f3f3f3f;
const double eps = 1e-9;
#define lson rt<<1
#define rson rt<<1|1
void cmax(int& a, int b){ if (b > a)a = b; }
void cmin(int& a, int b){ if (b < a)a = b; }
void cmax(ll& a, ll b){ if (b > a)a = b; }
void cmin(ll& a, ll b){ if (b < a)a = b; }
void cmax(double& a, double b){ if (a - b < eps) a = b; }
void cmin(double& a, double b){ if (b - a < eps) a = b; }
void add(int& a, int b, int mod){ a = (a + b) % mod; }
void add(ll& a, ll b, ll mod){ a = (a + b) % mod; }
const ll MOD = 1000000007;
const int maxn = 1100;
int dp[maxn][maxn];
int a[maxn];

int main(){
	int T, n, cas, v;
	scanf("%d", &T);
	for (int cas = 1; cas <= T; cas++){
		scanf("%d", &n);
		for (int i = 1; i <= n; i++)
			scanf("%d", &a[i]);
		memset(dp, 0, sizeof dp);
		for (int L = 2; L <= n; L += 2)
		for (int i = 1; i + L - 1 <= n; i++){
			int j = i + L - 1;
			dp[i][j] = dp[i + 1][j - 1] + (a[i] == a[j]);
			for (int k = i; k <= j; k++)
				dp[i][j] = max(dp[i][j], dp[i][k] + dp[k + 1][j]);
		}
		printf("%d\n", dp[1][n]);
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值