现在的位置: 首页 > 综合 > 正文

UVa 311 – Packets

2013年08月26日 ⁄ 综合 ⁄ 共 2937字 ⁄ 字号 评论关闭

链接:

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=247

原题:

A factory produces products packed in square packets of the same height h and of the sizes tex2html_wrap_inline27 , tex2html_wrap_inline29 , tex2html_wrap_inline31 , tex2html_wrap_inline33 ,tex2html_wrap_inline35 , tex2html_wrap_inline37 .
These products are always delivered to customers in the square parcels of the same height h as the products have and of the size tex2html_wrap_inline37 .
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 tex2html_wrap_inline27 to the biggest size tex2html_wrap_inline37 .
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

题目大意:

有1*1,2*2,3*3,4*4,5*5,6*6大小的盒子,要把它们装到6*6的盒子里,它们的高度都是相同的。求用最少的6*6盒子把所有尺寸的盒子都装起来。

分析与总结:

这一题是我在Volume
4. Algorithm Design
这个专题中最后才做的一题,主要是因为没有思路。最好还是参考了别人的思路。


6*6的盒子中可以由各种尺寸的盒子来填满。可以有以下这些情况:

1个6*6

1个5*5+11个1*1

1个4*4+5个2*2(有空隙时优先放置2*2,如果没放完2*2的,剩下的就放置1*1) 

放置3*3时,组合情况比较复杂。 没有放完3*3时,剩下的空隙也是优先尽可能多地放置2*2
当放置1个3*3时,还可以放置7个1*1和5个2*2
当放置2个3*3时,还可以放置6个1*1和3个2*2
当放置3个3*3时,还可以放置5个1*1和1个2*2

因为一个4*4,5*5,6*6只能放置在一个盒子里,所以有多少个这些,就需要多少个盒子来装。

然后因为3*3的可以和1*1和2*2的组合放置,所以也可以确定装完3*3需要的盒子。


那么在计算时,首先就可以确定放完3*3,4*4,5*5,6*6的盒子个数,假设是t个盒子。

然后这t个盒子中会有空隙,再优先把2*2的放置到t个盒子中的空隙中。如果这些空隙不能放完2*2的,那么就需要再增加盒子知道放完为止。最后在考虑放置1*1。

代码:

/*
 * UVa: 311 - Packets
 * Time: 0.008s
 * Author: D_Doubel
 *
 */
#include<iostream>
#include<cstdio>
using namespace std;
int arr[7];

// 放置3*3的在6*6中的组合情况,优先放置更多的2*2
// 当放置1个3*3时,还可以放置7个1*1和5个2*2
// 当放置2个3*3时,还可以放置6个1*1和3个2*2
// 当放置3个3*3时,还可以放置5个1*1和1个2*2
int three[4][2]= {{0,0},{7,5},{6,3},{5,1}};

int main(){
    while(~scanf("%d%d%d%d%d%d",&arr[1],&arr[2],&arr[3],&arr[4],&arr[5],&arr[6])){
        if(!arr[1]&&!arr[2]&&!arr[3]&&!arr[4]&&!arr[5]&&!arr[6])break;

        int ans=(arr[3]+3)/4+arr[4]+arr[5]+arr[6]; // 3*3,4*4,5*5,6*6一起用了几个

        arr[1] -= arr[5]*11; //6*6的可以放置一个5*5和11个1*1,所以直接优先把1*1和5*5放在一个盒子里

        int left_for_2=three[arr[3]%4][1]+arr[4]*5;//已用的盒子中的空隙中共还可以放置多少个2*2

        if(arr[2]<=left_for_2){ // 当已用盒子中的空隙可以放置完2*2盒子时
            arr[1] -= three[arr[3]%4][0]; // 在空隙中与2*2组合的可以放置的1*1个数
            arr[2] -= left_for_2;
            arr[1] += arr[2]*4; // 如果2*2是负数,那说明负数的绝对值是空隙,可以用来放置1*1
            if(arr[1]>0) // 如果1*1还有,那么要增加盒子
                ans += (arr[1]+35)/36;
        } 
        else{  // 不能放置完2*2
            arr[1] -= three[arr[3]%4][0]; //之前和2*2组合一起填满盒子的1*1
            arr[2] -= left_for_2;
            ans += (arr[2]+8)/9;  // 再增加盒子直到能放完2*2
            arr[1] -= (9-arr[2]%9)*4; // 增加的盒子中可能不能占满2*2,还有空位可以放置1*1
            if(arr[1]>0)
                ans += (arr[1]+35)/36;
        }
        printf("%d\n", ans);
    }
    return 0;
}

——  生命的意义,在于赋予它意义。

          
     原创 http://blog.csdn.net/shuangde800 , By   D_Double  (转载请标明)

抱歉!评论已关闭.