Card games
Main topic: give you a row of cards, you can draw some cards from it (but not the most left and right cards), each card has a number, when you draw a card, you will have the number of cards and the cards left and right two cards of the number of the product of the score, asked when left the left and the best two cards, What is the smallest score you can get ?
This question seems quite complicated, but if we change the way of thinking, this problem will become familiar
First of all, the problem is obvious DP (find the minimum, can't take the limit mode)
First of all, he wants us to draw a card, right? At the outset we might have thought of taking a look at it, and then sweeping through the other cards to see if we could smoke, but the immediate consequence would be that it was difficult to store the score on the cards we had previously smoked, so if we were to draw the cards, The extraction of the left I-right J-card has been written down (the equivalent of an evacuation), and then we add this card to the i-1 and j+1 cards and the score of the left I Zhang and right J card is not equal to the score of the card we are currently pumping! So in the end we can maintain a minimum interval, extending the interval to n-2 (removing the leftmost and most right two), and the resulting value is the last smallest fraction.
And this method is exactly the order of matrix multiplication, the idea of familiarity and kindness
1#include <stdio.h>2#include <stdlib.h>3#include <string.h>4 5 Static Long Longcards[ -];6 Static Long Longdp[ -][ -];7 8 voidSearch (Const int);9 Long LongMinConst Long Long,Const Long Long);Ten One intMainvoid) A { - intN, I; - while(~SCANF ("%d", &N)) the { - for(i =0; i < N; i++) -scanf"%d", &cards[i]); - //memset (DP, 0, sizeof (DP)); + Search (N); - } + return 0; A } at - Long LongMinConst Long LongXConst Long Longy) - { - returnx < y?x:y; - } - in voidSearch (Const intN) - { to intI, J, K, Pos; + - for(i =1; I < N-1; i++) theDp[i][i] = cards[i-1] * Cards[i] * cards[i +1]; * for(k =1; K < N-2; k++) $ {Panax Notoginseng for(i =1; I < N-1K i++) - { thepos = i +K; +Dp[i][pos] = cards[i] * cards[i-1] * Cards[pos +1] + dp[i +1][pos];//note the location of the cards A for(j = i +1; J < Pos; J + +) the { +Dp[i][pos] =min (Dp[i][pos], -CARDS[J] * cards[i-1] * Cards[pos +1] + dp[i][j-1] + dp[j +1][pos]); $ } $Dp[i][pos] = min (Dp[i][pos], cards[i-1] * Cards[pos] * Cards[pos +1] + Dp[i][pos-1]); - } - } theprintf"%lld\n", dp[1][n-2]); -}
Dp:multiplication Puzzle (POJ 1651)