Super Jumping! Jumping! Jumping!【基础DP】

题目链接:HDU-1087

kuangbin带你飞【专题十二 基础DP1 】

题目描述:

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.

## **思路:** 按照动态规划的常规套路分为4步;

1.确定状态
我们发现第k步的贡献等于前k - 1步的贡献 + 当前的权值;
所以我们设 dp[i]表示到达i点的最大权值
它的子问题就是到达dp[i - p]点的最大权值。显然这满足最优子结构和无后效性
2.转移方程
因为a[i]这个点 只能由 a[j] (j < i && a[j] < a[i])这种点转移过来,所以我们可以枚举所有在i前面的点 判断它是否 < a[i]即可;
如果a[j] < a[i] && j < i 则 dp[i] = min(dp[i], dp[j] + a[i]);
3.边界情况与初始化
只需要在计算每个点的时候,将其dp[i] 赋值为 a[i]即可 代表其最小贡献就是自己的权值;
4.计算顺序
显然,我们需要dp[i],就需要dp[i - k] (k < i),所以我们先计算前面的点,然后转移到后面的点即可;

代码:

#include <bits/stdc++.h>
using namespace std;

#define long long long
#define INF 0x3f3f3f3f
#define MAX_N 10010

int a[MAX_N], dp[MAX_N];

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    int n;
    while(cin >> n, n)
    {
        for(int i = 1; i <= n; i++)
        {
            cin >> a[i];
        }
        int MAX = 0;
        for(int i = 1; i <= n; i++)
        {
            dp[i] = a[i];
            for(int j = 1; j < i; j++)
            {
               if(a[i] > a[j])
                    dp[i] = max(dp[i], dp[j] + a[i]);
            }
            MAX = max(MAX, dp[i]);
        }
        cout << MAX << endl;
    }

    return 0;
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值