Running (poj 3661)

Running
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 5728 Accepted: 2155

Description

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.

Input

* Line 1: Two space-separated integers: N and M
* Lines 2..N+1: Line i+1 contains the single integer: Di

Output

* Line 1: A single integer representing the largest distance Bessie can run while satisfying the conditions.
 

Sample Input

5 2
5
3
4
2
10

Sample Output

9



题意:

给出一个时间表,表示每分钟可以跑的路程,例如a[3]表示如果在第3分钟选择跑步则可以跑的距离。题目首先给出n,m表示一共有多少分钟以及最大的可接受疲劳度。

如果在某一分钟选择跑步则会疲劳度 ++,如果当前疲劳度等于m,那么就不能继续跑只能选择休息。而如果选择休息,每分钟就会下降一点疲劳,但是一旦选择休息就只能等疲劳到0才能继续前进。当然,任何时候都可以选择休息,包括疲劳为0的时候。


思路:

定义dp[i][j] 表示第 i 分钟疲劳为 j 的最大距离,那么显然 dp[i][j] = dp[i-1][j-1] + a[i];

然后因为在疲劳为0的时候也可以休息,则有 dp[i][0] = dp[i-1][0];


因为任意时刻都可以休息,则可以枚举在第i分钟疲劳为0的时候已经休息了k分钟,即从 i - k 分钟疲劳为k休息到现在

for(int k = 1;k <= m;k++)

if(i - k >= 0)

dp[i][0] = max(dp[i-k][k],dp[i][0]);

#include"iostream"
#include"cstring"
#include"cstdio"

using namespace std;

int n,m;
int a[10005];
int dp[10005][505];

int main(void)
{
    while(~scanf("%d%d",&n,&m))
    {
        for(int i = 1;i <= n;i++) scanf("%d",&a[i]);

        memset(dp,0,sizeof(dp));

        for(int i = 1;i <= n;i++)
        {
            for(int j = 1;j <= m;j++)
            {
                dp[i][j] = dp[i-1][j-1] + a[i];
            }
            
            dp[i][0] = dp[i-1][0];
            
            for(int k = 1;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;
}


  • 4
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值