动态规划 HDU 1087

Super Jumping! Jumping! Jumping!

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 22816    Accepted Submission(s): 10039


Problem Description
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 这个DP 别人说比较简单,但是我还是搞了有段时间才把它给弄出来, 主要是一开始我的思路比较局限,而且对动态规划的思想还是不够深入地了解, 之前只是觉得动态规划关键是最优子结构和状态转移方程, 其实动态规划主要优化的地方在于对于一个大问题,进行了分步的处理,分成了若干个有共性的子问题,而且对于每个子问题的解决可追溯到之前已经解决的子问题的答案上来, 就针对这类序列问题来说,主要有两种思路,一个是按照长度分步处理子问题,然后输出最后长度的最优解, 而是每个子问题解决当前 可能的最优解, (并且保证以后的问题中可以直接引用前面的解),最后取所有可能解中的最优解!
---------------------以下是错误代码---------------------------
#include<stdio.h>
#include<iostream>
#define maxn 10002
using namespace std;
int num[maxn],dp[maxn];
int main()
{
    int n;
    while(scanf("%d",&n)&&n)
    {    fill(dp,dp+n+1,0);
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&num[i]);
        }
        dp[1]=num[1];int max=num[1];
        for(int i=2;i<=n;i++)
        {    dp[i]=dp[i-1];
            if(num[i]>max)
            {
                dp[i]=dp[i-1]+num[i];
                max=num[i];
            }
            else
            {
                for(int j=i;j>=1;j--)
                {
                    if(num[i]>num[j]&&num[i]>max)
                    {
                        dp[i]=dp[j]+num[i];
                        max=num[i];
                        break;
                    }
                }
            }
        }
        cout<<dp[n]<<endl;
    }
}
给出一套数据: 1 3 2 3 输出应为: 6而不是4
---------------------以下是正确代码---------------------------
#include<iostream>
#include<stdio.h>
#include<algorithm>
#define maxn 1020
using namespace std;
int num[maxn],dp[maxn];
int main() //最长上升子序列,正确思想应为 每次选择数的时候要选择比前面大但是又要选大小差最小的,如2的后面有3和4 所以要选3而不是4
{
	int n;
	while(scanf("%d",&n)&&n)
	{	fill(num,num+n+1,0);
		fill(dp,dp+n+1,0);
		for(int i=1;i<=n;i++)
		{
			scanf("%d",&num[i]);
		}
		dp[1]=num[1];int max_dp=num[1];
		for(int i=2;i<=n;i++)
		{	int temp=0;
			for(int j=i-1;j>=1;j--)
			{	
				if(num[i]>num[j]&&dp[j]>dp[temp])
				{
					temp=j;
				}
			}
			dp[i]+=dp[temp]+num[i];
			max_dp=max(max_dp,dp[i]);
		}
		cout<<max_dp<<endl;
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值