http://www.lydsy.com/JudgeOnline/problem.php?id=1606
越来越水了T_T
这题两种做法,一个正规01背包,价值就是体积
还有一种是非正规背包,即
if(f[j-v[i]]) f[j]=1
然后从大向小扫出第一个f[i]==1的,i就是答案
越来越水了啊。。
#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
#define rep(i, n) for(int i=0; i<(n); ++i)
#define for1(i,a,n) for(int i=(a);i<=(n);++i)
#define for2(i,a,n) for(int i=(a);i<(n);++i)
#define for3(i,a,n) for(int i=(a);i>=(n);--i)
#define for4(i,a,n) for(int i=(a);i>(n);--i)
#define CC(i,a) memset(i,a,sizeof(i))
#define read(a) a=getint()
#define print(a) printf("%d", a)
#define dbg(x) cout << #x << " = " << x << endl
#define printarr(a, n, m) rep(aaa, n) { rep(bbb, m) cout << a[aaa][bbb]; cout << endl; }
inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<'0'||c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0'&&c<='9'; c=getchar()) r=r*10+c-'0'; return k*r; }
inline const int max(const int &a, const int &b) { return a>b?a:b; }
inline const int min(const int &a, const int &b) { return a<b?a:b; }
const int N=50000;
int f[N], V, v;
int main() {
read(V); int n=getint();
while(n--) {
read(v);
for(int i=V; i>=v; --i)
f[i]=max(f[i], f[i-v]+v);
}
print(f[V]);
return 0;
}
Description
约翰遭受了重大的损失:蟑螂吃掉了他所有的干草,留下一群饥饿的牛.他乘着容量为C(1≤C≤50000)个单位的马车,去顿因家买一些干草. 顿因有H(1≤H≤5000)包干草,每一包都有它的体积Vi(l≤Vi≤C).约翰只能整包购买,
他最多可以运回多少体积的干草呢?
Input
第1行输入C和H,之后H行一行输入一个Vi.
Output
最多的可买干草体积.
Sample Input
7 3 //总体积为7,用3个物品来背包
2
6
5
2
6
5
The wagon holds 7 volumetric units; three bales are offered for sale with
volumes of 2, 6, and 5 units, respectively.
Sample Output
7 //最大可以背出来的体积
本文探讨了一个基于01背包算法的干草购买问题。在一个容量为C的马车中,如何通过整包购买的方式,从H包不同体积的干草中,找出能装下的最大体积干草组合。文章提供了详细的算法实现,包括如何使用01背包算法进行求解,并附带了完整的代码示例。
2241

被折叠的 条评论
为什么被折叠?



