|Tyvj|动态规划|1023 奶牛的锻炼

http://tyvj.cn/p/1023

比较简单的区间DP,注意如果要休息,那就必须得一直休息到疲劳值为0,则f[i][0]可以从f[i-k][k]转移而来

#include
#include
#include
using namespace std;
#define ms(i,j) memset(i, j, sizeof i);
int n,m;
int d[2005];
int f[2005][505];
int main()
{
    scanf("%d%d", &n, &m);
    for (int i=1;i<=n;i++) scanf("%d", &d[i]);
    ms(f,0);
    for (int i=1;i<=n;i++)
    {
        for (int j=1;j<=m;j++)
        {
            f[i][j] = f[i-1][j-1]+d[i];
        }
        f[i][0] = f[i-1][0];
        for (int k=1;k<=i;k++)
        f[i][0] = max(f[i][0], f[i-k][k]);
    }
    printf("%d\n", f[n][0]);
    //getchar(); getchar();
    return 0;
}


转载于:https://www.cnblogs.com/flyinthesky1/p/6384298.html

你可能感兴趣的:(数据结构与算法)