xtu read problem training 4 B - Multiplication Puzzle

來源:互聯網
上載者:User

標籤:style   blog   http   color   java   os   io   for   

Multiplication PuzzleTime Limit: 1000msMemory Limit: 65536KBThis problem will be judged on PKU. Original ID: 1651
64-bit integer IO format: %lld      Java class name: Main  The multiplication puzzle is played with a row of cards, each containing a single positive integer. During the move player takes one card out of the row and scores the number of points equal to the product of the number on the card taken and the numbers on the cards on the left and on the right of it. It is not allowed to take out the first and the last card in the row. After the final move, only two cards are left in the row. 

The goal is to take cards in such order as to minimize the total number of scored points. 

For example, if cards in the row contain numbers 10 1 50 20 5, player might take a card with 1, then 20 and 50, scoring 
10*1*50 + 50*20*5 + 10*50*5 = 500+5000+2500 = 8000
If he would take the cards in the opposite order, i.e. 50, then 20, then 1, the score would be 
1*50*20 + 1*20*5 + 10*1*5 = 1000+100+50 = 1150. InputThe 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. OutputOutput must contain a single integer - the minimal score. Sample Input
610 1 50 50 20 5
Sample Output
3650
SourceNortheastern Europe 2001, Far-Eastern Subregion 解題:dp[i][j]表示從i到j被劃分後的最小值!為什麼dp[i][j] = min(dp[i][j],dp[i][k]+dp[k][j]+d[i]*d[j]*d[k])ne 舉個栗子 1 2 3 4 5 dp[1][5] = min(dp[1][5],dp[1][3]+dp[3][5]+d[1]*d[3]*d[5]) dp[i][j]表示i j段 剩有i j,像剛才的轉移方程,dp[1][5]不是取了3以後 剩下了1 5 麼 
 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <climits> 7 #include <vector> 8 #include <queue> 9 #include <cstdlib>10 #include <string>11 #include <set>12 #include <stack>13 #define LL long long14 #define INF 0x3f3f3f3f15 using namespace std;16 int dp[110][110],d[110],n;17 int main(){18     int i,j,k;19     while(~scanf("%d",&n)){20         for(i = 1; i <= n; i++)21             scanf("%d",d+i);22         memset(dp,0,sizeof(dp));23         for(k = 3; k <= n; k++){24             for(i = 1; i+k-1 <= n; i++){25                 dp[i][i+k-1] = INF;26                 for(j = i+1; j < i+k; j++)27                     dp[i][i+k-1] = min(dp[i][i+k-1],dp[i][j]+dp[j][i+k-1]+d[i]*d[j]*d[i+k-1]);28             }29         }30         cout<<dp[1][n]<<endl;31     }32     return 0;33 }
View Code

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.