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;
}
D - Super Jumping! Jumping! Jumping!的更多相关文章
- HDU - 1087 Super Jumping!Jumping!Jumping!(dp求最长上升子序列的和)
传送门:HDU_1087 题意:现在要玩一个跳棋类游戏,有棋盘和棋子.从棋子st开始,跳到棋子en结束.跳动棋子的规则是下一个落脚的棋子的号码必须要大于当前棋子的号码.st的号是所有棋子中最小的,en ...
- E - Super Jumping! Jumping! Jumping!
/* Nowadays, a kind of chess game called "Super Jumping! Jumping! Jumping!" is very popula ...
- Super Jumping! Jumping! Jumping!
Nowadays, a kind of chess game called “Super Jumping! Jumping! Jumping!” is very popular in HDU. May ...
- DP专题训练之HDU 1087 Super Jumping!
Description Nowadays, a kind of chess game called "Super Jumping! Jumping! Jumping!" is ve ...
- hdu 1087 Super Jumping! Jumping! Jumping! 简单的dp
Super Jumping! Jumping! Jumping! Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 ...
- Super Jumping! Jumping! Jumping!——E
E. Super Jumping! Jumping! Jumping! Time Limit: 1000ms Memory Limit: 32768KB 64-bit integer IO forma ...
- HDU 1087 Super Jumping! Jumping! Jumping! 最大递增子序列
Super Jumping! Jumping! Jumping! Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 ...
- HDU 1087 Super Jumping! Jumping! Jumping
HDU 1087 题目大意:给定一个序列,只能走比当前位置大的位置,不可回头,求能得到的和的最大值.(其实就是求最大上升(可不连续)子序列和) 解题思路:可以定义状态dp[i]表示以a[i]为结尾的上 ...
- HDU 1087 Super Jumping! Jumping! Jumping! (DP)
C - Super Jumping! Jumping! Jumping! Time Limit:1000MS Memory Limit:32768KB 64bit IO Format: ...
- HDU 1087 Super Jumping! Jumping! Jumping!(动态规划)
Super Jumping! Jumping! Jumping! Problem Description Nowadays, a kind of chess game called “Super Ju ...
随机推荐
- python学习记录_中断正在执行的代码,执行剪切板中的代码,键盘快捷键,魔术命令,输入和输出变量,记录输入和输出变量_
2018-03-28 00:56:39 中断正在执行的代码 无论是%run执行的脚本还是长时间运行的命令ctrl + cIn [1]: KeyboardInterrupt 执行剪切板中的代码 ctrl ...
- Java Opencv 实现锐化
§ Laplacian() void cv::Laplacian ( InputArray src, O ...
- 最新版jdk 13环境变量配置
1.配置环境变量 右击“我的电脑”-->"属性"-->"高级系统设置"-->"高级"-->"环境变量&qu ...
- 一个和与后台数据连接的模板get post put 以及延伸的query
/* example: require.config({ paths: { "httpClient": "../../core/http-client" } } ...
- volatile 手摸手带你解析
前言 volatile 是 Java 里的一个重要的指令,它是由 Java 虚拟机里提供的一个轻量级的同步机制.一个共享变量声明为 volatile 后,特别是在多线程操作时,正确使用 volatil ...
- iOS开发技术 - 二维码扫描、生成
QRecLevel:QR_ECLEVEL_H // 二维码容错率,最高为30%(即QR_ECLEVEL_H),即LOGO有大 ...
- 114 Flatten Binary Tree to Linked List [Python]
114 Flatten Binary Tree to Linked List Given a binary tree, flatten it to a linked list in-place. 将二 ...
- Selenium IDE安装及简介
一.Selenium IDE安装 Selenium IDE是Firefox浏览器的一个插件,依附于Firefox浏览器.在网上搜了Selenium IDE的安装教程,大部分都是说在官网下载安装,其实最 ...
- python基础知识6——函数
函数:自定义函数:函数的参数:不带参数,普通参数,默认参数,动态参数:返回值return:函数作用域:内置函数高阶函数:map,reduce,filter,sorted:lambda表达式:文件操作: ...
- 初探elasticsearch
目录 安装elasticsearch elasticsearch中的层级结构与关系型数据库的对比 elasticsearch的分布式特性 集群和节点 为java用户提供的两种内置客户端 节点客户端(n ...
