Input
The first line of the input contains the number of cards N (3 <= N <= 100). The second line contains N integers in the range from 1 to 100, separated by spaces.Output
Output must contain a single integer - the minimal score.Sample Input
6 10 1 50 50 20 5Sample Output
3650本题较易,主要解释状态转移方程:
dp[s][e]=min{dp[s][k]+sample[s]∗sample[k]∗sample[e]+dp[k][e]}其中,dp[s][e]表示位置s、e为首尾的区间所能对应的最低得分sample[]表示位置为k的牌对应的数值 由题可知,对任意一个区间而言,最左和最右的牌是永远在桌面上的。我们令位置 k 为区间 [s,e] 中最后一张牌的位置,那么显然,对于这个区间来说,取出位置 k 上的牌后,区间的最终分数就是状态转移方程中右侧最小值函数内的部分。为此,我们只需对每一个区间枚举出它的最小得分,区间长度逐渐加大,就可以最终得到区间 [1,n] 所对应的最低得分了。 #include <iostream> #include <cstdio> #include <algorithm> #include <vector> #include <cstring> #include <queue> using namespace std; const int INF = 0x7fffffff; int dp[104][104], sample[105]; int main(){ #ifdef TEST freopen("test.txt", "r", stdin); #endif // TEST int n; while(cin >> n){ memset(dp, 0, sizeof(dp)); for(int i = 0; i < n; i++){ cin >> sample[i]; } for(int len = 3; len <= n; len++){//区间长度 for(int s = 0, e = len-1; e < n; s++, e++){ dp[s][e] = INF; for(int k = s+1; k < e; k++) dp[s][e] = min(dp[s][e], dp[s][k]+sample[s]*sample[k]*sample[e]+dp[k][e]); } } cout << dp[0][n-1] << endl; } return 0; }