POJ 1651 Multiplication Puzzle, pojmultiplication

Source: Internet
Author: User

POJ 1651 Multiplication Puzzle, pojmultiplication
Multiplication Puzzle

Time Limit:1000 MS   Memory Limit:65536 K
Total Submissions:8760   Accepted:5484
DescriptionThe 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 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 wocould take the cards in the opposite order, I. e. 50, then 20, then 1, the score wocould 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 question: there are n cards with numbers in a row, take the N-2 card (the first card and the last one can not be taken) in a certain order, each time only take one card. Each time a card is taken away, a score is obtained. The score size is the product of the number of the cards to be taken and the cards on both sides of the card. Take the middle N-2 card in different order, the total score may be different, now you want to find a given group of cards, according to the above rules take the card's possible minimum total score. Solution: a topic in the interval DP. If dp [l] [r] is set to represent the optimal solution of the range [l, r], the state transition is as follows: 1. When r-l = 2, that is, when there are only three numbers, apparently dp [l] [r] = num [l] * num [l + 1] * num [r]; 2. When r-l> 2, enumerate the last number of items to be taken, dp [l] [r] = min (dp [l] [r], dp [l] [I] + dp [I] [r] + num [l] * num [I] * num [r]), where l <I <r. Attach the AC code: 1 # include <cstdio> 2 # include <cstring> 3 # include <algorithm> 4 using namespace std; 5 const int maxn = 105; 6 const int inf = 0x3f3f3f; 7 int num [maxn], dp [maxn] [maxn]; 8 int n; 9 10 int dfs (int l, int r) {11 if (abs (l-r) <2) 12 return 0; 13 if (r-l = 2) 14 return dp [l] [r] = num [l] * num [l + 1] * num [r]; 15 if (dp [l] [r]! = Inf) 16 return dp [l] [r]; 17 for (int I = l + 1; I <r; ++ I) 18 dp [l] [r] = min (dp [l] [r], dfs (l, I) + dfs (I, r) + num [l] * num [I] * num [r]); 19 return dp [l] [r]; 20} 21 22 int main () {23 while (~ Scanf ("% d", & n) {24 for (int I = 0; I <n; ++ I) 25 scanf ("% d ", num + I); 26 memset (dp, inf, sizeof (dp); 27 dfs (0, n-1); 28 printf ("% d \ n ", dp [0] [n-1]); 29} 30 return 0; 31}View Code

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.