Energy necklace -- Zhongshan No. 1 Middle School Co., OJ_23--DP.

Source: Internet
Author: User

Address: http://zsyz.openjudge.cn/dp/23/

/* Author: bob lee2012.9.20 ======================================== ============= this is a Chinese question, I don't need to explain it. This is an interval DP. We use F [I] [J] to represent the maximum energy value from I to J. Then its subproblem is to divide I to J into two parts. then, the maximum value of the input fields multiplied by the input values can be used to export the state transition equation f [I] [J] = max (F [I] [k] + F [k + 1] [J] + data [i] * Data [k + 1] * Data [J + 1]) (1 <= I <= k <j <= 2n-1) where data [I] indicates the header of the I, And I + 1 is the end, that is, this necklace is a ring, so the head is not necessarily 1, so we are in 1 ~ N plus one so we only need to enumerate f [I] [n + I-1] (1 <= I <= N) obtain the maximum value */# include <iostream> # include <cstdio> # include <cstdlib> using namespace STD; # define maxn 105 # define INF 1 <30int f [2 * maxn] [2 * maxn]; int data [2 * maxn]; int N; void Update (Int & X, int y) {If (Y> X) x = y;} int main () {// while (scanf ("% d ", & N )! = EOF) scanf ("% d", & N); {int I, j; for (I = 1; I <= N; I ++) {scanf ("% d", & Data [I]); Data [I + N] = data [I];} for (I = 1; I <= 2 * n; I ++) {for (j = 1; j <= 2 * n; j ++) {if (I = J) f [I] [J] = 0; else f [I] [J] = 0 ;}} int Len; For (LEN = 1; Len <n; Len ++) {for (I = 1; I <= 2 * n-len; I ++) {J = I + Len; int K; For (k = I; k <J; k ++) {Update (F [I] [J], f [I] [k] + F [k + 1] [J] + data [I] * Data [k + 1] * Data [J + 1]);}} int ans =-INF; for (I = 1; I <= N; I ++) {Update (ANS, F [I] [n + I-1]); // cout <F [I] [n + I-1] <Endl;} printf ("% d \ n", ANS);} return 0 ;}

Related Article

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.