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.

  • 相关阅读:
    观察者模式
    工厂模式
    单例模式
    关于状态机
    关于memset的错误使用
    关于STL容器
    关于内存及其相关
    python学习手册:第十一章——赋值表达式及打印
    python学习手册:第九章——元组、文件及其他
    python学习手册:第七章——字符串
  • 原文地址:https://www.cnblogs.com/YYC-0304/p/9500199.html
Copyright © 2011-2022 走看看