2.running-dp

题目:

The cows are trying to become better athletes, so Bessie is running on a track for exactly N (1 ≤ N ≤ 10,000) minutes. During each minute, she can choose to either run or rest for the whole minute.

The ultimate distance Bessie runs, though, depends on her 'exhaustion factor', which starts at 0. When she chooses to run in minute i, she will run exactly a distance of Di (1 ≤ Di ≤ 1,000) and her exhaustion factor will increase by 1 -- but must never be allowed to exceed M (1 ≤ M ≤ 500). If she chooses to rest, her exhaustion factor will decrease by 1 for each minute she rests. She cannot commence running again until her exhaustion factor reaches 0. At that point, she can choose to run or rest.

At the end of the N minute workout, Bessie's exaustion factor must be exactly 0, or she will not have enough energy left for the rest of the day.

Find the maximal distance Bessie can run.

输入:

<p>* Line 1: Two space-separated integers: <i>N</i> and <i>M</i><br>* Lines 2..<i>N</i>+1: Line <i>i</i>+1 contains the single integer: <i>D<sub>i</sub></i> </p>

输出:

<p>* Line 1: A single integer representing the largest distance Bessie can run while satisfying the conditions.<br> </p>

题目大意:

        这个牛有N分钟跑步,一开始疲劳指数是0,跑步一分钟不论长短都要使疲劳指数+1,疲劳指数有上限M,到达M就必须要休息了,休息可以使疲劳指数-1,但必须要疲劳指数到0才能继续跑步,休息不受限制。

思路:

            这一次休息:dp[i][0]=dp[i-1][0],这一次跑步:dp[i][j]=dp[i-1][j-1]+d[i]

            dp[i][j]表示第i分钟时疲劳指数是j时获得的最大的距离

代码:


#include<stdio.h>
#include<algorithm>
using namespace std;
const int N=10010;
int d[N];
int dp[N][505];
int main()
{
    int n,m;
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&d[i]);
    }
    for(int i=1;i<=n;i++)
    {
        for(int j=0;j<=m;j++)
        {
            dp[i][j]=dp[i-1][j-1]+d[i];
            dp[i][0]=dp[i-1][0];
        }
        for(int k=0;k<=m;k++)
        {
            if(i-k>0)
            dp[i][0]=max(dp[i][0],dp[i-k][k]);
        }
    }
    printf("%d\n",dp[n][0]);
    return 0;
}



猜你喜欢

转载自blog.csdn.net/wentong_xu/article/details/80504168