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;

}

poj 1017 装箱子(模拟+贪心)的更多相关文章

  1. poj 1017 装箱子问题 贪心算法

    题意:有1*1到6*6的的东西,需要用6*6的箱子将它们装起来.问:至少需要多少个6*6箱子 思路: 一个瓶子怎么装东西最多?先装石头,在装沙子,然后装水. 同样放在本题就是先装6*6然后5*5... ...

  2. POJ 2260 Error Correction 模拟 贪心 简单题

    Error Correction Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6825   Accepted: 4289 ...

  3. POJ 1017 Packets【贪心】

    POJ 1017 题意: 一个工厂制造的产品形状都是长方体,它们的高度都是h,长和宽都相等,一共有六个型号,他们的长宽分别为 1*1, 2*2, 3*3, 4*4, 5*5, 6*6.  这些产品通常 ...

  4. POJ - 1017 贪心训练

    Packets Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 59725   Accepted: 20273 Descrip ...

  5. Greedy:Packets(POJ 1017)

    装箱问题1.0 题目大意:就是一个工厂制造的产品都是正方形的,有1*1,2*2,3*3,4*4,5*5,6*6,高度都是h,现在要包装这些物品,只能用6*6*h的包装去装,问你怎么装才能使箱子打到最小 ...

  6. POJ 1017:Packets

    Packets Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 47513   Accepted: 16099 Descrip ...

  7. poj 1017 Packets 裸贪心

    Packets Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 43189   Accepted: 14550 Descrip ...

  8. Poj 1017 Packets(贪心策略)

    一.题目大意: 一个工厂生产的产品用正方形的包裹打包,包裹有相同的高度h和1*1, 2*2, 3*3, 4*4, 5*5, 6*6的尺寸.这些产品经常以产品同样的高度h和6*6的尺寸包袱包装起来运送给 ...

  9. POJ 1922 Ride to School(贪心+模拟)

    题意:起点与终点相隔4500米.现Charley 需要从起点骑车到终点.但是,他有个习惯,沿途需要有人陪伴,即以相同的速度, 与另外一个人一起骑.而当他遇到以更快的速度骑车的人时,他会以相应的速度跟上 ...

随机推荐

  1. [转贴]Linux内核LTS长期支持版生命周期

    Linux内核LTS长期支持版生命周期 https://blog.51cto.com/dangzhiqiang/1894026 搞不懂长期支持版本的特点和区别. 党志强关注0人评论4371人阅读201 ...

  2. SpreadJS与Vue集成,苏宁集团『极客办公』系统开发案例

    “造极”如今已成为苏宁集团的年度核心关键词.“造极”在具体工作上的体现,代表着苏宁不断追求极致的工匠精神,即对待每一个环节,都要严格要求.精益求精.“极客办公”系统,正是在这种环境下应运而生.本期公开 ...

  3. Git_基础命令

    gitinit//初始化一个Git仓库" role="presentation">gitinit//初始化一个Git仓库gitinit//初始化一个Git仓库 gi ...

  4. tensorflow 使用tfrecords创建自己数据集

    直接采用矩阵方式建立数据集见:https://www.cnblogs.com/WSX1994/p/10128338.html 制作自己的数据集(使用tfrecords) 为什么采用这个格式? TFRe ...

  5. 通过Playbook部署LAMP

    Ansible的PlayBook文件格式为YAML语言,所以希望你在编写PlayBook前对YAML语法有一定的了解,否则在运行PlayBook的时候经常碰到语法错误提示,这里我们通过介绍批量部署LA ...

  6. js 条件方法、数组方法

    经常写代码写的很多很累赘,看看下面例子,争取以后代码简洁简化.个人也觉得简洁分明的代码很重要. 本文来自另一篇博客:https://www.cnblogs.com/ljx20180807/p/1084 ...

  7. 谷歌浏览器(Chrome)离线包的下载方法!

    谷歌浏览器(Chrome)其实可以下载离线包,用离线包安装的好处,就是一次获得全部安装文件,不需要漫长的在线下载过程了! 下载地址:https://www.google.com/chrome/eula ...

  8. 转载:Cesium的Property机制总结

    转自:https://www.jianshu.com/p/f0b47997224c 前言 Cesium官方教程中有一篇叫<空间数据可视化>(Visualizing Spatial Data ...

  9. linux无界面模式安装selenium+chrome+chromedriver并成功完成脚本(亲测可用)

    环境:docker centos 7.4 能通外网 写好的selenium脚本. 具体步骤: 一:安装selenium  这是最简单的 直接利用 pip3 install selenium 二 安装c ...

  10. linux添加开机启动项、登陆启动项、定时启动项、关机执行项等的方法

    使用chkconfig命令可以查看在不同启动级别下课自动启动的服务(或是程序),命令格式如下: chkconfig --list 可能输出如下: network         0:off   1:o ...