Gravel merger / energy necklace interval [dp]

Topic links: http://www.51mxd.cn/problem.php-pid=737.htm

Title effect: it gives the n th stack of pebbles and stones stack the n number of stones, the operator takes each merge two adjacent stones stack, a consideration of the number of stacks and two stones, gravel heap seeking a final synthesis the minimum cost.

Problem-solving ideas: the typical interval dp

. 1 #include <stdio.h>
 2 #include < String .h>
 . 3 #include <algorithm>
 . 4  #define MEM (A, B) Memset (A, B, the sizeof (A))
 . 5  const  int MAXN = 220 ;
 . 6  const  int INF = 0x3f3f3f3f ;
 . 7  the using  namespace STD;
 . 8  
. 9  int ARR [MAXN], SUM [MAXN];
 10  int DP [MAXN] [MAXN]; // represents the minimum cost in the interval i ~ j 
. 11  
12 is  int main ( )
 13 is  {
 14      int n;
15     while(scanf("%d", &n) != EOF)
16     {
17         mem(dp, 0), mem(sum, 0);
18         for(int i = 1; i <= n; i ++)
19         {
20             scanf("%d", &arr[i]);
21             sum[i] += sum[i - 1] + arr[i];
22         }
23         for(int len = 2; Len <= n-; len ++) // Enumeration length of 
24          {
 25              for ( int I = . 1 ; I len + - . 1 <= n-; I ++) // Enumeration starting section 
26 is              {
 27                  int J = len + I - . 1 ; // interval end 
28                  DP [I] [J] = INF;
 29                  for ( int K = I; K <J; K ++ )
 30                  {
 31 is                      DP [I] [J] = min ( DP [I] [J], DP [I] [K] + DP [K + . 1 ] [J] + SUM [J] - SUM [I - . 1]);
32                 }
33             }
34         }
35         printf("%d\n", dp[1][n]);
36     }
37     return 0;
38 }
View Code

Topic links: https://ac.nowcoder.com/acm/contest/1089/J

Title effect: given a ring necklace, two adjacent each operation synthetic Sarah, Sarah consideration of the first two, the middle, the end product, the final synthesis of seeking a minimum cost spent when Sarah.

Problem-solving ideas:

1. DP interval, breaking the ring into a chain, opening array twice.

 1 #include<stdio.h>
 2 #define LL long long
 3 #include<math.h>
 4 #include<algorithm>
 5 using namespace std;
 6 const int MAXN = 220;
 7 
 8 int a[MAXN];
 9 LL dp[MAXN][MAXN];
10 
11 int main()
12 {
13     int n;
14     scanf("%d", &n);
15     for(int i = 1; I <= n-; I ++ )
 16      {
 . 17          Scanf ( " % D " , & A [I]);
 18 is          A [I + n-] = A [I];
 . 19      }
 20 is      for ( int len = 2 ; len <= n-; len ++) // enumeration length 
21 is      {
 22 is          for ( int I = . 1 ; I len + - . 1 <= 2 * n-; I ++)   // enumeration starting point 
23 is          {
 24              int J = I len + - 1 ;   //终点 
25             for(int k = i; k < j; k ++)
26             {
27                 dp[i][j] = max(dp[i][j], dp[i][k] + dp[k + 1][j] + a[i] * a[k + 1] * a[j + 1]);
28             }
29         }
30     }
31     LL ans = -1;
32     for(int i = 1; i <= n; i ++)
33         ans = max(ans, dp[i][i + n - 1]);
34     printf("%lld\n" , Year);
 35      return  0 ;
 36 }
View Code

 

Guess you like

Origin www.cnblogs.com/yuanweidao/p/11574583.html