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.
在这里插入图片描述

Sample Input

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

题意如下

这一题:给一个序列,问在这个序列中递增的最大的自序和是什么,

思路如下

这题用动态规划中的 状态转移方程是难以理解:那么我就用该题样例1中的

1 3 2 序列来模拟 这个 状态转移的过程:
先展示一下核心代码
for(int i = 1;i <= n;i ++)
{
dp[i] = ar[i];
for(int j = 1;j < i;j ++)
{
if(ar[j] < ar[i])
{
dp[i] = max(dp[i] , dp[j] + ar[i]);
}
}
ans = max(ans , dp[i]);
}
在解释一些变量的意思:
dp[i] 当只有前 i 个数的时候,我们能取到最大递增子序和
ar[i] 序列中第 i 个元素的值
ans 要求的最终的最优答案
在模拟的时候给你么灌输一个对这个题一些理解:首先我们 可以刚开始在ar[i]的点每个元素都是一个最小的子序列,只不过这个子序列的最后一个元素是ar[i]( 而在这个子序中最重要的就是 子序列的最后一个值,通过这最后一个值,我们可以确定什么元素是可以 添加到这个字串的最后边),这个递增的子序和是dp[i],我们可以将第i个元素ar[i],将ar[ i ] 放到 1~i - 1 之间的任何一个元素ar[j]上,只要ar[i] > ar[j]上,但是可能有好几个 j 符合题意,所以所以要通过max函数选取最优的那个把ar[ i ]放置上去这样就会产生一个新的子序列,而我们把这个新产生的子序列的最有一个值放到 ar[i]中(其实ar[i ] 的值是不会变的因为,因为往那个子序中添加元素,则这个子序列的最后一个元素就一定是这个元素),如果不把ar[i] 添加到某个子序列中(ar[1] ~ ar[i - 1](⚠️在ar中存到是某个序列的最后一个元素,用这个最后一个元素以及对应的dp[1 ] ~dp[i - 1]来代表整个序列) 那ar[ i ]序列的值是不会变的还是它本身。还有一点每个新产生的子序列ar[ i ](所代表子序列)是不会影响 之前ar[1] ~ar[i - 1] 所代表的子序列

模拟开始:
当 i = 1 的时候:我们只需要考虑所给序列中的 第一个元素 1 ,这个时候通过 dp[1] = ar[1] 把 1 赋值给了dp[1],当执行内层for循环时候 由于 j < i
不成立,所以不执行内层for循环,继续向下执行然后 此时最优解 ans = 1 ,所以此时只有一个子序列 「 1 」。
当 i = 2 点时候:我们需要考虑 能不能把 第 2 个数3放到 第一个数字的上边,如果可以放到上边(if ar[2] > ar[1])那么此就会产生一个新的 子序列「1 ,2」,那么也可以不放到 i 之前的 子序列上 那么前 i - 1 个就是不放ar[ i ] 所产生的结果。这个时候有点子序列有 ar[1 ]所代表的 「 1 」 序列,ar[2] 所代表的「 1 ,2 」子序列,

当 i = 3的时候:由于赋值刚开始的时 有一个仅有自身一个元素的子序列 「 2 」,然后在第二层for寻呼遍历点时候我们可以考虑把 2 放到以ar[1 ] 为代表的「 1 」子序列中,产生一个以ar[ 3 ]为代表的子序列 「 1 , 2」,由于ar[ 3 ] 大于ar[ 2 ]为代表的点序列「1 ,3 」最后一个元素3 所以此时不能把ar[ 3 ]放上去, 最终通过max 取这个过程产生的最优序列为
「1 ,2」,所以有了 以ar[3 ] 为代笔的「 1 ,2」序列
综上:从 i 等于 1 ~ 3的过程中产生的子序列有 以ar[1 ] 为代表的 「 1 」子序列 、以ar[2 ]为代表的 「1 , 3 」序列 、 以 ar[ 3 ] 为代表的「 1 ,2」序列 ,最后根据dp[]值的大小决定要取那一个新产生的递增的子序列序列的 。
那么最后的结果就是:以ar[2]为代表的子序列「 1 ,3」,而dp[ 2 ]正好代表 「 1, 3」递增序列的和 4.。。。(手打真幸苦)

题解如下

#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;
}
发布了73 篇原创文章 · 获赞 100 · 访问量 2694

猜你喜欢

转载自blog.csdn.net/qq_34261446/article/details/103877126
今日推荐