poj3628
来源:互联网 发布:手机刷机数据恢复 编辑:程序博客网 时间:2024/05/17 09:26
Description
Farmer John recently bought another bookshelf for the cow library, but the shelf is getting filled up quite quickly, and now the only available space is at the top.
FJ has N cows (1 ≤ N ≤ 20) each with some height of Hi (1 ≤ Hi ≤ 1,000,000 - these are very tall cows). The bookshelf has a height of B (1 ≤ B ≤ S, where S is the sum of the heights of all cows).
To reach the top of the bookshelf, one or more of the cows can stand on top of each other in a stack, so that their total height is the sum of each of their individual heights. This total height must be no less than the height of the bookshelf in order for the cows to reach the top.
Since a taller stack of cows than necessary can be dangerous, your job is to find the set of cows that produces a stack of the smallest height possible such that the stack can reach the bookshelf. Your program should print the minimal 'excess' height between the optimal stack of cows and the bookshelf.
Input
* Line 1: Two space-separated integers: N and B
* Lines 2..N+1: Line i+1 contains a single integer: Hi
Output
* Line 1: A single integer representing the (non-negative) difference between the total height of the optimal set of cows and the height of the shelf.
Sample Input
5 1631356
Sample Output
1
///f[牛的高度] = 书架高度///理解错了, 原来叠罗汉的高度范围是当前这只牛的高度到所有牛的高度和#include <iostream>#include<cstring>#define INF 65535using namespace std;int f[50];int h[50];int sum;int main(){ int n, b; cin >> n >> b; memset(f, 0, sizeof(f)); for(int i=1; i<=n; i++) { cin >> h[i]; sum += h[i]; } f[0] = 0; for(int i=1; i<=n; i++) { for(int j=sum; j>=h[i]; j--) { f[j] = max(f[j], f[j-h[i]]+h[i]); } } int ans = INF; for(int i=b; i<=sum; i++) { if(f[i] > b) ans = min(ans, f[i]-b); } cout << ans << endl; return 0;}/*5 1631356*/
- POJ3628
- poj3628
- poj3628
- poj3628
- poj3628
- POJ3628 Bookshelf2
- poj3628 Bookshelf2
- poj3628(01背包问题)
- POJ3628 Bookshelf 2
- poj3628(背包变形)
- POJ3628 背包问题
- POJ3628 Bookshelf 2
- 01背包-poj3628
- poj3628-Bookshelf 2
- poj3628解题报告
- POJ3628 01背包
- POJ3628:Bookshelf 2【01背包】
- POJ3628:Bookshelf 2【01背包】
- 完美 定义popwindow
- 调整数组顺序使奇数位于偶数前面
- 双向链表
- 夜间模式
- 四、封装OkHttp
- poj3628
- 安卓 toast
- Servlet(1)
- 传智播客黑马程序员骗局真相揭秘
- 解决:Error running app: Instant Run requires 'Tools | Android | Enable ADB integration' to be enabled.
- CentOS6.5上Boost库编译安装全记录
- http://wxb-j2ee.javaeye.com/blog/797958
- 动态的创建ImageView,并设置图片自适应
- Android WebView使用深入浅出