F - 你这是第一次让我看到落泪了呢 POJ - 3661Running 区间DP

F - 你这是第一次让我看到落泪了呢 POJ - 3661 

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分钟,第i分钟能跑Di的长度;每分钟可以选择跑或者休息,跑+1疲劳值,休息-1疲劳,如果开始休息,必须等疲劳降低到0才开始跑;疲劳度不能超过M; N分钟后疲劳值必须为0;求最大路程

思路:dp[i][j]数组存第i分钟结束时疲劳值为j的最大路程;

           刚开始是顺着题意来想的:休息 dp[i][j]=dp[i-1][j+1]
                                                      走 dp[i][j]=max ( dp[i-1][j-1](之前在走),(之前在休息)dp[i-1][0]  )+var[i]

          但是这样走的那里不知道dp[i-1][j-1]究竟是在走还是休息。就在想能不能dp[i][j]里存的都是走的状态,然后发现输出是dp[N][0](N需要算的是停留的状态),就可以反过来想:走: dp[i][j ]= dp[i-1][j-1] + var[i]   (假设dp[i][j]里存的都是走的状态)

                                                                             休息:dp[i][0]=max( dp[i][0], dp[i-j][j] )    (j指前面休息了j-1分钟)

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <stack>
#include <vector>
#define fora(i,a,b) for(i=a;i<b;i++)
#define fors(i,a,b) for(i=a;i>b;i--)
#define fora2(i,a,b) for(i=a;i<=b;i++)
#define fors2(i,a,b) for(i=a;i>=b;i--)
#define PI acos(-1.0)
#define eps 1e-6
#define INF 0x3f3f3f3f

typedef long long LL;
typedef long long LD;
using namespace std;
const int maxn=10000+5;
int N,M;
int var[maxn];
int dp[maxn][505];
void init()
{
    memset(dp,0,sizeof(dp));
}
int main()
{
    while(~scanf("%d%d",&N,&M))
    {
        init();
        int i,j;
        fora2(i,1,N)
        {
            scanf("%d",var+i);
        }
        int ans=0;
        fora2(i,1,N)
        {
            dp[i][0]=max(dp[i][0],max(dp[i-1][0],dp[i-1][1]));
            fora2(j,1,M)//选择走
            {
                if(j>i)break;
                dp[i][j]=dp[i-1][j-1]+var[i];
            }
            fora2(j,1,M)//i秒休息结束,共休息了j秒
            {
                if(i-j<j)break;
                dp[i][0]=max(dp[i][0],dp[i-j][j]);
            }
        }
        printf("%d\n",dp[N][0]);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/liyang__abc/article/details/81429465