[tyvj1045] The biggest formula

Source: Internet
Author: User

The description of the 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 result required
Final Results <=maxlongint test sample 1 input
5 2
1 2 3) 4 5
Output
-
Note for 30% of data, n<= 10;
For all data, N <= 100. Analysis

Since parentheses can be arbitrarily added, the most straightforward idea is to divide a sentence into left and right two segments, which are recursively solved by these two paragraphs. Due to the nature of the inequalities, it is best to combine the left and right two segments to the maximum value (a>c>0, B>d>0⇒a+b>c+d, a*b>c*d).

Set F (L, R, K) to add the maximum value of k multiplication sign to the number on [L, R], then the equation:

F (L, r, k) = A[l], l=r;

F (L, r, k) = max{max{f (l, D, K ') + f (d+1, K-k ')} (L≤d<r, 0≤k ' ≤k, K ' ≤d-l, K-k ' ≤r-d-1), Max{f (L, D, K ') + f (d+1, K-k '-1) } (L≤d<r, 0≤k ' ≤k, K ' ≤d-l, K-k ' ≤r-d)}

Of course, it would be more complicated to enumerate the number of multiplication sign and simplify it.

Set F (i, j) to add the maximum value of J multiplication sign for the first I number, then the equation:

F (i, j) = Max{f (k, J) + (S[i]-s[k] + a[k]), F (k, j-1) * (S[i]-s[k] + a[k])} (0<k<i)

Code
1#include <iostream>2 using namespacestd;3UnsignedLong Longf[102][102];4UnsignedLong Longs[102];5UnsignedLong Longa[102];6UnsignedLong Longans;7 intN, K;8 intMain ()9 {TenCIN >> N >>K; One      for(inti =1; I <= N; ++i) ACIN >>A[i]; -S[n] =A[n]; -      for(inti = N-1; I >=1; --i) theS[i] = s[i +1] +A[i]; -f[1][0] = a[1]; -      for(inti =2; I <= N; ++i) { -          for(intj =0; J! = I && j <= K; ++j) { +              for(intK =1; K! = i; ++k) { -F[I][J] = max (F[k][j] + (s[k +1]-S[i] +A[i]), f[i][j]); +                 if(F[i][j] >ans) AAns =F[i][j]; at             } -         } -          for(intj =1; J! = I && j <= K; ++j) { -              for(intK =1; K! = i; ++k) { -F[I][J] = max (F[k][j-1] * (S[k +1]-S[i] +A[i]), f[i][j]); -                 if(F[i][j] >ans) inAns =F[i][j]; -             } to         } +     } -cout <<F[n][k]; the     return 0; *}

[tyvj1045] 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.