# CH5301 Stone Merge "interval DP"

Source: Internet
Author: User

5301 Stone Merge 0x50"Dynamic Programming" examplesDescribe

An N heap of sand is arranged in a row, numbered 1,2,3,...,n (n<=300). Each heap of sand has a certain quantity, can be described by an integer, now to merge the N heap of sand into a pile, each time can only merge adjacent two piles, the cost of the merger of the two piles of sand, the sand adjacent to the two piles of sand will be adjacent to the new heap, when merging due to different order of choice, The total cost of the merger is not the same, if there are 4 piles of sand 1 3 5 2 We can merge 1, 2 heap, the cost is 4, get 4 5 2 and merge the heap, the cost is 9, get 9 2, then merge to 11, the total cost is 4+9+11=24, if the second step is to merge 2,3 heap first, the cost is 7 4 7, the last merger cost is 11, the total cost is 4+7+11=22; The problem is: find a reasonable way to minimize the total cost. The minimum cost of the output.

Input format

The first row, a number n, represents the heap number N of sand.
The second row n number, which represents the mass of sand per heap (<=1000).

Output format

Minimum cost of consolidation

Sample input
`41 3 5 2`
Sample output
`22`
Source

CCF NOI1995

Test instructions

n Heap of stones, each pile has a weight of AI. Now I want to merge them into a pile, and merge the adjacent one at a time. The energy expended is combined with two piles of gravel weights and. Requires minimal physical exertion.

Ideas:

At any given moment, any heap of stones can be described by a closed interval [l,r], which means that the gravel is a combination of the first l~r stones. There must be an integer k, before this heap of stones is formed, there is a l~k heap of stones are merged, k+1~r heap of Stones are merged, and then these two piles are merged into L~r

The information on the two-length interval is shifted to a longer interval, and the dividing point K is the decision to transfer. The interval length len is used as the DP phase.

Abuse Dog Treasure Note:

The interval DP takes "interval Length" as the DP stage, using two coordinates (the left and right end of the interval) to describe each dimension. In Interval DP, a state is transferred from a state that is represented by a number of bands smaller than it is contained in it, so the decision of interval DP is often the method of dividing the interval.

In order to realize the state transition equation of dynamic programming, it is necessary to distinguish the stage, state and decision, and the three should circulate sequentially from the outside to the inside.

`1 //#include <bits/stdc++.h>2#include <iostream>3#include <cmath>4#include <algorithm>5#include <stdio.h>6#include <cstring>7#include <map>8 9 #defineINF 0x3f3f3f3fTen using namespacestd; OnetypedefLong LongLL; A  - intN; - Const intMAXN =305; the intA[MAXN]; - intDP[MAXN][MAXN], SUM[MAXN]; -  - intMain () + { -      while(SCANF ("%d", &n)! =EOF) { +memset (SUM,0,sizeof(sum)); AMemset (DP, INF,sizeof(DP)); at          for(inti =1; I <= N; i++){ -scanf"%d", &a[i]); -Sum[i] = sum[i-1] +A[i]; -Dp[i][i] =0; -         } -  in          for(intLen =2; Len <= N; len++){ -              for(intL =1; L <= N-len +1; l++){ to                 intR = L + Len-1; +                  for(intK = l; K < R; k++){ -Dp[l][r] = min (Dp[l][r], dp[l][k] + dp[k +1][r]); the                 } *Dp[l][r] + = sum[r]-sum[l-1]; \$             }Panax Notoginseng         } -  theprintf"%d\n", dp[n]); +     } A     return 0; the}`

CH5301 Stone Merge "interval DP"

Related Keywords:

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.