Dynamic Programming-Linear DP

Source: Internet
Author: User

When we solve the optimization problem on some linear interval, we can often use the idea of dynamic programming, which can be called linear DP. In this article, we'll discuss some of the problems with linear DP.

In relation to the linear DP problem, there are several classical and basic models, such as the longest ascending subsequence (LIS), the longest common subsequence (LCS), the maximal subsequence, and so on, so first we start with the exploration of linear DP from these classical problems.

First, let's look at the longest ascending subsequence problem.

The problem is based on the background that for a set of n elements s = {A1, a2, a3......an}, a sub-sequence s ' = {Ai,aj,ak} for S, if Ai<aj<ak is satisfied, then S ' is a ascending sub-sequence of s, then the question now is, In s numerous ascending subsequence, what is the number of elements in the subsequence that contains the most elements? Or what is the maximum length of this ascending sub-sequence?

In accordance with the usual DP thinking, we have the whole problem (this is very important to solve the problem with DP thinking, based on the relationship between the sub-problems we can find the state transition equation), we set the array dp[i] to represent the AI as the ascending subsequence end of the maximum length of the ascending sub-sequence. Then for Dp[i] and dp[i-1], there is the following relationship between them.

if (Ai > Ai-1) dp[i] = Dp[i-1] + 1

else Dp[i] = 1

This is the most basic problem of the longest ascending subsequence, and we continue to experience it through a specific problem. (Problem Source:hdu 1087)

Problem descriptionnowadays, a kind of chess game called "Super jumping! jumping! Jumping! "is very popular in HDU. Maybe you is a good boy, and know little about the this game, so I introduce it to you now. The game can be played by and more than the players. It consists of a chessboard (chessboard) and some chessmen (chess pieces), and all chessmen is marked by a positive integer or "start" or "End ”. The player starts from start-point and must jumps to end-point finally. In the course of jumping, the player would visit the chessmen in the path, but everyone must jumps from one Chessman to Ano Ther absolutely bigger (you can assume start-point are a minimum and end-point is a maximum.). And all players cannot go backwards. One jumping can go from a chessman to next, also can go across many chessmen, and even can straightly get to end-point From Start-point. Of course you get the zero point in this situation. A player is a winner if and only if he can get a bigger score according to his jumping solution. Note that your score comes from the sum of value on the chessmen in you jumping path. Your task is to output the maximum value according to the given Chessmen List. inputinput contains multiple test CASes. Each test case was described in a line as Follow:n value_1 value_2 ... value_n It was guarantied that N was not more than 1000 And all value_i is in the range of 32-int. A test case starting with 0 terminates the-input and this-test case are not-be-processed. outputfor each case, Prin T the maximum according to rules, and one line one case.    title: In summary, it is based on the model of the eldest son sequence problem given above, the oldest sequence is S ', And the total of S contains n elements, to find out the ∑ai, wherein I∈[1,n] and Ai∈s ', that is, the eldest son of the sequence.    Mathematical Analysis: With the basics of the oldest oldest sequence problem, here we just need to do a similar analysis. We set dp[i] to record the and of the oldest sequence of the AI as the end point, and we can get the following state transition equation.                                      Dp[i] = MAX{J|DP [j] + ai} , where j∈[1,i-1] and aj<ai   so complete the calculation of dp[1] to dp[n, we only need to find the maximum output. The    reference code is as follows.  
#include <stdio.h>#include<string.h>#include<algorithm>using namespacestd;Const intMAXN =1005;Const intINF =999999999;intA[MAXN], DP[MAXN];intMain () {intN, m, ans;  while(SCANF ("%d", &n) &&N) {memset (DP,0,sizeof(DP));  for(inti =1; I <= n;i++) scanf ("%d",&A[i]);  for(inti =1; I <= n;i++) {ans= -inf;  for(intj =0; J < I; J + +)                   {                       if(a[i]>A[j]) ans=Max (ans, dp[j]); } Dp[i]= ans +A[i]; } ans= -inf;  for(inti =1; I <= n;i++) ans=Max (ans, dp[i]); printf ("%d\n", ans); }}

Dynamic Programming-Linear DP

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.