您的位置:首页 > 其它

usaco 月赛 2008 January Best Cow Line 贝茜的晨练计划 题解

2014-03-04 10:55 519 查看
Running题解

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 4954 Accepted: 1835
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 ofDi (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

Source

USACO 2008 January Silver

中文大意:奶牛们打算通过锻炼来培养自己的运动细胞,作为其中的一员,贝茜选择的 运动方式是每天进行N(1 <= N <= 10,000)分钟的晨跑。在每分钟的开始,贝茜 会选择下一分钟是用来跑步还是休息。 贝茜的体力限制了她跑步的距离。更具体地,如果贝茜选择在第i分钟内跑 步,她可以在这一分钟内跑D_i(1 <=
D_i <= 1,000)米,并且她的疲劳度会增加 1。不过,无论何时贝茜的疲劳度都不能超过M(1 <= M <= 500)。如果贝茜选择 休息,那么她的疲劳度就会每分钟减少1,但她必须休息到疲劳度恢复到0为止。 在疲劳度为0时休息的话,疲劳度不会再变动。晨跑开始时,贝茜的疲劳度为0。 还有,在N分钟的锻炼结束时,贝茜的疲劳度也必须恢复到0,否则她将没有 足够的精力来对付这一整天中剩下的事情。 请你计算一下,贝茜最多能跑多少米。

------------------------------------------------------分割线-------------------------------------------------------------------

虽然据说标算是贪心,但我打算还是用DP来写。

第一次志在必得,直接推方程:

for (i=1;i<=n;i++)
{
f[i][0]=f[i-1][1];
for (j=1;j<=m;j++)
f[i][j]=max(f[i-1][j+1],f[i-1][j-1]+a[i]);
}
其中f[i][j]表示到第i秒,疲劳度为j时最远的距离。

然后一遍过样例,提交~~~~~~WA!

仔细一看题目:每次休息时必须休息到0为止!这可怎么办呢?

SYC大牛指点:只能顺推才能防止此错误!

于是又吭哧吭哧写代码:

for (i=1;i<=n;i++)
{
for (j=1;j<=min(m,n-i);j++)
{
f[i][j]=max(f[i][j],f[i-1][j-1]+a[i]);
f[i+j][0]=max(f[i+j][0],f[i][j]);
}
}
但是还是过不了!最后想了半天,我发现还有一种情况没有考虑:第i--j分钟奶牛一直休息!

因此再加一句话就过了。以下是AC代码:

#include<stdio.h>
#include<iostream>
using namespace std;
int f[10001][502],a[10001],n,m,i,j,maxx,ans;
int main()
{
scanf("%ld %ld",&n,&m);
for (i=1;i<=n;i++)
scanf("%ld",&a[i]);
f[1][1]=a[1];
for (i=1;i<n;i++)
{
for (j=0;j<=min(m,n-i);j++)
{
f[i+1][j+1]=max(f[i+1][j+1],f[i][j]+a[i+1]);
f[i+j][0]=max(f[i+j][0],f[i][j]);
f[i+1][0]=max(f[i][0],f[i+1][0]);
}
}
printf("%ld",f
[0]);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  usaco月赛 DP