首页 > 代码库 > HDU - 1087 Super Jumping! Jumping! Jumping!(dp)

HDU - 1087 Super Jumping! Jumping! Jumping!(dp)

题意:从起点依次跳跃带有数字的点直到终点,要求跳跃点上的数字严格递增,问跳跃点的最大数字和。

分析:

1、若之前的点比该点数字小,则可进行状态转移,dp[i] = max(dp[i], dp[j] + a[i]);

2、dp[i]---截止到i,跳跃的最大数字和。

3、由于不确定最终是哪个点直接跳往终点可保证数字和最大,因此,扫一遍,ans = max(ans, dp[i]);

#include<cstdio>#include<cstring>#include<cstdlib>#include<cctype>#include<cmath>#include<iostream>#include<sstream>#include<iterator>#include<algorithm>#include<string>#include<vector>#include<set>#include<map>#include<stack>#include<deque>#include<queue>#include<list>#define lowbit(x) (x & (-x))const double eps = 1e-8;inline int dcmp(double a, double b){    if(fabs(a - b) < eps) return 0;    return a > b ? 1 : -1;}typedef long long LL;typedef unsigned long long ULL;const int INT_INF = 0x3f3f3f3f;const int INT_M_INF = 0x7f7f7f7f;const LL LL_INF = 0x3f3f3f3f3f3f3f3f;const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f;const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};const int MOD = 1e9 + 7;const double pi = acos(-1.0);const int MAXN = 1000 + 10;const int MAXT = 10000 + 10;using namespace std;int a[MAXN];int dp[MAXN];int main(){    int N;    while(scanf("%d", &N) == 1){        if(N == 0) return 0;        memset(dp, 0, sizeof dp);        for(int i = 1; i <= N; ++i){            scanf("%d", &a[i]);        }        for(int i = 1; i <= N; ++i){            dp[i] = a[i];            for(int j = 1; j <= i - 1; ++j){                if(a[j] < a[i]){                    dp[i] = max(dp[i], dp[j] + a[i]);                }            }        }        int ans = 0;        for(int i = 1; i <= N; ++i){            ans = max(ans, dp[i]);        }        printf("%d\n", ans);    }    return 0;}

  

HDU - 1087 Super Jumping! Jumping! Jumping!(dp)