题目描述
有一个箱子的容量为V,有N个物品,每个物品都有一个体积,要求在这N个物品中使箱子剩余的体积最小。
样例输入
24
6
8
3
12
7
9
7
样例输出
0
思路
O(nm)
连续写了4题关于动态规划的题目,但毫不例外的都是01背包,能不能再简单一点,我也是没看出有多少改变。
跟采药相比也就少了个每个物品的价值,但物品的体积也可以看作是它的价值,最后总体积减去最大体积就是答案,方程就变成了:
f[j]:=max(f[j],f[j-a[i]]+a[i]);
还是有点变化的啊~~~~
var
n,m,i,j:longint;
a,b,f:array[0..20000]of longint;
begin
readln(n);
readln(m);
for i:=1 to m do
readln(a[i]);
for i:=1 to m do
for j:=n downto a[i] do
if f[j-a[i]]+a[i]>f[j] then
f[j]:=f[j-a[i]]+a[i];
writeln(n-f[n]);
end.