Jia's magic wand (vijos 1283)

Source: Internet
Author: User

Question:

A tree branch has n segments, each of which has a score. You can select an interval that is not completely contained (can be intersection). The sum of all numbers in the interval can be obtained each time you select. Calculate the maximum score.

 

Problem solving process:

1. obviously, the default selection interval is from left to right. f [I] [J] indicates that the last selection interval is [I, j] optimal solution (I <= J ). Obviously f [I] [J] = max {f [p] [Q]} + sum [I] [J]. (P <I & q <j );

But the complexity is obviously too high, reaching O (N ^ 4 ).. The equation of this question is easy to think of, and it is rare that it is how to reduce complexity.

2. Considering that the condition of q <J in the equation is well satisfied, taking J as the stage, the value of the phase J-1 can satisfy q <j. Then we use an auxiliary array G [I] [J] To represent max {f [p] [Q]} (P <= I & q <= J ), maintenance G [I] [J] As long as each completion phase J, for I = 1-> j g [I] [J] = max {G [I-1] [J], f [I] [J], G [I] [J-1]};

In this way, the state transition equation becomes f [I] [J] = G [I-1] [J-1] + sum [I] [J]. the complexity is successfully reduced to O (N ^ 2 );

 

Summary: Some DP questions are generally easy to obtain the simplest state transition equation, but the complexity is often too high. Therefore, an auxiliary array needs to be maintained to reduce the complexity.

Jia's magic wand (vijos 1283)

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.