POJ | 1017装箱问题 摸鱼题解

今天是打卡第一台咧,我是有好好想过努力成为大师的。其实是在读那本poj上面的书,最近还在找例题做,话不多说1017上题。


POJ 1017 Packets

[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 


思路

据说这道题是经典的贪心算法, 但是小白入我暂时还是没有依然难理出头绪。目前认为是要从方便入手的顺序从大到小的排列,用6×6的箱子装,看看剩余多少的2×2和1×1的箱子。

需要注意的地方就是3×3箱子的情况,因为他们不可以装进去4×4,5×5,6×6装过的箱子里面所以必须要新开一个箱子。然后分四种情况:

1.箱子里只有一个3×3:可以装下5个2×2和7个1×1

2.箱子里有两个3×3:可以装下3个2×2和6个1×1

3.箱子里有三个3×3:可以装下1个2×2和5个1×1

4.箱子里有四个3×3:没空位置了

只要算好了一层一层地填空位就可以了。还有一个点就是,2×2以及1×1的空位够不够,不够的话要开新箱子。


嗯,其实都是学习得来的,自己摸鱼去了。


代码


#include<stdio.h>

int main(){
    int N,a,b,c,d,e,f;//N: total #of cases
    int x,y; //x: #of 1*1; y: #of 2*2
   
    int u[4]={0,5,3,1};
    while(1){
        N=0;
        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 trick: ceiling of c/4
        y=d*5+u[c%4];
        if(b>y) N+=(b-y+8)/9;
        //Use subtraction to calculate the remaining 1*1
        x=36*N-36*f-25*e-16*d-9*c-4*b;
        if(a>x) N+=(a-x+35)/36;
        printf("%d\n",N);
    }
    return 0;
}



明天再加油咯!



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值