Running (poj 3661)

来源:互联网 发布:数据漫游用打开吗 编辑:程序博客网 时间:2024/06/08 10:21

Running
Time Limit: 1000MS Memory Limit: 65536KTotal 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 2534210

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;}


3 0
原创粉丝点击