HDU 4283
Test instructions: There are n men's cock line to the stage in turn, if a male cock in front row with K person, then the cock silk very angry, angry degree = (k-1) *d (D for the Cock silk degree). Now there is a small black house, the small black House advanced out, if a certain cock put in, then the people behind him will be able to take the stage first. Give everyone's cock silk degree and the original order, how to use the small black House, can make the most of the cock silk anger and minimum, to find the minimum value.
Ideas:
Anger levels #define RAGE value
Dp[i][j] represents the min (SUM (rage value)) of the interval from the first I-Stud to the J-cock (assuming that only this j-i+1 a cock silk)
Then for DP[I][J] the first I cock silk, it is possible 1th, 2nd ... The first j-i+1 to play. Consider the no. i cock Silk K-play.
1. Cock Silk I before the first appearance of the situation: in the interval I, the cock silk is originally the debut, is now the K-show, the original K-1 after the Cock silk I was the first to play, then there is a sub-problem dp[i+1][i+k-1] A case of k-1 a cock that was played before the first cock wire;
2. Cock Silk I when the time of the appearance: because the cock of I is the first k, then its contribution to the sum of the value of Anger is num[i]* (K-1);
3. After the first K-appearance of the Cock silk I: the rest of the people from the beginning of the first k+1, that is, a sub-question Dp[i+k][j], for the band of the cock, because the row from the first k+1, so the overall rage value to add k* (Sum[j]-sum[i+k-1]).
Synthesis: dp[i][j] = min (Dp[i][j], dp[i+1][i+k-1] + num[i] * (k-1) + dp[i+k][j] + (Sum[j]-sum[i+k-1]) * k).
Code
/** @author novicer* language:c++/c*/#include <iostream> #include <sstream> #include <fstream># include<vector> #include <list> #include <deque> #include <queue> #include <stack># include<map> #include <set> #include <bitset> #include <algorithm> #include <cstdio># include<cstdlib> #include <cstring> #include <cctype> #include <cmath> #include <ctime># Include<iomanip> #define INF 10000000using namespace Std;const double eps (1e-8); typedef long LONG Lint;int dp[105][ 105];int num[105];int Sum[105];int Main () {//freopen ("Input.txt", "R", stdin); int t;cin >> T;int kase = 1;while (t--) {memset (Dp,0,sizeof (DP)), memset (num,0,sizeof (num)), memset (sum,0,sizeof (sum)), int n;scanf ("%d", &n); for (int i = 1; I <= N; i++) {scanf ("%d", &num[i])//cout << num[i] << endl;sum[i] = sum[i-1] + num[i];//cout << Sum[i] <& Lt Endl;} for (int i = 1, i <= N; i++) {for (int j = i+1; J <= N; j + +) {DP[I][J] = inf;}} for (int l = 1, l < n; l++) {for (int i = 1; I <= n-l; i++) {Int J = i + l;for (int k = 1; k <= j-i + 1; k++ {Dp[i][j] = min (Dp[i][j], dp[i+1][i+k-1] + dp[i+k][j] + num[i]* (k-1) + k* (Sum[j]-sum[i+k-1]));} cout << "dp[" << I << "]" << "[" << j << "] =" << dp[i][j] << Endl;}} printf ("Case #%d:%d\n", Kase++,dp[1][n]);} return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
HDU 4283 You is the one (interval DP)