3.29一周小结(dp)

这周学习了dp(动态规划),感觉有点力不从心了,主要体现在几个方面,第一点是在读题方面,现在都是在vj上做题,题目都是英文的,先是要翻译一下,然后还要理解题意,但这题就像老师说的一样,要挑出题目中的重点,干货,但我往往找不到重点,或者有时候翻译的不够准确,就没法正确理解题意。

第二点就是在找状态转移方程上,由于题目做的太少,积累的经验不够多,还有就是对老师给的题目理解的不够透彻,导致自己找不到思路。

再就是第三点,我觉得可能也是令我最苦恼的一点,最近在家呆的属实是开始懈怠了,题目越来越读不进去,抠题时间也越来越长,上课精力不像开始那几周那么集中了,我也是从这周开始才明白了原来在家和在学校学习是完全两个概念(希望能够赶紧开学吧)。
下面对动态规划进行一个小结:

一、动态规划的一般解题步骤

在这里插入图片描述

二、重要的几个题型

最长上升序列,最长公共子序列,最大字段和,最大子矩阵和,最大M子段和问题。

三、一道dp的题目

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

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

题解:
题意是有P瓶魔法药水,能让牛每次跳的高度相应的增加或减少。有头牛每跳一步喝一瓶,当它跳的是第奇数步,跳的高度增加a[i],如果是第偶数步,跳得高度就减少a[i].问你如何挑选题目所给的数据可以使牛跳的最高?
拿到这道题我首先是注意到了题目所说的奇数和偶数,这就可以利用二维数组分别进行储存奇数和偶数项的数值。

#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
int num[150010];
int dp[100000][2];
int main()
{
int p;
int ans;
while(cin >>p)
{
memset(dp,0,sizeof(dp));
memset(num,0,sizeof(num));
ans=0;
for (int i=1;i<=p;i++)
{
 cin >>num[i];
}
dp[0][0]=0;
dp[0][1]=0;
for (int i=1;i<=p;i++)
{
 dp[i][0]=max(dp[i-1][0],dp[i-1][1]-num[i]);
 dp[i][1]=max(dp[i-1][1],dp[i-1][0]+num[i]);
}
ans=max(dp[p][0],dp[p][1]);
cout <<ans<<endl;
}
return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值