codevs2161 奶牛的鍛鍊

題目描述 Description

奶牛Bessie有N分鐘時間跑步,每分鐘她可以跑步或者休息。若她在第i分鐘跑步,可以跑出D_i米,同時疲倦程度增加1(初始爲0)。若她在第i分鐘休息,則疲倦程度減少1。無論何時,疲倦程度都不能超過M。另外,一旦她開始休息,只有當疲憊程度減爲0時才能重新開始跑步。在第N分鐘後,她的疲倦程度必須爲0。

輸入描述 Input Description

第一行,兩個整數,代表N和M。
接下來N行,每行一個整數,代表D_i。

輸出描述 Output Description

Bessie想知道,她最多能跑的距離。

樣例輸入 Sample Input

5 2
5
3
4
2
10

樣例輸出 Sample Output

9

數據範圍及提示 Data Size & Hint

N <= 2000 , M <= 500 , D_i <= 1000
dpdpd,考慮在第 i 分鐘疲勞爲 j 和 0 的情況,具體轉移方程見代碼
代碼如下

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int sz = 2010;
int f[sz][501];
int num[sz];
int n,m;
int main()
{
    scanf("%d%d",&n,&m);
    for(int i = 1 ; i <= n ; i ++)
        scanf("%d",&num[i]);
    for(int i = 1 ; i <= n ; i ++)
    {
        for(int j = 1 ; j <= m ; j ++)
        {
            if(i > j)
                f[i][0] = max(max(f[i-j][j],f[i-1][0]),f[i][0]);
            f[i][j] = max(f[i][j],f[i-1][j-1]+num[i]);

        }
    }
    printf("%d\n",f[n][0]);
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章