poj1976

dp

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

#define MAX_COACH_NUM 50005

int coach_num;
int coach[MAX_COACH_NUM];
int f[4][MAX_COACH_NUM];
int sum[MAX_COACH_NUM];
int min_loco;

void input()
{
    scanf("%d", &coach_num);
    coach[0] = 0;
    for (int i = 1; i <= coach_num; i++)
        scanf("%d", &coach[i]);
    scanf("%d", &min_loco);
}

void work()
{
    memset(f, 0, sizeof(f));
    sum[0] = 0;
    for (int i = 1; i <= coach_num; i++)
        sum[i] = sum[i - 1] + coach[i];
    for (int i = 1; i <= 3; i++)
    {
        for (int j = coach_num - i * min_loco + 1; j > 0; j--)
            f[i][j] = max(f[i][j + 1], f[i - 1][j + min_loco] + sum[j + min_loco - 1] - sum[j - 1]);
    }
    printf("%d\n", f[3][1]);
}

int main()
{
    int t;
    scanf("%d", &t);
    while (t--)
    {
        input();
        work();
    }
    return 0;
}
View Code

 

posted @ 2013-06-22 15:50  金海峰  阅读(154)  评论(0编辑  收藏  举报