http://soj.me/1345
類似矩陣連乘。
/*dp,n顆珠子,求最後剩下一顆珠子能放出最大能量d[i][j]表示第i顆珠子開始,長度為j的珠子所釋放的最大能量轉移方程:d[i][j] = max(d[i][j] ,d[i][k]+d[x][j-k] + node[i].head * node[x].head * node[y].rear) ; 其中1<=i<=n ,1 <= j <= n , 1 <= k <= j , x = i + k ,if x > n , x = x - n ; y = i +j - 1 , if y > n , y = y - n ;最後求的就是max(d[1][n],d[2][n] , d[3][n] , d[4][n],........,d[n][n]) ; */// source code of submission 829987, Zhongshan University Online Judge System#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std ;struct Node{ int head , rear ;};Node node[105] ;int d[105][105] ;int main(){ int n ; while(scanf("%d",&n)!= EOF) { int temp ; for(int i = 1 ; i <= n ; i ++) { scanf("%d",&temp) ; node[i].head = temp ; if(i==1) node[n].rear = temp ; else node[i-1].rear = temp ; } memset(d,0,sizeof(d)) ; int x , y ; //d[i][j] 從i開始的j顆 for(int j = 2 ; j <= n ; j ++) { for(int i = 1 ; i <= n ; i ++) { for(int k = 1 ; k < j ; k ++) { x = i + k ; if(x > n ) x = x - n ; y = x + (j-k) -1 ; if(y>n) y = y - n ; d[i][j] = max(d[i][j] ,d[i][k]+d[x][j-k] + node[i].head * node[x].head * node[y].rear) ; } } } int Max = -1 ; for(int i = 1 ; i <= n ;i ++) { if(Max < d[i][n]) Max = d[i][n] ; } cout << Max << endl; }}