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;
}

D - Super Jumping! Jumping! Jumping!的更多相关文章

  1. HDU - 1087 Super Jumping!Jumping!Jumping!(dp求最长上升子序列的和)

    传送门:HDU_1087 题意:现在要玩一个跳棋类游戏,有棋盘和棋子.从棋子st开始,跳到棋子en结束.跳动棋子的规则是下一个落脚的棋子的号码必须要大于当前棋子的号码.st的号是所有棋子中最小的,en ...

  2. E - Super Jumping! Jumping! Jumping!

    /* Nowadays, a kind of chess game called "Super Jumping! Jumping! Jumping!" is very popula ...

  3. Super Jumping! Jumping! Jumping!

    Nowadays, a kind of chess game called “Super Jumping! Jumping! Jumping!” is very popular in HDU. May ...

  4. DP专题训练之HDU 1087 Super Jumping!

    Description Nowadays, a kind of chess game called "Super Jumping! Jumping! Jumping!" is ve ...

  5. hdu 1087 Super Jumping! Jumping! Jumping! 简单的dp

    Super Jumping! Jumping! Jumping! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 ...

  6. Super Jumping! Jumping! Jumping!——E

    E. Super Jumping! Jumping! Jumping! Time Limit: 1000ms Memory Limit: 32768KB 64-bit integer IO forma ...

  7. HDU 1087 Super Jumping! Jumping! Jumping! 最大递增子序列

    Super Jumping! Jumping! Jumping! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 ...

  8. HDU 1087 Super Jumping! Jumping! Jumping

    HDU 1087 题目大意:给定一个序列,只能走比当前位置大的位置,不可回头,求能得到的和的最大值.(其实就是求最大上升(可不连续)子序列和) 解题思路:可以定义状态dp[i]表示以a[i]为结尾的上 ...

  9. HDU 1087 Super Jumping! Jumping! Jumping! (DP)

    C - Super Jumping! Jumping! Jumping! Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format: ...

  10. HDU 1087 Super Jumping! Jumping! Jumping!(动态规划)

    Super Jumping! Jumping! Jumping! Problem Description Nowadays, a kind of chess game called “Super Ju ...

随机推荐

  1. 前端开发--Mongodb篇

    安装和启动 安装 官方安装文档 本地mac Os推荐使用Homebrew ⚠️ 目前直接使用--brew install mongodb-- 安装 mongodb 时提示:Error: No avai ...

  2. Flink系列之状态及检查点

    Flink不同于其他实时计算的框架之处是它可以提供针对不同的状态进行编程和计算.本篇文章的主要思路如下,大家可以选择性阅读. 1. Flink的状态分类及不同点. 2. Flink针对不同的状态进行编 ...

  3. 【Weiss】【第03章】链表例程

    这种基础例程,如之前所提,会有一个实现和一个简单的测试代码. 链表其实没什么可说的,其实包括后面的栈和队列也没什么可说的,直接放代码吧. 下面这个是测试代码 #include <iostream ...

  4. 免注册公众号的三种微信推送消息服务的C#代码实现

    有时候我们需要监控一些网络上的变化,但是每次去刷新网页却又很麻烦,而且大部分刷新的时候网页并没有更新.那么有没有一个工具,可以监控网页变化,并将变化的结果推送到手机微信上呢? 这里有很多应用场景,比如 ...

  5. VS2019 C++动态链接库的创建使用(4) - C语言客户端

    前面提到过C++具有函数重载功能,所以引出的函数名会有变化,即名称改编,如果是C语言编写的客户端则无法正确识别. 处理方法: ①只需在宏定义中间增加 extern "C",但这种使 ...

  6. Swift4.1 新特性compactMap函数

    关于compactMap函数 苹果在Swift 4.1中新增compactMap函数,用来代替flatMap函数. 在Swift标准库中compactMap定义如下 public func compa ...

  7. Jsp el表达式无解析 解决

    需要开启 el isELIgnored="false" 这时已经可以正常访问了  

  8. django中的缓存以及跨域

    django中的缓存 先来了解以下问题

  9. centos 安装activeMq

    Apache ActiveMQ是一个免费的开源消息代理和集成模式服务器.它支持来自JAVA.c++.C.Python.Perl.PHP等多种语言的客户端和协议.它提供了许多功能,如消息组.虚拟目的地. ...

  10. 动态规划-Maximum Subarray-Maximum Sum Circular Subarray

    2020-02-18 20:57:58 一.Maximum Subarray 经典的动态规划问题. 问题描述: 问题求解: public int maxSubArray(int[] nums) { i ...