poj3661

dp

f[i] = max(f[i], sum(i~i+j-1) + f[i + j * 2]) 或者 f[i] = f[i + 1]

View Code
#include <iostream>

#include <cstdio>

#include <cstdlib>

#include <cstring>

using namespace std;



#define maxn 10005



int n, m;

int dis[maxn];

int f[maxn];

int sum[maxn];



void input()

{

    scanf("%d%d", &n, &m);

    for (int i = 1; i <= n; i++)

        scanf("%d", &dis[i]);

    sum[0] = dis[0] = 0;

    for (int i = 1; i <= n; i++)

        sum[i] = sum[i - 1] + dis[i];

}



void work()

{

    f[n + 1] = 0;

    for (int i = n; i >= 1; i--)

    {

        f[i] = f[i + 1];

        for (int j = 1; j <= m && i + j * 2 <= n + 1; j++)

            f[i] = max(f[i], sum[i + j - 1] - sum[i - 1] + f[i + j * 2]);

    }

    printf("%d\n", f[1]);

}



int main()

{

    //freopen("t.txt", "r", stdin);

    input();

    work();

    return 0;

}

你可能感兴趣的:(poj)