Algorithm training the biggest formula

Source: Internet
Author: User

algorithm Training the biggest formulatime limit: 1.0s memory limit: 256.0MBProblem 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------------------------------------------------

Ideas:

DP[I][J] indicates The maximum value of J multiplication sign for the number of previous I.

Dp[i][j]=max (Dp[i][j],dp[k-1][j-1]*sum (k,i)); K is Any position in the number of previous I.

---------------------------------------------------------------
ImportJava.util.Scanner; Public classMain { Public Static voidMain (string[] args) {//TODO auto-generated Method Stub        inta[]=New int[120]; Longsum[]=New Long[120];//Sum[i] Indicates the sum of the number of previous I        Longf[][]=New Long[120] [120];//Dp[i][j] Indicates the maximum value when J multiplication sign is in the first I number        intn,k; sum[0]=0; Scanner SC=NewScanner (system.in); N=Sc.nextint (); K=Sc.nextint ();  for(inti=1;i<=n;i++) {A[i]=Sc.nextint (); Sum[i]=sum[i-1]+A[i]; }         for(inti=1;i<=n;i++) f[i][0]=Sum[i];  for(inti=0;i<=n;i++){             intt = math.min (i-1, K);  for(intj=1;j<=t;j++) {//number of control multiply                 for(intk=2;k<=i;k++){                    LongS=SUM[I]-SUM[K-1];//back I-(k-1) number of and, K to <=iF[i][j]=math.max (f[k-1][j-1]*s, f[i][j]);//J indicates the number of products, f[k-1][j-1] to multiply the last is J multiplication sign, K number}}} System.out.println (F[n][k]); }}

Algorithm training 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.