题意:Bessie要运动N分钟,已知每一分钟可以跑的距离,每一分钟可选择跑或者不跑,若选择跑,疲劳度加1,但疲劳度不能超过M;若选择不跑,则每过一分钟,疲劳度减1,且只有当疲劳度减为0时可以继续跑。求运动N分钟后且疲劳度恰好为0时可以跑的最远距离。
分析:
1、dp[i][j]---第i分钟疲劳度为j时可以跑的最远距离。
2、dp[i][0] = dp[i - 1][0]---在第i分钟选择休息。
3、dp[i][0] = Max(dp[i][0], dp[i - j][j])---在第i-j分钟选择休息。
4、dp[i][j] = dp[i - 1][j - 1] + a[i]---第i分钟选择继续跑。
#pragma comment(linker, "/STACK:102400000, 102400000") #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 Min(a, b) ((a < b) ? a : b) #define Max(a, b) ((a < b) ? b : a) 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 = 10000 + 10; const int MAXT = 10000 + 10; using namespace std; int a[MAXN]; int dp[MAXN][510]; int main(){ int N, M; scanf("%d%d", &N, &M); for(int i = 1; i <= N; ++i){ scanf("%d", &a[i]); } for(int i = 1; i <= N; ++i){ dp[i][0] = dp[i - 1][0]; for(int j = 1; j <= M; ++j){ if(i - j > 0){ dp[i][0] = Max(dp[i][0], dp[i - j][j]); } dp[i][j] = dp[i - 1][j - 1] + a[i]; } } printf("%d\n", dp[N][0]); return 0; }