POJ 1017 Packets
Packets
Time Limit: 1000MS |
Memory Limit: 10000K |
|
---|---|---|
Total Submissions: 53686 |
Accepted: 18250 |
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
题目链接:http://poj.org/problem?id=1017
解题思路 这个问题描述得比较清楚,我们在这里只解释一下输入输出样例:共有两组有效输入, 第一组表示有4 个3*3 的产品和一个6*6 的产品,此时4 个 3*3 的产品占用一个箱子,另外 一个 6*6 的产品占用 1 个箱子,所以箱子数是 2;第二组表示有7 个 1*1 的产品,5 个 2*2 的产品和1 个 3*3 的产品,我们可以把他们统统放在一个箱子中,所以输出是1。 分析六个型号的产品占用箱子的具体情况如下:6*6的产品每个会占用一个完整的箱 子,并且没有空余空间;5*5 的产品每个占用一个新的箱子,并且留下 11 个可以盛放 1*1 的产品的空余空间;4*4 的产品每个占用一个新的箱子,并且留下5 个可以盛放2*2 的产品 的空余空间;3*3 的产品情况比较复杂,首先3*3 的产品不能放在原来盛有5*5 或者4*4 的箱子中,那么必须为3*3 的产品另开新的箱子,新开的箱子数目等于3*3 的产品的数目除以 4 向上取整;同时我们需要讨论为3*3 的产品新开箱子时,剩余的空间可以盛放多少2*2 和 1*1 的产品(这里如果有空间可以盛放2*2 的产品,我们就将它计入2*2 的空余空间,等到 2*2 的产品全部装完,如果还有2*2 的空间剩余,再将它们转换成 1*1 的剩余空间)。我们 可以分情况讨论为3*3 的产品打开的新箱子中剩余的空位,共为四种情况:第一种,3*3 的 产品的数目正好是4 的倍数,所以没有空余空间;第二种,3*3 的产品数目是4 的倍数加1, 这时还剩 5 个2*2 的空位和7 个 1*1 的空位;第三种,3*3 的产品数目是4 的倍数加2,这 时还剩3 个2*2 的空位和6 个 1*1 的空位;第四种,3*3 的产品数目是4 的倍数加3,这时 还剩 1 个 2*2 的空位和5 个 1*1 的空位;处理完3*3 的产品,就可以比较一下剩余的2*2 的空位和2*2 产品的数目,如果产品数目多,就将2*2 的空位全部填满,再为2*2 的产品打 开新箱子,同时计算新箱子中 1*1 的空位,如果剩余空位多,就将2*2 的产品全部填入2*2 的空位,再将剩余的 2*2 的空位转换成 1*1 的空位;最后处理 1*1 的产品,比较一下 1*1 的空位与1*1 的产品数目,如果空位多,将1*1 的产品全部填入空位,否则,先将1*1 的空 位填满,然后再为 1*1 的产品打开新的箱子。
下面给出AC代码:
1 #include <stdio.h>
2 int main()
3 {
4 int a,b,c,d,e,f;
5 int y,x;/*y用来存储2*2的空位数目,x用来存储1*1的空位数目*/
6 int N;/*N用来存储需要的箱子数目*/
7 int u[4]={0,5,3,1};/*数组u表示3*3的产品数目分别是4的倍数,4的倍数+1,4的倍数加2,4的倍数+3时,为3*3的产品打开的新箱子中剩余的2*2的空位个数*/
8 while(1)
9 {
10 scanf("%d%d%d%d%d%d",&a,&b,&c,&d,&e,&f);
11 if(a==0&&b==0&&c==0&&d==0&&e==0&&f==0)
12 break;
13 N=f+e+d+(c+3)/4;//这里有一个小技巧:(c+3)/4正好是c/4向上取整的结果
14 y=5*d+u[c%4];//计算2*2的空位数目
15 if(b>y)//需求>提供
16 N+=(b-y+8)/9;//同上,向上取整
17 x=N*36-36*f-25*e-16*d-9*c-4*b;
18 if(a>x)//需求>提供
19 N+=(a-x+35)/36;//同上,向上取整
20 printf("%dn",N);
21 }
22 return 0;
23 }
- 每周学点大数据 | No.25二叉搜索树回顾(二)
- RBF神经网络及Python实现(附源码)
- 【干货】计算机视觉实战系列03——用Python做图像处理
- Adaboost从原理到实现(Python)
- Selenium+python自动化22-发送各种类型附件邮件
- Selenium2+python自动化38-显示等待(WebDriverWait)
- 逆元(个人模版)
- Selenium2+python自动化37-爬页面源码(page_source)
- ex_gcd(个人模版)
- Selenium2+python自动化36-判断元素存在
- Java A+B(个人模版)
- TensorFlow实战:SoftMax手写体MNIST识别(Python完整源码)
- set排序(个人模版)
- TSP(个人模版)
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- 05 . k8s实战之部署PHP/JAVA网站
- 拖不得了,Android11真的来了,最全适配实践指南奉上
- 03 . Python入门之运算符
- 04 . Python入门之条件语句
- vue-element的select下拉框赋值
- 女儿拿着小天才电话手表问我App启动流程
- 数据库三问—快手真题
- 新人手册系列:思考篇-稳定性&大促保障
- RecycleView三问—腾讯真题
- css文字底部阴影效果
- 04 . kubernetes资源清单YAML入门
- 05 . Python入门值循环语句
- Constraintlayout约束布局三问
- 微信小程序底部导航栏实现
- 06 . Python3入门之IO编程(文件操作)