DP 想了好久 还是看了一下题解。。。。 f[i][j]表示i到j全部合并后的最小花费,f[i][j] = min{f[i][k]+f[k+1][j]+d[i][k]*d[k+1][j]} (i <= k <= j-1)
#include#include #include #include using namespace std;#define INF 100000000int f[101][101], d[101];int n;int main(){ while (scanf("%d",&n) == 1) { memset(d, 0, sizeof(d)); memset(f, 0, sizeof(f)); for (int i = 1; i <= n; i++) scanf("%d",&d[i]), d[i] += d[i-1]; for (int k = 2; k <= n; k++) for (int i = 1; i <= n-k+1; i++) { int j = i+k-1; f[i][j] = INF; for (int q = i; q <= j-1; q++) f[i][j] = min(f[i][j], f[i][q]+f[q+1][j]+((d[q]-d[i-1])%100)*((d[j]-d[q])%100)); } printf("%d\n",f[1][n]); } return 0;}