zoukankan      html  css  js  c++  java
  • 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
    ",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
    ",i,j,k,dp[i][j][k]);
        }
        //fclose(stdin);
        return 0;
    }
    至少做到我努力了
  • 相关阅读:
    Hibernate3 第一天
    Struts2第四天
    Struts2第二天
    Struts2第一天
    【剑指offer】数字在排序数组中出现的次数,C++实现
    【剑指offer】两个链表的第一个公共结点,C++实现
    【剑指offer】数组中的逆序对。C++实现
    [Python]定时任务框架 APScheduler
    [pandas] SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame
    alicode35-找出二叉搜索树的第2大的数
  • 原文地址:https://www.cnblogs.com/chensunrise/p/3679096.html
Copyright © 2011-2022 走看看