装箱问题

    xiaoxiao2021-03-26  24

    题意

    要求n个物品中,任取若干个装入箱内,使箱子的剩余空间为最小。

    分析

    这题跟装载问题差不多,只不过这题是输出剩余空间。

    先排序,再从大到小搜。

    var n,c,i,tao,t,tj:longint; b,s:array[0..5000]of longint; procedure kp(l,r:longint); var i,j,mid:longint; begin     if l>=r then exit;     i:=l;j:=r;mid:=s[(l+r) div 2];     repeat          while s[i]>mid do inc(i);          while s[j]<mid do dec(j);          if i<=j then          begin              s[0]:=s[i];s[i]:=s[j];s[j]:=s[0];              inc(i);dec(j);          end;     until i>j;     kp(l,j);     kp(i,r); end; procedure search(dep,t:longint); begin     if t<c then if t>tao then tao:=t;     if t=c then     begin         write(0);         halt;     end;     if (t>c)or(dep>n) then exit;     if (t+s[dep])<=c then search(dep+1,t+s[dep]);     search(dep+1,t); end; begin     readln(c);     readln(n);     fillchar(b,sizeof(b),0);     for i:=1 to n do     readln(s[i]);     kp(1,n);     tao:=0;     search(1,0);     write(c-tao); end.

    转载请注明原文地址: https://ju.6miu.com/read-658356.html

    最新回复(0)