poj2385 Apple Catching(dp状态转移方程推导)
https://vjudge.net/problem/POJ-2385
猛刷简单dp的第一天的第一题。
状态:dp[i][j]表示第i秒移动j次所得的最大苹果数。关键要想到移动j次,根据j的奇偶判断人在哪里。
想了挺久的,最后还是参考了一篇和自己思路最近的代码https://blog.csdn.net/hellohelloc/article/details/52050207
我比他缺少的是特判dp[i][0]的状态,后面的一切都以此为基础的。
#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<set>
#define INF 0x3f3f3f3f
typedef long long ll;
using namespace std;
int a[], dp[][];
int main()
{
memset(dp, , sizeof(dp));
int n, m;
cin >> n >> m;
for(int i = ; i <= n; i++){
cin >> a[i];
}
for(int i = ; i <= n; i++){
dp[i][] = dp[i-][];
if(a[i] == ){//苹果在左边
dp[i][]++;
for(int j = ; j <= m; j++){
if(j&)//人在右边
dp[i][j] = max(dp[i-][j], dp[i-][j-]);
else//人在左边
dp[i][j] = max(dp[i-][j], dp[i-][j-])+;
}
}
else{//苹果在右边
for(int j = ; j <= m; j++){
if(j&)//人在右边
dp[i][j] = max(dp[i-][j], dp[i-][j-])+;
else//人在左边
dp[i][j] = max(dp[i-][j], dp[i-][j-]);
}
}
}
int maxm = -INF;
for(int i = ; i <= m; i++){
maxm = max(maxm, dp[n][i]);
}
cout << maxm << endl;
return ;
}
poj2385 Apple Catching(dp状态转移方程推导)的更多相关文章
- poj2385 Apple Catching (线性dp)
题目传送门 Apple Catching Apple Catching Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 154 ...
- POJ2385——Apple Catching
$Apple~Catching$ Time Limit: 1000MS Memory Limit: 6553 ...
- Apple Catching(dp)
Apple Catching Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9831 Accepted: 4779 De ...
- Mark一下, dp状态转移方程写对,可是写代码都错,poj 1651 poj 1179
dp题: 1.写状态转移方程; 2.考虑初始化边界,有意义的赋定值.还没计算的赋边界值: 3.怎么写代码自底向上计算最优值 今天做了几个基础dp,所有是dp方程写对可是初始化以及计算写错 先是poj ...
- poj 2385 Apple Catching(dp)
Description It and ) in his field, each full of apples. Bessie cannot reach the apples when they are ...
- poj2385 - Apple Catching【动态规划】
Description It is a little known fact that cows love apples. Farmer John has two apple trees (which ...
- poj2385 Apple Catching
思路: 简单dp. 实现: #include <iostream> #include <cstdio> #include <cstring> using names ...
- dp 动态规划 之C - Apple Catching 简单基础
终于开始写dp了,还很不熟练 It is a little known fact that cows love apples. Farmer John has two apple trees (whi ...
- DP 状态 DP 转移方程 动态规划解题思路
如何学好动态规划(2) 原创 Gene_Liu LeetCode力扣 今天 算法萌新如何学好动态规划(1) https://mp.weixin.qq.com/s/rhyUb7d8IL8UW1IosoE ...
随机推荐
- weex用阿里矢量图
首先这段代码来自 zwwill在github上的 weex网易严选项目 他是在utils下封装了一个方法 let utilFunc = { initIconFont () { let domModul ...
- la 4490
题解: 这道思路还是比较水的 我们可以等价变形成hi<=7 我们的最优决策是把抽出来的那些相同颜色的书最后插在一起(所以要统计序列中还有没有相同元素的书) f[i][j][k][x]表示前i本书 ...
- 【Arduino】开发入门【十】Arduino蓝牙模块与Android实现通信
[Arduino]开发入门[十]蓝牙模块 首先show一下新入手的蓝牙模块 蓝牙参数特点 1.蓝牙核心模块使用HC-06从模块,引出接口包括VCC,GND,TXD,RXD,预留LED状态输出脚,单片机 ...
- device not found解决方案
今天在使用cordova tools进行真机调试的,报了一个错:device not found; 字面意思就是没有发现设备,这时候可以首先尝试重启adb服务 C:\Users\username\Ap ...
- pip安装其他包报错
pip安装时报错 Unknown or unsupported command 'install 一.是否配置了路径 配置了看下面的方法. 二.有多个pip系统不知道调用哪个. 1.where pi ...
- nginx配置2
第四节 nginx 配置文件 1 keepalive_timeout 65; 设定保存长久连接时长 0代表禁止, 若不设置默认是75s 2keepalive_requests nu; 在一 ...
- Codeforces 380D Sereja and Cinema (看题解)
Sereja and Cinema 首先我们可以发现除了第一个人, 其他人都会坐在已入坐人的旁边. 难点在于计算方案数.. 我们可以从外往里把确定的人用组合数算上去,然后缩小范围. #include& ...
- Codeforces 1136E Nastya Hasn't Written a Legend 线段树
vp的时候没码出来.. 我们用set去维护, 每一块区域, 每块区域内的元素与下一个元素的差值刚好为ki,每次加值的时候我们暴力合并, 可以发现我们最多合并O(n)次. 然后写个线段树就没了. #in ...
- 数据特征分析:3.统计分析 & 帕累托分析
1.统计分析 统计指标对定量数据进行统计描述,常从集中趋势和离中趋势两个方面进行分析 集中趋势度量 / 离中趋势度量 One.集中趋势度量 指一组数据向某一中心靠拢的倾向,核心在于寻找数据的代表值或中 ...
- .net面试问答
转载自:https://www.cnblogs.com/dingfangbo/p/5768991.html .net面试问答(大汇总) 原文://http://blog.csdn.net/weny ...