nyoj 234 吃土豆
描述
Bean-eating is an interesting game, everyone owns an M*N matrix, which is filled with different qualities beans. Meantime, there is only one bean in any * grid. Now you want to eat the beans and collect the qualities, but everyone must obey by the following rules: if you eat the bean at the coordinate(x, y), you can’t eat the beans anyway at the coordinates listed (if exiting): (x, y-), (x, y+), and the both rows whose abscissas are x- and x+.
Now, how much qualities can you eat and then get ?
- 输入
-
There are a few cases. In each case, there are two integer M (row number) and N (column number). The next M lines each contain N integers, representing the qualities of the beans. We can make sure that the quality of bean isn't beyond 1000, and 1<=M,N<=500.
- 输出
-
For each case, you just output the MAX qualities you can eat and then get.
- 样例输入
-
- 样例输出
-
思路:
考虑对于某行某列元素,row[i][j]表示加上位置为i,j的土豆的质量的i行j列最大的和
列的最大值:row[i][j]=max(row[i][j-2]+row[i][j-3])+val
看图说话:
假设红色的格子为i行j列,那么它的前面有两种选择方案:
1、选择蓝色格子
2、选择黄色格子
那么该行最大的和是什么呢?
由于n列、n-1列具有状态无关性(n-1列的状态影响不了n列的状态),很显然等于max(row[i][n],row[i][n-1])
同理对于dp[i] (i行的最大值)
dp[i]=max(dp[i-2],dp[i-3])+max_row[i]
看图说话:
max土豆质量=max(dp[m],dp[m-1])
为了方便计算,我的代码把n,m扩大了2
AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
#define N 506
int n,m;
int col[N][N];
int dp[N];
int main()
{
while(scanf("%d%d",&n,&m)==){
memset(col,,sizeof(col));
memset(dp,,sizeof(dp));
for(int i=;i<n+;i++){
for(int j=;j<m+;j++){
int x;
scanf("%d",&x);
col[i][j] = max(col[i][j-],col[i][j-])+x;
}
}
for(int i=;i<n+;i++){
dp[i]=max(dp[i-],dp[i-])+max(col[i][m+],col[i][m+]);
}
printf("%d\n",max(dp[n+],dp[n+]));
}
return ;
}
nyoj 234 吃土豆的更多相关文章
- nyoj 吃土豆
吃土豆 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描述 Bean-eating is an interesting game, everyone owns an M* ...
- nyoj234 吃土豆 01背包
思路:假设我们先只考虑一行,规则就是取了i处的土豆,每一个土豆有两种选择,拿与不拿,那么i-1和i+1处的土豆都不能再取,那么要求某一行的最大取值就用一次动态规划即可,dp(i)表示前i个土豆能取得的 ...
- nyoj 对决吃桃
时间限制:3000 ms | 内存限制:65535 KB 难度:0 描述 有一堆桃子不知数目,猴子第一天吃掉一半,又多吃了一个,第二天照此方法,吃掉剩下桃子的一半又多一个,天天如此,到第m天早 ...
- 设计模式(二)简单工厂模式(Simple Factory Pattern)
一.引言 这个系列也是自己对设计模式的一些学习笔记,希望对一些初学设计模式的人有所帮助的,在上一个专题中介绍了单例模式,在这个专题中继续为大家介绍一个比较容易理解的模式——简单工厂模式. 二.简单工厂 ...
- head first 设计模式读书笔记 之 策略模式
作为一个php开发者,深知曾经很多程序员都鄙视php,为什么呢?因为他们认为php的语法是dirty的,并且由于开发者水平参差不齐导致php的代码更加乱上加乱,维护起来简直一坨shit一样.随着php ...
- C#设计模式之二简单工厂模式(过渡模式)
一.引言 之所以写这个系列,是了为了自己更好的理解设计模式,也为新手提供一些帮助,我都是用最简单的.最生活化的实例来说明.在上一篇文章中讲解了单例模式,今天就给大家讲一个比较简单的模式--简单工厂模式 ...
- gensim和jieba分词进行主题分析,文本相似度
参考链接:https://blog.csdn.net/whzhcahzxh/article/details/17528261 demo1:结巴分词: # 构造分词库,格式如下: ''' [['楼下', ...
- ThreadPoolExecutor 入参 corePoolSize 和 maximumPoolSize 的联系
前言 我们可以通过 java.util.concurrent.ThreadPoolExecutor 来创建一个线程池: new ThreadPoolExecutor(corePoolSize, max ...
- C#设计模式(2)——简单工厂模式(转)
C#设计模式(2)——简单工厂模式 一.引言 这个系列也是自己对设计模式的一些学习笔记,希望对一些初学设计模式的人有所帮助的,在上一个专题中介绍了单例模式,在这个专题中继续为大家介绍一个比较容易理 ...
随机推荐
- (转)iOS7界面设计规范(13) - UI基础 - 与iOS的系统整合
突然就到了周日傍晚.你永远不会知道自己的生活在接下来的一周当中能够发生多少变化:各种不可预知性所带来的更多是快感还是焦虑与不安,冷暖自知.相比之下,白天工作当中那些需求列表与排期文档就显得那么可爱了, ...
- [RxJS] Updating Data with Scan
You often need to update the data flowing through the stream with custom logic based on what you nee ...
- IPMITOOL常用操作指令
一.开关机,重启 1. 查看开关机状态: ipmitool -H (BMC的管理IP地址) -I lanplus -U (BMC登录用户名) -P (BMC 登录用户名的密码) power statu ...
- 《第一行代码》学习笔记16-碎片Fragment(1)
1.碎片( Fragment):一种可以嵌入在活动当中的UI片段,能让程序更加合理和充分地利用大屏幕的空间,在平板上的应用广泛. 2.碎片同样包括布局,有自己的生命周期,甚至可理解成一个迷你型的活动. ...
- Oracle关联查询关于left/right join的那点事
/*题外话 --更改foreign key约束定义引用行(delete cascade/delete set null/delete no action),默认delete on action--引用 ...
- iOS的Bundle资源束制作
在静态库的制作中,很多时候我们的静态库也是带着文件,图片和多媒体资源的. 若只是直接加入到项目中也是可以,但是,考虑到方便管理(方便插件使用者的管理),我们希望把插件的资源文件打成一个包来管理. 当然 ...
- 阐述Lambada表达式
在C#2.0引入匿名方法之前,声明委托的唯一方法就是使用命名方法,C#2.0之后的C#3.0中开始引入了Lambda表达式取代了匿名方法. 匿名方法 要说Lambda必然离不开匿名方法,实际上,Lam ...
- DataTable转json字符串,jQuery.parseJSON()把json字符串转为标准的json对象格式
1.string res = DataTableToJson.DataTable2Json(dt);讲DataTable转换为json字符串 http://www.365mini.com/page/j ...
- Cogs 12 运输问题2 (有上下界网络流)
#include <cstdlib> #include <algorithm> #include <cstring> #include <iostream&g ...
- QT5-控件-QDateEdit 和 日期类QDate
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <QDate> #inclu ...