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 ...
随机推荐
- HDFS 客户端读写操作详情
1. 写操作 客户端向namenode发起上传请求 namenode检查datanode是否已经存有该文件,并且检查客户端的权限 确认可以上传后,根据文件块数返回datanode栈 注:namenod ...
- 复制图片链接和标题生成Markdown文本
写Markdown的时候常常会需要复制图片链接和标题以插入图片,不借助其他工具的话,一般需要先在Markdown文件中输入插入图片的格式,然后在浏览器中复制图片链接和标题将其依次粘贴到Markdown ...
- c++作业22题
一.单选题(共22题,100.0分) 1 已知int i=5,下列do-while循环语句的循环次数是 do{ cout<<i - -<<endl; i - -; }while ...
- 【猫狗数据集】使用预训练的resnet18模型
数据集下载地址: 链接:https://pan.baidu.com/s/1l1AnBgkAAEhh0vI5_loWKw提取码:2xq4 创建数据集:https://www.cnblogs.com/xi ...
- 028.核心组件-API Server
一 Kubernetes API Server原理 1.1 API Server功能 Kubernetes API Server的核心功能是提供Kubernetes各类资源对象(如Pod.RC.Ser ...
- Java 创建、编辑、删除Excel命名区域
Excel命名区域,即对指定单元格区域进行命名,以便对单元格区域引用,如在公式运用中可以引用指定命名区域进行公式操作.在创建命名区域时,可针对整个工作簿来创建,即workbook.getNameRan ...
- Oracle数据库使用sysdba登陆时出现ORA-01031: insufficient privileges问题
今天在自己本本上装上了oracle数据库,然而在命令框登录时 用 sqlplus / as sysdba 时却出现了: insufficient privileges问题 原因就是没有加入ora_ ...
- Redis 服务端程序实现原理
上篇我们简单介绍了 redis 客户端的一些基本概念,包括其 client 数据结构中对应的相关字段的含义,本篇我们结合这些,来分析分析 redis 服务端程序是如何运行的.一条命令请求的完成,客户端 ...
- [code]poj3349 Snowflake Snow Snowflakes
哈希+挂链.可以用next数组挂链. ; type arr=..]of longint; var a,b:Array[..]of arr; next:Array[..]of longint; i,j, ...
- Python习题集(四)
每天一习题,提升Python不是问题!!有更简洁的写法请评论告知我! https://www.cnblogs.com/poloyy/category/1676599.html 题目 如果一个 3 位数 ...
