poj 3661 Running(区间dp)

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

Source

 

题意:给你一个n,m,n表示有n分钟,每i分钟对应的是第i分钟能跑的距离,m代表最大疲劳度,每跑一分钟疲劳度+1,当疲劳度==m,必须休息,在任意时刻都可以选择休息,如果选择休息,那么必须休息到疲劳度为0,当然,当疲劳度为0的时候也是可以继续选择休息的,求在n分钟后疲劳度为0所跑的最大距离

思路:dp[i][j]表示在第i分钟疲劳度为j的时候所跑的最大距离,dp[i][j]=dp[i-1][j-1]+d[i];这个转移,说的是,第i分钟选择跑步,当然,第i分钟可以选择不跑步,那么就是选择休息,题目说了,选择休息的话必须要休息到疲劳度为0才可以跑,那还有一点,当疲劳度为0了,还是选择继续休息呢?dp[i][0]=dp[i-1][0];
选择休息,那么疲劳度到0了,这一点的最大距离怎么做呢?dp[i][0]=max(dp[i][0],dp[i-k][k])   (0<k<=m&&i-k>0)

这样所有的状态都考虑完了,可以写代码了.......

 1 #pragma comment(linker, "/STACK:1024000000,1024000000")
 2 #include<iostream>
 3 #include<cstdio>
 4 #include<cstring>
 5 #include<cmath>
 6 #include<math.h>
 7 #include<algorithm>
 8 #include<queue>
 9 #include<set>
10 #include<bitset>
11 #include<map>
12 #include<vector>
13 #include<stdlib.h>
14 #include <stack>
15 using namespace std;
16 #define PI acos(-1.0)
17 #define max(a,b) (a) > (b) ? (a) : (b)
18 #define min(a,b) (a) < (b) ? (a) : (b)
19 #define ll long long
20 #define eps 1e-10
21 #define MOD 1000000007
22 #define N 10006
23 #define M 506
24 #define inf 1e12
25 int n,m;
26 int d[N],dp[N][M];//dp[i][j]表示i分钟j疲劳时的最大距离。
27 int main()
28 {
29    while(scanf("%d%d",&n,&m)==2){
30       for(int i=1;i<=n;i++){
31          scanf("%d",&d[i]);
32       }
33       memset(dp,0,sizeof(dp));
34       for(int i=1;i<=n;i++){
35          for(int j=1;j<=m;j++){
36             dp[i][j]=dp[i-1][j-1]+d[i];//在i分钟时选择跑
37          }
38          dp[i][0]=dp[i-1][0];//如果在疲劳度为0时选择休息,=dp[i-1][0]
39          for(int j=1;j<=m;j++){
40             if(i-j>=0){
41                dp[i][0]=max(dp[i][0],dp[i-j][j]);//在i分钟选择休息到疲劳度为0
42             }
43          }
44          //dp[i][0]=dp[i-1][0];
45       }
46       printf("%d
",dp[n][0]);
47 
48    }
49     return 0;
50 }
View Code
原文地址:https://www.cnblogs.com/UniqueColor/p/5017832.html