#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
int t, n, M;
int v[50005];
int s[50005];
int f[50005][4];
struct poj1976 {
/*【问题描述】:有n节车厢,每节车厢有v[i]个乘客,有3列火车,
* 每列火车只能拉连续的M节车厢,求最多能拉多少乘客
*/
/*【解题思路】:f[i][j]表示用j列火车去拉前i节厢的乘客,最多能拉多少人,
* 则f[i][j]=max{f[i-1][j], f[i-M][j-1]+sum{v[i-M+1],...,v[i]} }
* 用s[i]表示前i节车厢的乘客总数
* 则f[i][j]=max{f[i-1][j], f[i-M][j-1]+s[i]-s[i-M]}
*/
void work() {
cin >> t;
while (t--) {
cin >> n;
s[0] = 0;
for (int i = 1; i <= n; i++) {
cin >> v[i];
s[i] = s[i - 1] + v[i];
}
cin >> M;
memset(f, 0, sizeof(f));
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= 3; j++) {
if (i < M)
f[i][j] = s[i];
else
f[i][j] = max(f[i - 1][j], f[i - M][j - 1] + s[i] - s[i - M]);
}
}
cout << f[n][3] << endl;
}
}
};
int main()
{
poj1976 solution;
solution.work();
return 0;
}
poj1976-A Mini Locomotive(01背包)
最新推荐文章于 2021-08-15 16:04:37 发布