poj 3628 Bookshelf 2 (0/1背包)
来源:互联网 发布:校园网绑定mac地址 编辑:程序博客网 时间:2024/04/28 13:15
点击打开链接poj 3628
思路: 0/1背包
分析:
1 题目抽象出来的话就是0/1背包
2 我们用dp[i][j]表示前i头牛放在高度为j的最小高度,那么我们只要求出
dp[n][B~s]中的最小值,然后减去B即可
代码:
#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespace std;const int INF = 0x3f3f3f3f;const int N = 20;int n , B , s;int v[N] , dp[N*1000001]; int solve(){ for(int i = 0 ; i <= s ; i++) dp[i] = INF; dp[0] = 0; for(int i = 1 ; i <= n ; i++) for(int j = s ; j >= v[i] ; j--) dp[j] = min(dp[j] , dp[j-v[i]]+v[i]); int ans = INF; for(int i = B ; i <= s ; i++) if(dp[i] >= B) ans = min(ans , dp[i]); return ans-B;}int main(){ while(scanf("%d%d" , &n , &B) != EOF){ s = 0; for(int i = 1 ; i <= n ; i++){ scanf("%d" , &v[i]); s += v[i]; } printf("%d\n" , solve()); } return 0;}
- poj 3628 Bookshelf 2 (0/1背包)
- POJ 3628 Bookshelf 2 0-1背包
- POJ 3628 Bookshelf 2(0-1背包)
- poj 3628 Bookshelf 2( 0-1背包。)
- POJ 3628 Bookshelf 2 DFS 0-1背包问题
- POJ 3628 Bookshelf 2 0-1背包 or DFS 水题
- poj 3628 Bookshelf 2(0/1背包)
- POJ 3628 Bookshelf 2(背包)
- poj 3628 Bookshelf 2 背包
- poj 3628 Bookshelf 2 01背包
- poj 3628 Bookshelf 2 01背包!!!
- poj 3628 Bookshelf 2 01背包
- POJ 3628 Bookshelf 2 (01背包)
- [01背包]POJ 3628 Bookshelf 2
- POJ 3628 Bookshelf 2(DP:01背包)
- poj 3628 Bookshelf 2 01背包
- POJ 3628 Bookshelf 2 (01背包)
- poj 3628 Bookshelf 2(01背包)
- 总结一下以前的工作中遇到的问题1
- junit测试servlet
- IE9 : DOM Exception: INVALID_CHARACTER_ERR (5)
- jQuery插件之表单验证插件validationEngine(个人感觉好用)
- Entity Framework 4.1 DbContext使用记之二——如何玩转本地实体? DbSet.Local属性的使用与实现
- poj 3628 Bookshelf 2 (0/1背包)
- Python概述:C++程序员眼中的Python
- MVC3+EF4.1学习系列(十一)----EF4.1常见的问题解决
- JSP中的EL表达式详细介绍
- 开始加大学习强度 开启新的模式
- ASP.NET MVC3: Razor中的布局
- 安装Intellij,编译第一个android程序出错
- -128有8位二进制表示的原码、反码和补码
- hdoj_4417Super Mario