poj 1976 A Mini Locomotive 动规
这题有些像求连续子序列和的最大值
设dp[i][j]表示i个火车头拉动j个火车舱的人数和的最大值,状态方程如下:dp[i][j] = max(dp[i-1][j-times]+num[j]+..+num[j-times+1], dp[i][k])
#include <iostream> #include <cstdio> #include <cstring> using namespace std; const int MAX = 50005; int dp[5][MAX]; int num[MAX]; int n; int main() { int cases; scanf("%d", &cases); int times; while (cases--) { scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d", &num[i]); scanf("%d", ×); memset(dp, 0, sizeof(dp)); for (int i = 1; i <= 3; i++) for (int v = times; v <= n; v++) { int sum = 0; for (int j = 0; j < times; j++) sum += num[v-j]; dp[i][v] = dp[i-1][v-times] + sum; for (int k = i*times; k < v; k++) dp[i][v] = max(dp[i][v], dp[i][k]); } printf("%d\n", dp[3][n]); } return 0; }