xtu read problem training 4 B - Multiplication Puzzle

Multiplication Puzzle

Time Limit: 1000ms

Memory Limit: 65536KB

This 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.

Input

The 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.

Output

Output must contain a single integer - the minimal score.

Sample Input

6
10 1 50 50 20 5

Sample Output

3650

Source

Northeastern 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 long
14 #define INF 0x3f3f3f3f
15 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 }

xtu read problem training 4 B - Multiplication Puzzle

时间: 2024-08-26 14:45:20

xtu read problem training 4 B - Multiplication Puzzle的相关文章

xtu read problem training 4 A - Moving Tables

Moving Tables Time Limit: 2000ms Memory Limit: 65536KB This problem will be judged on ZJU. Original ID: 102964-bit integer IO format: %lld      Java class name: Main The famous ACM (Advanced Computer Maker) Company has rented a floor of a building wh

xtu read problem training 2 B - In 7-bit

In 7-bit Time Limit: 2000ms Memory Limit: 65536KB This problem will be judged on ZJU. Original ID: 371364-bit integer IO format: %lld      Java class name: Main Very often, especially in programming contests, we treat a sequence of non-whitespace cha

xtu read problem training 3 A - The Child and Homework

The Child and Homework Time Limit: 1000ms Memory Limit: 262144KB This problem will be judged on CodeForces. Original ID: 437A64-bit integer IO format: %I64d      Java class name: (Any) Once upon a time a child got a test consisting of multiple-choice

xtu read problem training A - Dividing

A - Dividing Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Description Marsha and Bill own a collection of marbles. They want to split the collection among themselves so that both receive an equal share of the marbles.

xtu read problem training 3 B - Gears

Gears Time Limit: 2000ms Memory Limit: 65536KB This problem will be judged on ZJU. Original ID: 378964-bit integer IO format: %lld      Java class name: Main Bob has N (1 ≤ N ≤ 2*105) gears (numbered from 1 to N). Each gear can rotate clockwise or co

xtu read problem training B - Tour

B - Tour Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Description John Doe, a skilled pilot, enjoys traveling. While on vacation, he rents a small plane and starts visiting beautiful places. To save money, John must de

POJ 1651 Multiplication Puzzle (区间dp)

题目大意:对n个数组成的序列取数,规定最两边不能取,每次取一个a[i],得到 a[l] * a[i] * a[r] 的分数(a[l]是a[i]左边的数,a[r]是a[i]右边的数),并把这个数从序列中移走,求n-2次取数后的得分和的最小值 分析:正着确定状态不好做,不如反着来,设dp[l][r]为向区间[l, r]中填满数所得到分数和的最小值,考虑最近一次填数的位置,不难得出: dp[l][r] = fmin(dp[l][m] + dp[m][r] + a[l] * a[m] * a[r]) (

POJ1651——Multiplication Puzzle

Multiplication Puzzle Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6511   Accepted: 3964 Description The multiplication puzzle is played with a row of cards, each containing a single positive integer. During the move player takes one

POJ 1651 Multiplication Puzzle(区间dp)

Language: Default Multiplication Puzzle Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6693   Accepted: 4083 Description The multiplication puzzle is played with a row of cards, each containing a single positive integer. During the move