"Algorithmic Learning Notes" 85. Broken loop for chain sequence DP relaxation + cost SJTU OJ 1073 Energy Necklace

Source: Internet
Author: User

Like a stone merge, in order to process the ring, we can copy the input data one after the other. In this way, the final result of the lifting point to find the largest.

Pay attention to the calculation of the price here, because our Data[i] only records the head of the bead and the tail of the beads is the head of the next bead.

Because it is necessary to calculate dp[i][j]

Used to dp[i][k] K is smaller than J so J order

Dp[k][j] K ratio i large so I reverse order

K Insert can be

 for (int2*n-1; I >=1 ;--i) {    for (int2*n ; + +J)        {0;          for (int k = i; k < J; + +k             ) = Max (Dp[i][j], dp[i][k] + dp[k+1][j] +  zz[i]*zz[k+1]*zz[j+1]);}     }

Or it's easier to write with Len's way of traversing.

 for(intLen =1; Len <2*n; ++Len) {     for(inti =1; I <=2*n-len; ++i) {intj = i +Len; DP[I][J]=0;  for(intK = i; K < J; ++k) Dp[i][j]= Max (Dp[i][j], dp[i][k]+dp[k+1][J] + zz[i]*zz[k+1]*zz[j+1]); }}

"Algorithmic Learning Notes" 85. Broken loop for chain sequence DP relaxation + cost SJTU OJ 1073 Energy Necklace

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.