您的位置:首页 > 其它

poj 3661 Running(区间dp)

2015-06-18 17:41 489 查看
Language:
Default

Running

Time Limit: 1000MSMemory Limit: 65536K
Total Submissions: 5518Accepted: 2067
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


n分钟内在疲劳值最大为m时 最多走多少距离

后面有n个值 分别代表每分钟能走的距离 每跑一分钟疲劳值+1 当疲劳值达到m时必须要休息

如果选择休息则必须休息到疲劳值为0

求n分钟疲劳值为0时所跑的最长距离

思路: dp[i][j]代表第i分钟疲劳值为j时所能跑的最大距离

dp[i][j]=dp[i-1][j-1]+d[i]代表第i选择跑步

如果第i分钟 疲劳值为0 选择休息时 dp[i][0]=dp[i-1][0]

另外还有如果在疲劳值为j时选择休息 那么得休息到疲劳值为0

那么就是在dp[i][0]枚举疲劳值j 从第i-j分钟疲劳值为j 休息到i分钟疲劳值为0

#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string.h>
#include <string>
#include <vector>
#include <queue>

#define MEM(a,x) memset(a,x,sizeof a)
#define eps 1e-8
#define MOD 10009#define MAXN 10010
#define MAXM 100010
#define INF 99999999#define ll __int64
#define bug cout<<"here"<<endl
#define fread freopen("ceshi.txt","r",stdin)
#define fwrite freopen("out.txt","w",stdout)

using namespace std;

int Read()
{
char c = getchar();
while (c < '0' || c > '9') c = getchar();
int x = 0;
while (c >= '0' && c <= '9') {
x = x * 10 + c - '0';
c = getchar();
}
return x;
}

void Print(int a)
{
if(a>9)
Print(a/10);
putchar(a%10+'0');
}

int dp[10010][510];
int d[10010];

int main()
{
//fread;
int n,m;
while(scanf("%d%d",&n,&m)!=EOF)
{
for(int i=1;i<=n;i++)
scanf("%d",&d[i]);
MEM(dp,0);
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
dp[i][j]=dp[i-1][j-1]+d[i];
dp[i][0]=dp[i-1][0];
for(int j=1;j<=m;j++)
{
if(i-j>=0)
dp[i][0]=max(dp[i][0],dp[i-j][j]);
}
}
printf("%d\n",dp
[0]);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: