【DP】poj2184
来源:互联网 发布:壁虎数据恢复破解版 编辑:程序博客网 时间:2024/06/09 16:53
这个网上大多数都是用一种偏移的思想做的,然而这样的话dp要分两种来做,但是如果运用滚动数组的话,既能使空间得到优化,也可以使dp只需要正向做一次就可以。
f[1][j]的状态用f[0][k]来转移,这样就不会出现多次填装的问题。
然而需要注意一点的是,f[1]一开始要初始化成和f[0]一样的。。。。因为这个问题wa了很多炮。。。
#include<iostream>#include<cstdio>#include<cstdlib>#include<cstring>#include<cmath>#include<algorithm>using namespace std;int f[2][300100];int main(){ int x,y,n; int ans; while (scanf("%d",&n)!=EOF) { ans=0; memset(f,-127,sizeof(f)); f[0][150000]=0; f[1][150000]=0; for (int i=1;i<=n;i++) { scanf("%d%d",&x,&y); for (int j=0;j<=300000;j++) if (j-x>=0&&f[0][j-x]>-20000000) f[1][j]=max(f[1][j],f[0][j-x]+y); for (int j=0;j<=300000;j++) f[0][j]=f[1][j]; } for (int i=150000;i<=300000;i++) if ((f[0][i]>=0)&&(f[0][i]+i-150000>ans)) ans=f[0][i]+i-150000; printf("%d\n",ans); } return 0;}这么写应该比通常的题解短一些。。。而且比较方便想
0 0
- 【DP】poj2184
- POJ2184
- poj2184
- poj2184
- poj2184
- poj2184
- POJ2184 Cow Exhibition(DP:变种01背包)
- 背包 poj2184
- POJ2184-Cow Exhibition
- poj2184 Cow Exhibition
- 背包问题(poj2184)
- poj2184:cow exhibition
- poj2184(01背包变形)
- poj2184 Cow Exhibition
- 【POJ2184】Cow Exhibition
- poj2184 Cow Exhibition
- poj2184解题报告
- 【poj2184】【Cow Exhibition】
- 黑马程序员----Java中如何使用注释
- OJ-对象转换
- 函数的预解析和逐行解读
- Struts2拦截器
- Android Intent Action有什么用?
- 【DP】poj2184
- Linux命令!!!
- mysql中char,varchar与text类型的区别和选用
- Android-手电筒应用小思路(一)
- [leetcode]爬楼梯的递归和非递归方法
- [Xutils]安卓框架的学习之路
- ADT离线安装(Eclipse)
- 【cocos2d-x】构造函数与初始化
- Redis服务的安装使用