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

  1. 3 1 3 2
  2. 4 1 2 3 4
  3. 4 3 3 2 1
  4. 0
  1. Sample Output
  2. 4
  3. 10
  4. 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.。。。(手打真幸苦)

题解如下

  1. #include<iostream>
  2. #include<string.h>
  3. #include<algorithm>
  4. using namespace std;
  5. int n;
  6. int dp[1005];
  7. int ar[1005];
  8. int max_val = -1;
  9. int main()
  10. {
  11. while(cin>>n && n)
  12. {
  13. for(int i = 0;i < n;i ++)
  14. cin>>ar[i];
  15. memset(dp,0,sizeof(dp));
  16. int ans = 0;
  17. for(int i = 0;i < n;i ++)
  18. {
  19. dp[i] = ar[i];
  20. for(int j = 0;j < i;j ++)
  21. {
  22. if(ar[j] < ar[i])
  23. {
  24. dp[i] = max(dp[i] , dp[j] + ar[i]);
  25. }
  26. }
  27. ans = max(ans , dp[i]);
  28. }
  29. cout<<ans<<endl;
  30. }
  31. return 0;
  32. }

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. centos7搭建ceph集群

    一.服务器规划 主机名 主机IP 磁盘配比 角色 node1 public-ip:10.0.0.130cluster-ip:192.168.2.130 sda,sdb,sdcsda是系统盘,另外两块数 ...

  2. Cisco asa组建IPSEC for ikev1

    IPSec的实现主要由两个阶段来完成:--第一阶段,双方协商安全连接,建立一个已通过身份鉴别和安全保护的通道.--第二阶段,安全协议用于保护数据的和信息的交换. IPSec有两个安全协议:AH和ESP ...

  3. 基于VR三维全景的虚拟展馆展览实现

    VR三维全景虚拟现实技术的应用,能够通过全方位互动式来还原真实场景,令人产生一种身临其境的感觉,由于三维全景虚拟现实技术具有一定应用优势,其在企业与院校展示.建筑规划展示.酒店宾馆展示等方面都逐步得到 ...

  4. 手机抓包app在python中使用

    使用python+airtesr+无线模式控制手机 官方文档中,在airtest.readthedocs.io/zh_CN/lates…有一段介绍如何连接安卓手机的例子: 但是这个线接模板,无线模式的 ...

  5. EF6.0 下sql语句自动生成的参数类型decimal(18,2)修改

    很多时候我们需要对插入到数据库的数据的精度做一个控制,例如sql server下保留6位小数使用numeric(10,6) .而到c#里对应的数据类型就是decimal ,但是使用EF6.0的crea ...

  6. 搭建私有 Nuget 服务器教程(1)

    对于 .NET 开发者来说,nuget 是必不可少的程序包管理工具.相应地,大部分开发团队都需要在内部搭建 Nuget 服务器,以管理私有 nupkg 包.本教程所使用的 Nuget 服务器,不是微软 ...

  7. 测试必知必会系列- Linux常用命令 - tar

    21篇测试必备的Linux常用命令,每天敲一篇,每次敲三遍,每月一循环,全都可记住!! https://www.cnblogs.com/poloyy/category/1672457.html 压缩一 ...

  8. GoJS学习笔记 (转)

    目录 基础概念 开始绘制图形 1. 通过代码构建图形 2. 通过 GraphObject.make 构建图形 3. 使用 Model 和 Templates 创建图形 获取图形数据 获取所有 Node ...

  9. Python之操作文件和目录

    Python内置的os模块可以直接调用操作系统提供的接口函数. # coding=utf-8 # 在指定目录以及指定目录的所有子目录下查找文件名包含指定字符串的文件,并打印出相对路径 import o ...

  10. C# 基础知识系列- 3 集合数组

    简单的介绍一下集合,通俗来讲就是用来保管多个数据的方案.比如说我们是一个公司的仓库管理,公司有一堆货物需要管理,有同类的,有不同类的,总而言之就是很多.很乱.我们对照集合的概念对仓库进行管理的话,那么 ...