暑假集训第二周——贪心 C -装箱


C - 装箱
Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u

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 
分析与总结:

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

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


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include<stdio.h>
#include<iostream>
using namespace std;
int main()
{
    int N,a,b,c,d,e,f,y,x;//Y存储2*2的空位数目,X存储1*1空位数目
int u[4]={0,5,3,1};//数组U表示3*3的产品对4取余的余数时,2*2的空位个数
while(scanf("%d%d%d%d%d%d",&a,&b,&c,&d,&e,&f))
{
    if(a==0&&b==0&&c==0&&d==0&&e==0&&f==0)
    break;
    N=f+e+d+(c+3)/4;//(c+3)/4 正好等于除以4向上取整的结果;
    y=5*d+u[c%4];
    if(b>y)
        N+=(b-y+8)/9;
    x=36*N-36*f-25*e-16*d-9*c-4*b;
    if(a>x)
        N+=(a-x+35)/36;//必须+35再去除以36,这样才能是向上取整,不能取整后加一
    printf("%d\n",N);
}
return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值