Topic Link: Click to open the link
Test instructions: In a straight line movement, every minute can exercise distance a[i], every minute can choose to exercise or rest, there is a fatigue coefficient, initially 0, each exercise one minute fatigue coefficient plus 1, (can not be greater than m) the same, each rest a minute, fatigue coefficient minus 1, (can not be less than 0) for n minutes after the maximum distance A fatigue factor of 0 is required for n minutes.
Two states, the current time and the current fatigue factor. Set DP[I][J] =dp[i-1][j-1]+a[i] (j>0) Else Dp[i][j] =max (Dp[i][j],max (dp[i-k][0],dp[i-k][k)) (k<=m&&k <i) that is, the current fatigue coefficient of 0 o'clock, may be from the previous state of rest, take the largest. Final output DP[N][0]
#include <algorithm> #include <iostream> #include <cstring> #include <cstdlib> #include < string> #include <cctype> #include <vector> #include <cstdio> #include <cmath> #include < queue> #include <stack> #include <map> #include <set> #define MAXN 1<<12#define _ll __int64# Define ll long long#define INF 0x3f3f3f3f#define Mod 1000000007#define pp pair<int,int> #define ull unsigned long lon Gusing namespace Std;int dp[10002][502],n,m,a[10002];void solve () {memset (dp,0,sizeof (DP));DP [1][0]=0;dp[1][1]=a[1] ; for (int i=2;i<=n;i++) {for (int j=0;j<=m;j++) {if (j==0) {int tem=-1;for (int k=0;k<=m&&k<i;k++) TEM =max (Tem,max (dp[i-k][0],dp[i-k][k));DP [I][0]=tem;} Elsedp[i][j]=dp[i-1][j-1]+a[i];}} printf ("%d\n", Dp[n][0]);} int main () {while (~scanf ("%d%d", &n,&m)) {for (int i=1;i<=n;i++) scanf ("%d", a+i); solve ();} return 0;}
Memory is not very good to write. Don't write it ~ ~
POJ 3661-running (DP)