POJ1017 ZOJ1307 UVA311 UVALive5526 Packets【贪心】

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 60322   Accepted: 20447

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 space 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 

Source


Regionals 1996 >> Europe - Central


扫描二维码关注公众号,回复: 510192 查看本文章

问题链接:POJ1017 ZOJ1307 UVA311 UVALive5526 Packets

问题简述

  产品规格是1×12×23×34×45×56×6,每组订单按顺序输入所需要的各种规格的数量,计算需要的6×6的箱子数量。

问题分析

  这个装箱计算问题,自然是贪心法,按6×6、5×5、4×4、3×3、2×2和1×1顺序来。

程序说明

  用模拟法做了一下,逻辑有点繁琐。

  后来又用数学方法做了一下。


AC的C++语言程序(贪心法+数学)如下:

/* POJ1017 ZOJ1307 UVA311 UVALive5526 Packets */

#include <iostream>
#include <stdio.h>

using namespace std;

const int SIX = 6;
int orders[SIX + 1], sum;
int r33[] = {0, 5, 3, 1};   // r33[i]表示,箱子中装入i个3*3之后,剩余2×2空位有几个

int main()
{
    for(;;) {
        sum = 0;
        for(int i = 1; i <= SIX; i++) {
            scanf("%d", &orders[i]);
            sum += orders[i];
        }
        if(sum == 0)
            break;

        // 先计算6×6、5×5、4×4和3×3需要的箱子数量
        sum = orders[6] + orders[5] + orders[4] + (orders[3] + 4 - 1) / 4;

        // 计算4×4和3×3箱子装箱后,空余的2×2的空位有几个
        int r22 = orders[4] * 5 + r33[orders[3] % 4];

        if(r22 < orders[2])
            sum += (orders[2] - r22 + 9 - 1) / 9;   // 1箱可以装入9个2×2

        // 计算5×5、4×4、3×3和2×2全部装箱后,空余的1×1的空位有几个
        int r11 = sum * 36 - orders[6] * 36 - orders[5] * 25 - orders[4] * 16 - orders[3] * 9 - orders[2] * 4;

        if(r11 < orders[1])
            sum += (orders[1] - r11 + 36 - 1) / 36;

        printf("%d\n", sum);
    }

    return 0;
}



AC的C++语言程序(贪心法+模拟法)如下:

/* POJ1017 ZOJ1307 UVA311 UVALive5526 Packets */

#include <iostream>
#include <stdio.h>

using namespace std;

const int SIX = 6;
int orders[SIX + 1], sum, r;

int main()
{
    for(;;) {
        sum = 0;
        for(int i = 1; i <= SIX; i++) {
            scanf("%d", &orders[i]);
            sum += orders[i];
        }
        if(sum == 0)
            break;

        sum = 0;
        // 6 * 6,1个箱子装1个,没有剩余空间
        sum += orders[SIX];

        // 5 * 5,1个箱子装1个,剩余的空间只能放入1×1,尽可能填满
        sum += orders[5];
        if(orders[5]) {
            r = orders[5] * 11;   // 剩余空间,,每个箱子剩余11个1×1空间
            orders[1] = max(0, orders[1] - r);
        }

        // 4 * 4,1个装1个,剩余的空间尽可能先装入2×2,再剩余空间经可能装1×1
        sum += orders[4];
        if(orders[4]) {
            r = orders[4] * 5;   // 剩余空间,每个箱子剩余5个2×2空间
            if(r <= orders[2])
                orders[2] -= r;
            else {
                r -= orders[2];
                r *= 4;         // 剩余空间,每个2×2空间=4个1×1空间
                orders[1] = max(0, orders[1] - r);
                orders[2] = 0;
            }
        }

        // 3 * 3,1个箱子可以装4个
        sum += (orders[3] + 4 - 1) / 4;
        if(orders[3] % 4) {
            r = 4 - orders[3] % 4;
            if(r == 3) {
                if(orders[2] >= 5) {
                    orders[2] -= 5;
                    r = 7;
                    orders[1] = max(0, orders[1] - r);
                } else {
                    r = (5 - orders[2]) * 4 + 7;
                    orders[1] = max(0, orders[1] - r);
                    orders[2] = 0;
                }
            } if(r == 2) {
                if(orders[2] >= 3) {
                    orders[2] -= 3;
                    r = 6;
                    orders[1] = max(0, orders[1] - r);
                } else {
                    r = (3 - orders[2]) * 4 + 6;
                    orders[1] = max(0, orders[1] - r);
                    orders[2] = 0;
                }
            } if(r == 1) {
                if(orders[2] > 0) {
                    orders[2]--;
                    r = 5;
                } else
                    r *= 9;
                orders[1] = max(0, orders[1] - r);
            }
        }

        // 2 * 2,1个箱子可以装9个
        sum += (orders[2] + 9 - 1) / 9;
        if(orders[2] % 9) {
            r = (9 - orders[2] % 9) * 4;
            orders[1] = max(0, orders[1] - r);
        }

        // 1 * 1,1个箱子可以装6*6=36个
        sum += (orders[1] + 36 - 1) / 36;

        printf("%d\n", sum);
    }

    return 0;
}






猜你喜欢

转载自blog.csdn.net/tigerisland45/article/details/80160616