D - Super Jumping! Jumping! Jumping!

Nowadays, a kind of chess game called “Super Jumping! Jumping!
Jumping!” is very popular in HDU. Maybe you are a good boy, and know
little about this game, so I introduce it to you now.

The game can be played by two or more than two players. It consists of
a chessboard(棋盘)and some chessmen(棋子), and all chessmen are marked by
a positive integer or “start” or “end”. The player starts from
start-point and must jumps into end-point finally. In the course of
jumping, the player will visit the chessmen in the path, but everyone
must jumps from one chessman to another absolutely bigger (you can
assume start-point is 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 you can straightly
get to end-point from start-point. Of course you get 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. Input Input contains multiple test cases. Each test case is
described in a line as follow: N value_1 value_2 …value_N It is
guarantied that N is not more than 1000 and all value_i are in the
range of 32-int. A test case starting with 0 terminates the input and
this test case is not to be processed. Output For each case, print
the maximum according to rules, and one line one case.
Here Insert Picture Description

Sample Input

3 1 3 2
4 1 2 3 4
4 3 3 2 1
0
Sample Output
4
10
3

Meaning of the questions below

The problem: given a sequence, asked in this sequence 递增的最大的自序和是什么,

Ideas are as follows

This problem with the state transition equation of dynamic programming is difficult to understand: So I use the sample in question 1

1 3 2Sequence to simulate the state transition process:
first show how the core code
for (int I =. 1; I <= n-; I ++)
{
DP [I] Ar = [I];
for (int. 1 = J; J < I; J ++)
{
IF (Ar [J] <Ar [I])
{
DP [I] = max (DP [I], DP [J] Ar + [I]);
}
}
ANS = max (ANS , DP [i]);
}
in the interpretation of some variables meaning:
DP [i] when only the first i number of times, we can take a maximum increment subsequence and
Ar [i] in the sequence value of the i th element
ans requires the final best answer
When the simulation to instill what you understand this problem a few: First, we can just start in ar [i] point of each element is a minimal sequence, but this last element of the sequence is the ar [ i] (in this sub-sequence is the most important thing is the last child of a sequence of values, by which the last value, we can determine what elements can be added to the end edge of the string), and this is increasing subsequence DP [i], we may be the i-th element of Ar [i], the Ar [i] into 1 ~ i - on any one element ar [j] between 1, as long as ar [i]> ar [j ] on, but there may be several j in line with the meaning of problems, so it is best to choose by max function that the ar [i] to place up so it will generate a new sequence, and sequences we have generated this new most places a value ar [i] in the (in fact ar [i] value will not change because, as the additive element toward that sub-sequence, then the last element of a sequence is necessarily the element) If not the ar [I] is added to a subsequence (ar [1] ~ ar [i - 1] (⚠️ in ar It is stored to the last element of a sequence, and using this last element corresponding to dp [1] ~ dp [i - 1] to represent the entire sequence) that ar [i] value will not change the sequence or it . there itself for each new bit sequence generated ar [i] (representative of the sequence) before will not affect ar [1] ~ ar [i - 1] represented by the sequence

Analog Start:
When the time i = 1: we need only consider the first element in the sequence given 1, this time by dp [1] = ar [1 ] is assigned to the DP 1 to [1], when the inner performed for cycle time due to the j <i
does not hold is not performed for the inner loop, then continue down this time the optimal solution ans = 1, so in this case there is only one sub-sequence "1."
When the time point i = 2: Can we need to consider the number of the second 3into the first number of the upper, may be placed if the upper (if ar [2]> ar [1]) then this will produce a new the sequence "1, 2", then the child may not be placed on the sequence before so i before i - 1 Ge is to hold the results ar [i] generated. This time there are ideas sequences ar "1" sequence represented, ar "1, 2" sequence represented by [1] [2],

When i = 3 when: Since the beginning of the assignment only a subsequence of an element of itself "2", and then in the second layer for paging traversing the point when we can consider in order to put 2 ar [1] "1" sequence represented, of a sub-sequence Ar [3] represented by "1, 2", since the Ar [3] is greater than the Ar [2] is a point represented by the sequence "1, 3" last element 3 so in this case can not ar [3] put up, the best final sequence generated by this process is taking max
"1, 2", and so have to ar [3] were written as "1, 2" sequence
comprehensive on: the sub-sequence generated from i is equal to the process 1 to 3, there is "1" sequence to Ar [1] represented, "1, 3" sequence ar [2] represented to Ar [3] "1, 2" sequence represented last dp [] decided to take the size of the value that increasing a newly created sub-sequence based.
So the end result is this: to ar [2] represented by the sequence "1, 3", and dp [2] just stands for "1, 3" and 4 increasing sequence .. . . (Hand really fortunate bitter)

Solution to a problem as follows

#include<iostream>
#include<string.h>
#include<algorithm>

using namespace std;

int n;
int dp[1005];
int ar[1005];

int max_val = -1;
int main()
{
    while(cin>>n && n)
    {
        for(int i = 0;i < n;i ++)
            cin>>ar[i];
        memset(dp,0,sizeof(dp));
        int ans = 0;
        for(int i = 0;i < n;i ++)
        {
            dp[i] = ar[i];
            for(int j = 0;j < i;j ++)
            {
                if(ar[j] < ar[i])
                {
                    dp[i] = max(dp[i] , dp[j] + ar[i]);
                }
            }
            ans = max(ans , dp[i]);
        }
        cout<<ans<<endl;
    }

    return 0;
}
Published 73 original articles · won praise 100 · Views 2694

Guess you like

Origin blog.csdn.net/qq_34261446/article/details/103877126