HDU 2845 Beans (最大不连续子序列和)(压缩求解)
2016-07-29 16:13
330 查看
题意:在图中取数,例如取了81之后,同一行的相邻两个不能取,还有81的上面那行和下面那行也不能取,问能取到的最大和是多少?
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <deque>
#include <cmath>
#include <vector>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define L(i) i<<1
#define R(i) i<<1|1
#define INF 0x3f3f3f3f
#define pi acos(-1.0)
#define eps 1e-9
#define maxn 200010
#define MOD 1000000007
int dpx[maxn],dpy[maxn];
int n,m;
int main()
{
int t;
//scanf("%d",&t);
while(scanf("%d%d",&n,&m) != EOF)
{
memset(dpx,0,sizeof(dpx));
memset(dpy,0,sizeof(dpy));
for(int i = 2; i <= n+1; i++)
{
for(int j = 2; j <= m+1; j++)
{
int x;
scanf("%d",&x);
dpx[j] = max(dpx[j-2]+x,dpx[j-1]);
}
dpy[i] = max(dpy[i-2]+dpx[m+1],dpy[i-1]);
}
printf("%d\n",dpy[n+1]);
}
return 0;
}
题解:先求某一行能获得的最大值,然后求所有行能获得的最大值.
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <deque>
#include <cmath>
#include <vector>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define L(i) i<<1
#define R(i) i<<1|1
#define INF 0x3f3f3f3f
#define pi acos(-1.0)
#define eps 1e-9
#define maxn 200010
#define MOD 1000000007
int dpx[maxn],dpy[maxn];
int n,m;
int main()
{
int t;
//scanf("%d",&t);
while(scanf("%d%d",&n,&m) != EOF)
{
memset(dpx,0,sizeof(dpx));
memset(dpy,0,sizeof(dpy));
for(int i = 2; i <= n+1; i++)
{
for(int j = 2; j <= m+1; j++)
{
int x;
scanf("%d",&x);
dpx[j] = max(dpx[j-2]+x,dpx[j-1]);
}
dpy[i] = max(dpy[i-2]+dpx[m+1],dpy[i-1]);
}
printf("%d\n",dpy[n+1]);
}
return 0;
}
相关文章推荐
- HDU 5773 The All-purpose Zero(贪心LIS)
- Android Studio 开发中的一个小功能 使用TextView搜索ListView
- si4438 cca 侦听
- grep、egrep、fgrep
- linux下的定时任务
- Android静态安全检测 -> 主机名弱校验
- 解决Apache CXF 不支持传递java.sql.Timestamp和java.util.HashMap类型问题
- 华为[编程题]最高分是多少
- JavaScript中null和undefined的比较和区别
- mysql定时器
- 命令模式
- [Web基础笔记]JavaScript部分
- Oracle 建立索引及SQL优化
- nginx菜鸟入门三
- 总结一下Android中主题(Theme)的正确玩法
- rabbitmq安装、集群搭建
- CentOS 与 RedHat 关系和区别
- Catch That Cow<poj3278>
- html 校验
- 自动化运维平台搭建(一)