其他分享
首页 > 其他分享> > 题解 HDU4283 You Are the One

题解 HDU4283 You Are the One

作者:互联网

分析

由于栈复杂的性质,此题不可贪心(数据也有暗示)。

栈有先进后出的性质,因此一个人能第\(k\)位出栈,当且仅当有\(k-1\)个人比ta后进栈。

据此性质,考虑依次给每个人确定位置,自然想出每次枚举区间左端点对应人的位置以转移的区间dp。

代码

#include <bits/stdc++.h>

#define rei register int
#define ll long long
#define db double

using namespace std;

const int inf = 0x3f3f3f3f;
const ll INF = (ll)1e18 + 5;
const int N = 105;

int n, a[N], qzh[N], dp[N][N];

int cost(int l, int r) { return l <= r? qzh[r] - qzh[l - 1] : 0; }

inline void bemin(int &x, int y) { x = min(x, y); }
inline void bemax(int &x, int y) { x = max(x, y); }


int main() {
    ios::sync_with_stdio(0), cin.tie(0);
    int case_num;
    cin >> case_num;
    for(rei t = 1; t <= case_num; ++t) {
	memset(dp, 0x3f, sizeof(dp));
	cin >> n;
	for(rei i = 1; i <= n; ++i) cin >> a[i], qzh[i] = qzh[i - 1] + a[i];
	for(rei i = 1; i <= n + 1; ++i) dp[i][i] = a[i], dp[i][i - 1] = 0;
	for(rei len = 2; len <= n; ++len) {
	    for(rei l = 1; l <= n - len + 1; ++l) {
		int r = l + len - 1;
		for(rei k = 1; k <= len; ++k) {
		    bemin(dp[l][r], dp[l + 1][l + k - 1] + k * a[l] + dp[l + k][r] + k * cost(l + k, r));
		}
	    }
	}
	cout << "Case #" << t << ": " << dp[1][n] - qzh[n] << "\n";
    }
    return 0;
}

标签:HDU4283,const,int,题解,ll,qzh,rei,define
来源: https://www.cnblogs.com/Szzkp/p/15314499.html