先装大的,若未装满一箱,就考虑尽可能地先装(能装下的较大的)。
var a:array[1..6]of longint;
n,i,j:longint;
begin
for i:=1 to 6 do read(a[i]);
for i:=6 downto 1 do
if a[i]>0 then
begin
if i=6 then inc(n,a[i])
else if i=5 then begin inc(n,a[i]); dec(a[1],a[i]*11) end
else if i=4 then
begin
inc(n,a[i]); j:=a[i]*5;
if a[2]<=j then begin dec(j,a[2]); a[2]:=0 end
else begin dec(a[2],j); j:=0 end;
dec(a[1],4*j);
end
else if i=3 then
begin
inc(n,(a[3]+3)div 4);
j:=9*(4-a[3] mod 4);
if j=27 then
begin
if a[2]<=5 then begin dec(j,4*a[2]); a[2]:=0 end
else begin dec(a[2],5); j:=7 end;
dec(a[1],j);
end
else if j=18 then
begin
if a[2]<=3 then begin dec(j,4*a[2]); a[2]:=0 end
else begin dec(a[2],3); j:=6 end;
dec(a[1],j);
end
else begin
if a[2]<=1 then begin dec(j,4*a[2]); a[2]:=0 end
else begin dec(a[2]); j:=5 end;
end;
end
else if i=2 then
begin
inc(n,(a[2]+8)div 9);
j:=4*(9-a[2] mod 9);
dec(a[1],j);
end
else inc(n,(a[1]+35) div 36);
end;
writeln(n);
end.
只写思路可以吗,因为算法是模拟贪心,没必要写代码。
考虑贪心,因为每件物品肯定都要放,而且一些大的物品不能共存(比如说4*4和4*4肯定是在不同的箱子里),那么从这些不能共存的物品可以先推出一部分箱子;
继续按照从大到小的顺序来贪,如果当前物品能放在前面被计算得到的箱子中(也是从前往后考虑箱子),那就放,否则就新开一个箱子。
算法结束#