sicily 1345. 能量項鏈(dp)

來源:互聯網
上載者:User

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;    }}

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.