Blue Bridge Cup algorithm training ALGO-116 the biggest formula

Source: Internet
Author: User

Algorithm training maximum calculation time limit: 1.0s memory Limit: 256.0MB Problem description topic is very simple, give n numbers, do not change their relative position, in the middle add K multiplication sign and n-k-1 a plus, (parentheses arbitrarily add) to make the final result as large as possible. Because multiplication sign and the plus sign are all N-1, there is a sign between each of the two adjacent numbers. For example:
n=5,k=2,5 numbers are 1, 2, 3, 4, 5, respectively, can be added:
1*2* (3+4+5) =24
1* (2+3) * (4+5) =45
(1*2+3) * (4+5) =45
...... Input format input file a total of two lines, the first behavior two spaces separated by an integer, representing N and K, where (2<=n<=15, 0<=k<=n-1). The second behavior is n a number separated by spaces (each number is between 0 and 9). Output format output file only one row contains an integer representing the maximum required result sample Input 5 2
1 2 3 4 5 Sample Output 120 example description (1+2+3) *4*5=120

Topic Analysis:

The subject involves an algorithm--dynamic programming.

(1) Dynamic planning ideas

   In the process of divide-and-conquer, some sub-problems have been repeatedly computed many times. If you can save the answers to the solved sub-problems, and then find them when needed, we can avoid the computation of a lot of repetitive problems and get the polynomial time algorithm. (2) Steps to design dynamic planning   ① to find out the properties of the optimal solution and characterize its structure; ② defines the optimal value recursively (writes out the dynamic programming equation); ③ calculates the optimal value (fill in the table) in the bottom-up way; ④ constructs an optimal solution based on the information obtained when calculating the optimal value. Note: A. Step ①~③ is the basic step of dynamic programming algorithm; B. In the case where only the optimal value is required, step ④ can be omitted, and if an optimal solution is required, then step ④ must be taken.
(3) Characteristics of dynamic programming ① Optimal sub-structure when the optimal solution of the problem contains the optimal solution of its sub-problem, it is said that the problem has the optimal substructure property. ② Overlapping sub-problems when using recursive algorithm to solve the problem from top to bottom, the sub-problem is not always a new problem, and some sub-problems are counted repeatedly. The dynamic programming algorithm takes advantage of the important properties of this seed problem, solves each sub-problem only once, then saves the solution in a table and uses the sub-problems as much as possible later. taking the sample input given in the topic as an example, the dynamic programming algorithm is analyzed: (1) The sum of the number of the first I is saved using the sums array.         (2) using a DP array to hold the maximum value of the previous I number with 0 multiplication sign (full overtime value, same as Sum array), i.e. dp[i][0];      (3) In the dynamic programming algorithm, starting with the number of the second (I starting from 2) and adding multiplication sign, the first number of cyclic cumulative i-1 (J starting from 1, to the end of the i-1, and cannot be greater than k) multiplication sign, the multiplication sign position cycle from the position after the first number to the position of the number I before (p from 2 Start, to I-end);
Step 1:i = 2 j = 1 p = 2 Description: First two digits, one multiplication sign, position in front of the second number dp[2][1] = 0 Description: When the first two numbers are one multiplication sign, the value is 0 (table 1.2 in dp[2][1]) dp[1][0] X (sum[2]-sum[1]) = 2 Description: The previous number has no multiplication sign by the sum of the preceding two digits minus the sum of the previous number, that is, the previous number multiplied by the second number (1*2 = 2) dp[2][1] = max (0, 2) Description: fill in the DP table          Step 2:i = 3 J = 1 p = 2 Description: First three digits, one multiplication sign, position in front of the second number dp[3][1] = 0 Description: When the first three numbers are one multiplication sign, the value is 0 (table 1.2 in dp[3][1]) dp[1][0] X (sum[3]-sum[1]) = 2 Description: The previous number has no multiplication sign by the sum of the preceding three numbers minus the sum of the previous number multiplied by the first two numbers (1* (2+3) = 5) dp[3][1] = max (0, 5) Description: fill in the DP table             Step 3:i = 3 J = 1 p = 3 Description: First three digits, one multiplication sign, position in front of the third number dp[3][1] = 5 Description: When the first three numbers are one multiplication sign, the value is 0 (table 1.2 in dp[3][1]) dp[2][0] X (sum[3]-sum[2]) = 9 Description: The first two numbers do not multiplication sign by the sum of the preceding three numbers minus the sum of the first two numbers, that is, the sum of the previous number multiplied by the third number ((1+2) * 3) = 9) dp[3][1] = max (5, 9) Description: fill in the DP table          ... only to the end of all loop execution, altogether 19 steps. The final result of the DP table is:          When 5 numbers have 2 multiplication sign, the maximum value should be dp[5][2] = 120. In the loop execution, we don't have to worry about Dp[p-1][j-1] * (Sum[i]-sum[p-1]) is the result of those few numbers, and use its value is OK, this is one of the most important features of dynamic planning! Example code:
1 ImportJava.io.BufferedReader;2 Importjava.io.IOException;3 ImportJava.io.InputStreamReader;4 5  Public classMain {6      Public Static voidMain (string[] args)throwsIOException {7BufferedReader br =NewBufferedReader (NewInputStreamReader (system.in));8string[] str = Br.readline (). Split ("");9         intn = integer.parseint (str[0]);Ten         intK = Integer.parseint (str[1]); One          A         Long[] DP =New Long[N+1] [K+1];//Dp[i][j] Indicates the maximum value when J multiplication sign is in the first I number -         int[] sum =New int[N+1];//sum of the number of previous I -          thestr = Br.readline (). Split (""); -          for(inti = 1; I <= N; i++) { -Sum[i] = sum[i-1] + integer.parseint (str[i-1]); -         } +          -         //there is no multiplication sign situation, that is, the case of adding +          for(inti = 1; I <= N; i++) { ADp[i][0] =Sum[i]; at         } -         //Dynamic Planning -          for(inti = 2; I <= N; i++) {//number of previous I -              for(intj = 1; J <= I-1 && J <= K; J + +) {//number of multiplication sign -                  for(intp = 2; P <= i; p++) {//location of the multiplication sign -DP[I][J] = max (Dp[i][j], dp[p-1][j-1] * (Sum[i]-sum[p-1]));//maximum value of j multiplication sign for the number of first I in                 } -             } to         } +          - System.out.println (Dp[n][k]); the     } *  $     /**Panax Notoginseng * Maximum number -      * @parama parameter 1 the      * @paramB Parameter 2 +      * @returnthe maximum number in a B A      */ the     Private Static LongMaxLongALongb) { +         returnA>b?a:b; -     } $}

Blue Bridge Cup algorithm training ALGO-116 the biggest formula

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.