poj 3628 01背包
2013-09-12 21:21
113 查看
这道题目的意思看了好久,看了discuss后,写了一下,竟然WA了,后来看了别人的博客才发现与我所理解的意思刚好相反!! 英语不行真命苦啊!! 这里我大致说一下题目意思: 输入 n 和 b,其中n是指奶牛的数量,b则是指书架的高度,然后奶牛之间叠罗汉,找出超出书架的值中取最小的!! 代码如下: #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int maxn=20000000; const int inf=0x3f3f3f3f; int value[25],cost[25]; int f[maxn]; int n,b; int sum; void zeropack(int value1,int cost1) { for(int i=sum; i>=cost1; i--) { f[i]=max(f[i],f[i-cost1]+value1); } } int main() { int i,j; while(~scanf("%d%d",&n,&b)) { sum=0; memset(value,0,sizeof(value)); memset(f,0,sizeof(f)); for(i=1; i<=n; i++) { scanf("%d",&value[i]); sum+=value[i]; } for(i=1; i<=n; i++) { zeropack(value[i],value[i]); } int ans; int min1; min1=inf; for(i=b; i<=sum; i++) { if(f[i]>=b) { ans=f[i]-b; if(ans<min1) { min1=ans; } } } printf("%d\n",min1); } return 0; }
相关文章推荐
- POJ 3628 Bookshelf 2 (01背包)
- poj 3628 Bookshelf 2(01背包,dfs)
- poj 3628 Bookshelf 2 (01背包)
- poj 3628 Bookshelf 2(01背包)
- 【POJ 3628】Bookshelf 2(01背包)
- 简单01背包问题求解 POJ:3628 Bookshelf 2
- POJ 3628 Bookshelf 2 (01背包)
- POJ 3628 Bookshelf 2【01背包】
- poj 3628 Bookshelf 2 01背包或者dfs
- poj 3628 Bookshelf 2(dfs, 01背包)
- POJ 3628 Bookshelf 2 (01背包)
- POJ 3628 Bookshelf 2(用DFS做01背包)
- poj 3628 Bookshelf 2 01背包
- POJ---3628 Bookshelf 2【01背包】
- 动态规则 01背包问题 poj 1837 3628
- poj 3628 01背包问题
- poj 3628 Bookshelf 2(01背包)
- POJ 3628 01背包 OR 状压
- poj 3628 01背包
- POJ 3628 01背包 OR 状压