zoukankan      html  css  js  c++  java
  • 装箱问题

    题意

    要求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.

  • 相关阅读:
    汽车过沙漠问题【学习笔记】
    极值问题
    双色汉诺塔【分离型】
    二分查找(递归与非递归)
    t2712:字符串移位包含问题
    酒瓶和瓶盖换购新酒
    反转诗句
    字符串的展开
    回文子串[输出所有回文子串]
    最长最短单词
  • 原文地址:https://www.cnblogs.com/YYC-0304/p/9500199.html
Copyright © 2011-2022 走看看