zoukankan      html  css  js  c++  java
  • poj 1017 装箱子(模拟+贪心)


    Description

     

    A factory produces products packed in square packets of the same height h and of the sizes 1*1, 2*2, 3*3, 4*4, 5*5, 6*6. These products are always delivered to customers in the square parcels of the same height h as the products have and of the size 6*6. Because of the expenses it is the interest of the factory as well as of the customer to minimize the number of parcels necessary to deliver the ordered products from the factory to the customer. A good program solving the problem of finding the minimal number of parcels necessary to deliver the given products according to an order would save a lot of money. You are asked to make such a program.

     

     

     

    Input

     

    The input file consists of several lines specifying orders. Each line specifies one order. Orders are described by six integers separated by one  representing successively the number of packets of individual size from the smallest size 1*1 to the biggest size 6*6. The end of the input file is indicated by the line containing six zeros.

     

     

     

    Output

     

    The output file contains one line for each line in the input file. This line contains the minimal number of parcels into which the order from the corresponding line of the input file can be packed. There is no line in the output file corresponding to the last ``null'' line of the input file.

     

     

     

    Sample Input

     

    0 0 4 0 0 1  

    7 5 1 0 0 0  

    0 0 0 0 0 0

     

    Sample Output

     

    2  

    1

     

     

     

    解题思路:贪心法的思想,同时要仔细画图,分析箱子摆放的细节,从而得出解题的思路。

     

    首先,对于边长为4,5,6的产品,那么只要有一个这样的产品,就必须要给一个边长为6的集装箱。对于边长为4,5的产品还要考虑在剩余的空间中放入尽可能多的2,1产品以节省空间。具体地讲,就是对于产品6,一个装一箱恰好装满;对于产品5,最多只能在剩余空间中装6*6-5*5=11个产品1了。对于产品4而言,装了一个产品4后,剩余空间中最多可以装5个产品2(可画图验证),剩余再考虑装入产品1。思路就是这样的一个由大到小的贪心的过程。

     

    其次,对于产品3,2,1分别考虑:

     

    对于边长为3的产品,四个一箱后会剩余1,2,3这3种可能的余数,分别考虑填放入边长为2,1的产品;

     

    对于边长为2的产品,九个一箱后,剩余多少空格就直接用1产品填充就行。

     

    对最后剩下的边长为1的产品,36个一箱的方案来装就可以了

     

    程序源代码如下:

     

    #include<iostream>

    using namespace std;

     

    int max(int a, int b){

        if(a>b) return a;

        else return b;

    }

     

    int main(){

        int s1, s2, s3, s4, s5, s6;

        while(cin>>s1>>s2>>s3>>s4>>s5>>s6)

    {

    if(s1==0&&s2==0&&s3==0&&s4==0&&s5==0&&s6==0)break;

            int boxs = 0;

            boxs += s6; // 6*6的产品一个装一箱

            

            boxs += s5; // 5*5的产品一个装一箱

            s1 = max(0, s1-11*s5); // 剩余空间用1*1的产品尽量填满

            

            boxs += s4; // 4*4的产品一个装一箱

            if(s2<5*s4) s1 = max(0, s1-(5*s4-s2)); // 假如2*2的产品填完之后仍然有空隙,则用1*1填满

            s2 = max(0, s2-5*s4); // 尽量用2*2的产品填满

            

            boxs += (s3+3)/4; // 3*3的产品四个一箱

            s3 %= 4;            // 假如3*3的箱子不是四的倍数个,则先用2*2填充再用1*1填充

            if(s3==1){

                if(s2<5) s1 = max(0, s1-(27-4*s2));

                else     s1 = max(0, s1-7);

                s2 = max(0, s2-5);

            }

            else if(s3==2){

                if(s2<3) s1 = max(0, s1-(18-4*s2));

                else     s1 = max(0, s1-6);

                s2 = max(0, s2-3);

            }

            else if(s3==3){

                if(s2<1) s1 = max(0, s1-(9-4*s2));

                else     s1 = max(0, s1-5);

                s2 = max(0, s2-1);    

            }

            

            boxs += (s2+8)/9; // 2*2的产品九个一箱

            s2 %= 9;             // 假如2*2的箱子不是九的倍数个,则用1*1填充

            if(s2) s1 = max(0, s1-(36-4*s2));

            

            boxs += (s1+35)/36; // 1*1的产品三十六个一箱

            

            cout<<boxs<<endl;

        }

        

        return 0;

    }


  • 相关阅读:
    ShellExecuteEx 函数说明
    npm
    Byte和char
    如何高效阅读一个项目
    C++中慎用malloc
    #ifdef
    string
    C++与C混合编译
    git@github.com: Permission denied (publickey).
    connect to host github.com port 22: Connection refused
  • 原文地址:https://www.cnblogs.com/linruier/p/9485198.html
Copyright © 2011-2022 走看看