dp-Jumping Cows

Farmer John’s cows would like to jump over the moon, just like the cows in their favorite nursery rhyme. Unfortunately, cows can not jump.

The local witch doctor has mixed up P (1 <= P <= 150,000) potions to aid the cows in their quest to jump. These potions must be administered exactly in the order they were created, though some may be skipped.

Each potion has a ‘strength’ (1 <= strength <= 500) that enhances the cows’ jumping ability. Taking a potion during an odd time step increases the cows’ jump; taking a potion during an even time step decreases the jump. Before taking any potions the cows’ jumping ability is, of course, 0.

No potion can be taken twice, and once the cow has begun taking potions, one potion must be taken during each time step, starting at time 1. One or more potions may be skipped in each turn.

Determine which potions to take to get the highest jump.
Input* Line 1: A single integer, P

  • Lines 2…P+1: Each line contains a single integer that is the strength of a potion. Line 2 gives the strength of the first potion; line 3 gives the strength of the second potion; and so on.Output* Line 1: A single integer that is the maximum possible jump.Sample Input8
    7
    2
    1
    8
    4
    3
    5
    6
    Sample Output
    17
    拿题思想:这是一个给牛喝药水,牛就可以增加相应的跳跃能力,但是只有在奇数时间里是增加,而偶数时间里是减少相应的步数;这是一个求牛能获得的最高跳跃能力,为一个最值问题,所以可以用动态规划的方法进行求解;首先我想到的是只有一瓶药水时就直接喝掉,增加相应的跳跃能力作为边界,然后想当时间为i为奇数时就在i-2时间上再进行增加,同理再i为偶数时就在i-2的基础上减少就行,但这样行不通!
    遇到问题:依然是状态转移方程的寻找!首先边界是当i==1时,直接为增加相应的高度;然后当为i!=1的时刻,分开考虑;当i为奇数时,是在i-1上增加,也就是在i-1减少高度上增加;当i为偶数时是在上i-1增加步数的时刻上再减掉步数;
    最终解题步骤
    1)建立一个数组用来储存药水对应的能力,用两个动态数组用来探究每个时刻上的跳跃能力;
    2)对时刻i进行状态分析,利用状态转移方程来找目标值;
    3)在从两个动态数组中找出最大值;
#include <iostream>
#include <stdio.h>
using namespace std;

int main()
{
    int p,a[150001],dp1[150001],dp2[150001];
    int i,j,ans,ans1,ans2;
    while(scanf("%d", &p)!=EOF)
        {
            for(i=1;i<=p;i++)
                 scanf("%d", &a[i]);
            for(i=1;i<=p;i++)
                {
                    if(i==1)
                        {
                            dp1[i]=a[i];      //dp1,dp2分开考虑;
                            dp2[i]=0;
                        }
                    else
                        {
                            dp1[i]=dp2[i-1]+a[i];       //当i为奇数时在i-1减少高度的时刻上加上对应的a[i];
                            dp1[i]=max(dp1[i],dp1[i-1]);
                            dp2[i]=dp1[i-1]-a[i];       //当i为偶数时在i-1增加高度的时刻上减去对应的a[i];
                            dp2[i]=max(dp2[i],dp2[i-1]);
                        }
                }
            ans1=dp1[1];
            ans2=dp2[1];
            for(i=2;i<=p;i++)
                {
                    ans1=max(ans1,dp1[i]);
                    ans2=max(ans2,dp2[i]);
                }
                ans=max(ans1,ans2);
            cout<<ans<<endl;
        }
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值