HDU 4283 You Are the One

题目链接

有n个人排队,对于每个人有个不同的D,如果他是第k个排到的,会有(k-1)*D的不满,你可以将当前队首放入一个栈中并在任意时刻让栈顶元素出栈问最小的不满值

状态有两种转移,让队首直接出队或让他在入栈并在第i个人后出栈

#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
const int inf = 1000000000;
int T, N, D[105];
int f[105][105][105];
int DP(int l, int r, int k) {
	if(l == r) return k * D[l];
	int & res = f[l][r][k];
	if(res != -1) return res;
	res = inf;
	res = min(res, DP(l + 1, r, k + 1) + k * D[l]);
	for(int i = l + 1; i < r; i++) {
		int _k = k + i - l;
		res = min(res, DP(l + 1, i, k) + _k * D[l] 
					 + DP(i + 1, r, _k + 1));
	}
	res = min(res, DP(l + 1, r, k) + (k + r - l) * D[l]);
	return res;
}
int main() {
	scanf("%d", &T);
	for(int kase = 1; kase <= T; kase++) {
		scanf("%d", &N);
		for(int i = 1; i <= N; i++) scanf("%d", &D[i]);
		memset(f, -1, sizeof(f));
		printf("Case #%d: %d\n", kase, DP(1, N, 0));
	}
	return 0;
}

posted @ 2018-05-20 20:32  LJZ_C  阅读(124)  评论(0编辑  收藏  举报