Algorithm training the largest formula (DP)

Source: Internet
Author: User

Problem description is 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

:<-----Original link

Dynamic planning, increasing the number of multiplication sign, dp[i][j] is expressed as the maximum value of J multiplication sign for the number of previous I, the position of the J multiplication sign should be discussed at the time of Dp[i][j], if in the K position, then the dp[i][j at this time] Is the first k-1 number has j-1 the maximum value of multiplication sign multiplied by the number of K to J number of sum, and then the dp[i][j] compared to the size, take the maximum (because the position of K has been obtained is the last multiplication sign case, so dp[k-1][j-1] Already represents the maximum value of the previous k-1 number j-1 multiplication sign, directly multiplied by the sum of the remaining numbers)

That is: dp[i][j] = max (dp[i][j],dp[k-1][j-1]* (sum[i]-sum[k)), according to this in turn, note that the number of multiplication sign is less than the number of numbers, and if long long int

#include <stdio.h>#include<string.h>#include<algorithm>using namespacestd;Long Longdp[ -][ -];//Dp[i][j] Represents the maximum value of the first I element J multiplication SignLong Longsum[ -];intMain () {intN, K; intnum; Memset (DP,0,sizeof(DP)); memset (SUM,0,sizeof(sum)); scanf ("%d%d", &n, &k);  for(inti =1; I <= N; i++) {scanf ("%d", &num); Sum[i]= sum[i-1] +num; dp[i][0] =Sum[i]; }     for(inti =2; I <= N; i++)    {         for(intj =1; J <= I-1&& J <= K; J + +)        {             for(intK =2; K <= N; k++)//location of the multiplication sign{Dp[i][j]= Max (Dp[i][j], dp[k-1][j-1]* (sum[i]-sum[k-1])); }}} printf ("%lld\n", Dp[n][k]); return 0;}
View Code

Only hate myself too much water.

Algorithm training the largest formula (DP)

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.