HDU 4283 You Are the One(区间DP)

/*
这道题才真正接触到区间DP的思想
d[i][j]:表示从第i个到到第j个人的最小值,那么第i个人可以是第一个出,也可以是最后一个出,假设是第k个出,则
区间可分为[i + 1, i + 1 + k - 1 - 1],d[i + k, j]前者肯定是在k之前出,后者在k之后,于是转换为区间DP问题
其中需要对后者做一下处理,加上(k - 1) * (sum[j] - s[i + k - 1])
*/

#include <cstdio>
#include <cstring>
const int nMax = 107;
const int INF = 0x3fffffff;
int T;
int n;
int A[nMax];
int sum[nMax];
int d[nMax][nMax];
int min(int a, int b)
{
	return a < b ? a : b;
}
int main()
{
	//freopen("e://data.in", "r", stdin);
	scanf("%d", &T);
	int cas;
	for(cas = 1; cas <= T; ++ cas)
	{
		scanf("%d", &n);
		int i, j, k, l;
		sum[0] = 0;
		for(i = 1; i <= n; ++ i) 
		{
			scanf("%d", &A[i]);
			sum[i] = sum[i - 1] + A[i];
		}
		for(i = 1; i <= n; ++ i) 
			for(j = i + 1; j <= n; ++ j)
				d[i][j] = INF;
		for(i = 1; i <= n; ++ i) d[i][i] = d[i][i - 1] = 0;
		for(k = 1; k < n; ++ k)
		{
			for(i = 1; i <= n; ++ i)
			{
				for(j = i + k; j <= n; ++ j)
				{
					for(l = 1; l <= k + 1; ++ l)
						d[i][j] = min(d[i][j], d[i + 1][i + 1 + l - 1 - 1] + (sum[j] - sum[i + l - 1]) * l + d[i + l][j] + (l - 1) * A[i]);
				}
			}
		}
		printf("Case #%d: %d\n", cas, d[1][n]);
	}
	return 0;
}

你可能感兴趣的:(HDU 4283 You Are the One(区间DP))