hust 1038 Running

题目描述

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 D_i (1 <= D_i <= 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.

输入

* Line 1: Two space-separated integers: N and M * Lines 2..N+1: Line i+1 contains the single integer:D_i

输出

* Line 1: A single integer representing the largest distance Bessie can run while satisfying the conditions.

样例输入

5 2
5
3
4
2
10

样例输出

9
这个题有个很牛的地方,就是题目中我做了红色标记的地方
dp是强大的,但是简单dp我还是写的出来的,这个题就是一个简单的dp,不过我的程序就是直接dp,没有对空间进行优化,不过在时间和空间上,对于这个题已经是足够了,呵呵,要是严格那么一点,这个题可能就挂了,不过还好,至少dp方程是自己想出来的,加油
#include<map>
#include<set>
#include<queue>
#include<cmath>
#include<vector>
#include<cstdio>
#include<string>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#define  inf 0x0f0f0f0f
 
using namespace std;
 
const double pi=acos(-1.0);
const double eps=1e-8;
typedef pair<int,int>pii;
int dp[10010][510][2];
int a[10010];
 
int main()
{
    //freopen("in.txt","r",stdin);
 
    int n,m;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        for (int i=1;i<=n;i++) scanf("%d",&a[i]);
        memset(dp,0,sizeof(dp));
        for (int i=2;i<=n+1;i++)
        for (int j=0;j<=m;j++)
        {//分成3种情况来做
            if (j==0)
            {
                dp[i][j][0]=max(max(dp[i-1][j][0],dp[i-1][j][1]),max(dp[i-1][j+1][0],dp[i-1][j+1][1]));
            }
            else if (j==1)
            {
                dp[i][j][0]=max(dp[i-1][j+1][0],dp[i-1][j+1][1]);
                dp[i][j][1]=dp[i-1][j-1][0]+a[i-1];
            }
            else
            {
                dp[i][j][0]=max(dp[i-1][j+1][0],dp[i-1][j+1][1]);
                dp[i][j][1]=dp[i-1][j-1][1]+a[i-1];
            }
        }
        printf("%d\n",dp[n+1][0][0]);
        //这是用来查错的,就是因为题目没有看准,才写了这个
        //for (int i=1;i<=n+1;i++)
        //    for (int j=0;j<=m;j++)
        //        for (int k=0;k<2;k++)
        //        printf("%d %d %d %d\n",i,j,k,dp[i][j][k]);
    }
    //fclose(stdin);
    return 0;
}

 

posted @ 2014-04-21 19:46  Hust_BaoJia  阅读(364)  评论(0编辑  收藏  举报
努力