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

题意很简单,就是给一个n,然后输入n个数,找到其中的最长递增的子序列。(很明显,用dp)

要注意,用dp是因为:如果你直接就比较找答案,极有可能找到的不是最优解。(初学者容易犯的错误)

例如:5 6 7 1 2 3 4 5 6 7 8

直接比较找答案的结果:5 6 7 8  总和为26

用dp的结果:1 2 3 4 5 6 7 8      总和为36

感兴趣的可以试一下,看结果是不是这样。我会把两种方法的代码贴上去(都很短),比较一下。

回到正题,讲一下dp的做法:

状态转移方程:

dp[i] = max(dp[i],dp[j]+num[i]);//dp[i]代表当前状态的总和,dp[j]代表上一个数的状态的总和,num[i]代表当前的数。

上dp代码:

#include <iostream>
#include <string>
#include <sstream>
#include <stdio.h>
#include <stdlib.h>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <ctime>
#include <vector>
#include <set>
#include <sstream>
#include <map>
#include <cctype>using namespace std;
#define Start clock_t start = clock();
#define End clock_t end = clock();
#define Print cout<<(double)(end-start)/CLOCKS_PER_SEC*1000<<"ºÁÃë"<<endl;const int maxn = 10000 + 5;int dp[maxn],num[maxn];
int main()
{int n;while(cin>>n && n){memset(dp,0,sizeof(dp));int maxi = 0;for(int i = 0;i < n;i++)cin>>num[i];dp[0] = num[0];for(int i = 1;i < n;i++){//dp[i] = num[i];//34行就是这一处for(int j = 0;j < i;j++){if(num[i] > num[j]){//dp[i]代表当前状态的总和,dp[j]代表上一个数的状态的总和,num[i]代表当前的数。dp[i] = max(dp[i],dp[j]+num[i]);}}dp[i] = max(dp[i],num[i]);//这里有个技巧,此处的效果和34行的效果一样}for(int i = 0;i < n;i++){maxi = max(dp[i],maxi);}cout<<maxi<<endl;}return 0;
}

现在给的是直接比较找答案的方法(错误的方法,但是初学者可能一开始会这么做。)

#include <iostream>
#include <string>
#include <sstream>
#include <stdio.h>
#include <stdlib.h>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <ctime>
#include <vector>
#include <set>
#include <sstream>
#include <map>
#include <cctype>using namespace std;
#define Start clock_t start = clock();
#define End clock_t end = clock();
#define Print cout<<(double)(end-start)/CLOCKS_PER_SEC*1000<<"ºÁÃë"<<endl;const int maxn = 1000000 + 5;int a,b;
int main()
{int n;while(cin>>n && n){int ans = 0;b = 0;for(int i = 0;i < n;i++){cin>>a;if(a > b) {b = a;ans += a;}else continue;}cout<<ans<<endl;}return 0;
}

希望我的文章能给你们带来帮助,请点赞!

Super Jumping! Jumping! Jumping!(初学者也能看懂的dp)相关推荐

  1. 初学者也能看懂的 Vue2 源码中那些实用的基础工具函数

    1. 前言 大家好,我是若川.最近组织了源码共读活动,感兴趣的可以加我微信 ruochuan12 想学源码,极力推荐之前我写的<学习源码整体架构系列>jQuery.underscore.l ...

  2. 初学者也能看懂的 Vue3 源码中那些实用的基础工具函数

    1. 前言 大家好,我是若川.最近组织了源码共读活动.每周读 200 行左右的源码.很多第一次读源码的小伙伴都感觉很有收获,感兴趣可以加我微信ruochuan12,拉你进群学习. 写相对很难的源码,耗 ...

  3. 学校里学不到的调试技巧(初学者也可以看懂)

    实用调试技巧 1. 什么是bug? 2.调试是什么?有多重要? 2.1 调试是什么? 2.2 调试的基本步骤 2.3 Debug和Release的介绍. 3. Windows环境调试介绍 3.1 调试 ...

  4. C语言打印出心形表白,520神器,初学者也能看懂!!

    C语言实现打印出心形,初学者的表白神器. 解题思路:这道例题我分了4部分,前3行一部分,4-6行一部分,7-13行一部分,最后一行一部分,读者请仔细阅读注释,小林写的很详细了. 前三行输出,为了让初学 ...

  5. 初学者也能看懂的Ray March体积云

    前言 今天要给大家分享的主题是现在游戏开发中必备的一项游戏效果:游戏中的云渲染 现在大部分的游戏,不管是主机.PC还是手机游戏都在往3A的方向发展,所以现在的游戏在云的渲染上很少会采用传统的渲染方式 ...

  6. 初学者都能看懂的 Spring 源码之依赖注入(DI)源码分析

    前言 在面试中,经常被问到 Spring 的 IOC 和 DI (依赖注入),很多人会觉得其实 IOC 就是 DI ,但是严格上来说这两个其实并不等价,因为 IOC 注重的是存,而依赖注入注重的是取, ...

  7. 详解 matplotlib.pyplot ,Python 初学者真能看懂

    Matplotlib 是一个 Python 中的 2D 绘图库, pyplot 模块是一个方便使用 Matplotlib 的接口. 下面是 pyplot 模块中的五个重要的知识点: [创建图形]: p ...

  8. 初学者也能看懂的DPDK解析

    欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~ 本文由Willko发表于云+社区专栏 一.网络IO的处境和趋势 从我们用户的使用就可以感受到网速一直在提升,而网络技术的发展也从1GE/10 ...

  9. 绝对干货!初学者也能看懂的DPDK解析

    欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~ 本文由Willko发表于云+社区专栏 一.网络IO的处境和趋势 从我们用户的使用就可以感受到网速一直在提升,而网络技术的发展也从1GE/10 ...

最新文章

  1. 今晚8点直播 | 美团是怎么玩儿AI的?大牛揭秘美团超大规模数据集——美团大脑
  2. 网站推广期间如何理解定制网站推广基本要素
  3. Linux CPU cache
  4. JavaScript的值传递和引用传递
  5. 获取客户端ip_代理IP工具能否解决反爬?
  6. 使用OmniDB数据库管理工具,管理Oracle/MariaDB/PostgreSQL等关系型数据库
  7. opencv生成日志_OpenCV在Android环境下的使用方法
  8. 智能优化算法之免疫算法(IA)
  9. CSS半透明边框效果
  10. Java最新手机号正则验证
  11. 无线VoIP技术的现状
  12. java 中文大写金额_金额数字转中文大写
  13. iOS 开源图形库 Core Plot 使用教程
  14. 解决:测试HDFS读写性能时出现错误
  15. 从命令行中进入云主机及相关操作
  16. 亚马逊邮件关联 关联原因?邮件
  17. sublime中文出现乱码怎么办?这里有办法
  18. android ui v2ex,V2EX-android
  19. postgre的replace函数
  20. 自己写的几个简单常用的脚本

热门文章

  1. 伪造ACK实现TCP数据注入
  2. ArcGIS中两种配准方式-Spatial Adjustment和Georeference
  3. 计算机游戏的书推荐,游戏篇丨7款解密类神作推荐,剧情悬念深度,统统都有!...
  4. C++游戏编程教程(二)
  5. linux wine 发展 历程,Wine 4.1 发布,迈向Wine 5.0的第一步
  6. Python求两个数的最大公约数
  7. 《波斯战火》第一个世界帝国及其西征
  8. SSM民宿管理系统,JSP智能民宿系统
  9. 2023最新SSM计算机毕业设计选题大全(附源码+LW)之java社团管理系统85143
  10. JS中int和string的转换