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),所以我们先计算前面的点,然后转移到后面的点即可;

代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#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;
}
-------------本文结束,感谢您的阅读!-------------