Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 6511 | Accepted: 3964 |
Description
Input
Output
Sample Input
6 10 1 50 50 20 5
Sample Output
3650
Source
#include <map> #include <set> #include <list> #include <stack> #include <queue> #include <vector> #include <cmath> #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std; const int inf = 0x3f3f3f3f; __int64 dp[110][110]; int a[110]; int main() { int n; while (~scanf("%d", &n)) { memset (dp, 0, sizeof(dp)); __int64 tmp; for (int i = 1; i <= n; ++i) { scanf("%d", &a[i]); } for (int i = 1; i <= n - 2; ++i) { dp[i][i + 2] = a[i] * a[i + 1] * a[i + 2]; } for (int i = n; i >= 1; --i) { for (int j = i + 2; j <= n; ++j) { tmp = inf; // printf("%d --- %d ", i, j); for (int k = i + 1; k < j; ++k) { // printf("k = %d\n", k); // printf("dp[%d][%d] = %d, dp[%d][%d] = %d, a[%d] * a[%d] * a[%d] = %d\n", i, k - 1, dp[i][k - 1], k + 1, j, dp[k + 1][i], k - 1, k+1, k, a[k - 1] * a[k + 1] * a[k]); tmp = min(tmp, dp[i][k] + dp[k][j] + a[i] * a[j] * a[k]); } dp[i][j] = tmp; // printf("dp[%d][%d] = %d\n", i, j, tmp); } } printf("%I64d\n", dp[1][n]); } }
POJ1651——Multiplication Puzzle
原文:http://blog.csdn.net/guard_mine/article/details/40986217