Wikioi 1048 Stone Merge
Title Description
Description
There are n heap of stones in a row, each pile of stones have a weight of w[i], each merge can merge adjacent two piles of stones, the cost of a merger is the weight of two piles of stone and w[i]+w[i+1]. Ask what sort of merger sequence it takes to minimize the total merger cost.
Enter a description
Input Description
First line an integer n (n<=100)
Second row n integers w1,w2...wn (wi <= 100)
Output description
Output Description
An integer representing the minimum consolidation cost
Sample input
Sample Input
4
4 1 1 4
Sample output
Sample Output
18
Ideas:
Dividing stages with interval lengths
PS. If there are any two piles of direct greed, the more the first to merge the number of calculations
Code:
1#include <iostream>2#include <cstdio>3#include <string>4#include <cstring>5#include <algorithm>6 7 using namespacestd;8 Const intMAXN = $, Maxnum =10000000;9 intN,J,VALUE[MAXN],SUM[MAXN],DP[MAXN][MAXN];Ten intMain () { OneCin>>N; A for(inti =1; I <= n;i++){ -Cin>>Value[i]; -sum[i]= sum[i-1] +Value[i]; the } - for(intL =2; l <= n;l++){ - for(inti =1; I <= n-l +1; i++){ -j = i + L-1; +DP[I][J] =Maxnum; - for(intK = I;k < j;k++) +Dp[i][j] = min (dp[i][j],dp[i][k] + dp[k+1][J] + sum[j]-sum[i-1]); A } at } -cout<<dp[1][n]; - return 0; -}View Code
Wikioi 3002 Stone Merge 3
Title Description
Description
There are n heap of stones in a row, each pile of stones have a weight of w[i], each merge can merge adjacent two piles of stones, the cost of a merger is the weight of two piles of stone and w[i]+w[i+1]. Ask what sort of merger sequence it takes to minimize the total merger cost.
Enter a description
Input Description
First line an integer n (n<=3000)
Second row n integers w1,w2...wn (WI <= 3000)
Output description
Output Description
An integer representing the minimum consolidation cost
Sample input
Sample Input
4
4 1 1 4
Sample output
Sample Output
18
Data range and Tips
Data Size & Hint
The data range is larger than the "gravel merge"
Ideas:
Quadrilateral inequalities, recording interval dividing point K
Code:
1#include <iostream>2#include <cstdio>3#include <string>4#include <cstring>5#include <algorithm>6 7 using namespacestd;8 Const intMAXN =4000, Maxnum =100000000;9 intN,J,VALUE[MAXN],SUM[MAXN],DP[MAXN][MAXN],A[MAXN][MAXN];Ten intMain () { OneCin>>N; A for(inti =1; I <= n;i++){ -Cin>>Value[i]; -sum[i]= sum[i-1] +Value[i]; theA[i][i] =i; - } - for(intL =2; l <= n;l++){ - for(inti =1; I <= n-l +1; i++){ +j = i + L-1; -DP[I][J] =Maxnum; + for(intK = a[i][j-1];k <= a[i+1][j];k++){ A if(Dp[i][j] > dp[i][k] + dp[k+1][J] + sum[j]-sum[i-1]){ atDP[I][J] = Dp[i][k] + dp[k+1][J] + sum[j]-sum[i-1]; -A[I][J] =K; - } - - } - in } - } tocout<<dp[1][n]; + return 0; -}View Code
Wikioi 2102 Stone Merge 2
Title Description
Description
In a garden-shaped playground surrounded by n heap of stones, it is necessary to merge the stones into a pile in sequence. The rule is that only the adjacent 2 stacks are merged into a new pile each time, and a new pile of stones is counted as the combined score.
In this paper, we design 1 algorithms to calculate the minimum score and the maximum score of combining n heap stones into 1 piles.
Enter a description
Input Description
The 1th line of data is a positive integer n,1≤n≤100, which indicates that there are n heap stones. The 2nd line has n number, which indicates the number of stones per heap.
Output description
Output Description
Output total 2 lines, 1th behavior minimum score, 2nd behavior maximum score.
Sample input
Sample Input
4
4 4 5 9
Sample output
Sample Output
43
54
Data range and Tips
Data Size & Hint
The classic interval dynamic programming.
Idea: The process of finding the minimum value is basically the same as the process of finding the maximum value
wikioi 3657 Bracket sequenceTitle Description
Description
We define a valid parenthesis sequence using the following rules:
(1) The empty sequence is legal
(2) If s is a valid sequence, then (s) and [s] are both legal
(3) If both A and B are legal, then AB and BA are also legal
For example, the following is a valid parenthesis sequence:
(), [], (()), ([]), ()[], ()[()]
The following is an illegal bracket sequence:
(, [, ], )(, ([]), ([()
Now given some sequences consisting of ' (', ') ', ' [', ', '] ', add as few parentheses as possible to get a valid parenthesis sequence.
Enter a description
Input Description
Enter the include number sequence S. Contains up to 100 characters (four characters: ' (', ') ', ' [' and '] '), all on one line, with no other extra characters in the middle.
Output description
Output Description
The minimum number of parentheses is required to make the parentheses sequence s a valid sequence.
Sample input
Sample Input
([()
Sample output
Sample Output
2
Data range and Tips
Data Size & Hint
The sample description adds a minimum of 2 parentheses to get a valid sequence: () [()] or ([()]) The length of the "data range" s <=100 (up to 100 characters).
Ideas:
Length division, state left and right end, dp[i][j] = dp[i+1][j-1] + 1 (if I matches to J)
Code:
1#include <iostream>2#include <cstdio>3#include <string>4#include <cstring>5#include <algorithm>6 7 using namespacestd;8 Const intMAXN = $, Maxnum =20000000;9 intn,c,value[maxn],dp[maxn][maxn],sign,j,q;Ten Charcmd; One strings; A intMain () { -n = c =0; - getline (cin,s); theQ =s.length (); - for(inti =1; I <= q;i++){ -cmd = s[i-1]; - if(cmd = ='(') {Value[i-c] =1; n++;} + if(cmd = ='[') {Value[i-c] =2; n++;} - if(cmd = =']') {Value[i-c] =3; n++;} + if(cmd = =')') {Value[i-c] =4; n++;} A } atMemset (DP,0,sizeof(DP)); - for(inti =1; I <= n;i++) dp[i][i] =1; - for(intL =2; l <= n;l++){ - for(inti =1; I <= n-l +1; i++){ -j = i + L-1; -DP[I][J] =Maxnum; in if(Value[i] + value[j] = =5&& Value[i] < value[j]) dp[i][j] = dp[i+1][j-1]; - for(intK = I;k < j;k++){ toDp[i][j] = min (dp[i][j],dp[i][k] + dp[k+1][j]); + } - the } * } $cout<<dp[1][n];Panax Notoginseng return 0; -}View Code
Dynamic planning--interval