一、题目

Description

A factory produces products packed in square packets of the same height h and of the sizes 11, 22, 33, 44, 55, 66. 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 11 to the biggest size 66. 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...6 * 6的产品各a[i]个,包装袋大小固定为6 * 6,问最少最要多少个包装袋,可以把每个订单中所有产品包装起来。
  • 这个题目有点类似硬币问题,在选择时从最大的6 * 6的产品开始依次往小进行计算。对于6 * 6、5 * 5、4 * 4的产品,各需要包装袋a[6]、a[5]、a[4]个,其中放置5 * 5产品的包装袋可以额外装11个1 * 1的产品,放置4 * 4产品的可以额外装5个2 * 2的产品;对于3 * 3的产品比较特殊,对4取模后,根据余数0、1、2、3分四种情况进行判断,每次选择时尽可能得装入更多的2 * 2的产品再装入1 * 1的产品;对于2 * 2和1 * 1的产品判断较为简单,不再多说。
  • 在做这题时刚开始有点看不懂题意,便看了下discuss区,发现所有人都说这题非常非常难以及细节很多,导致不敢轻易下手。但是思路想清,WA了一两次之后,便AC了,好像也没想象中的那么难。
  • 在网上观摩到大神的极短代码,算法思想非常精辟,特另附上,以供学习。

三、代码

我的渣解法:

#include<cstdio>int a[7];int ans;void solve() {ans += (a[4] + a[5] + a[6]);a[1] -= a[5] * 11;a[2] -= a[4] * 5;if (a[2] < 0) {a[1] += a[2] * 4;}ans += (a[3] / 4 + 1);switch (a[3] % 4) {case 0: {ans --;break;}case 1: {if (a[2] > 0) {a[2] -= 5;if (a[2] < 0) {a[1] += a[2] * 4;}a[1] -= 7;} else {a[1] -= 27;}break;} case 2: {if (a[2] > 0) {a[2] -= 3;if (a[2] < 0) {a[1] += a[2] * 4;}a[1] -= 6;} else {a[1] -= 18;}break;}case 3: {if (a[2] > 0) {a[2] -= 1;a[1] -= 5;} else {a[1] -= 9;}break;}}if (a[2] > 0) {ans += a[2] / 9;if (a[2] % 9 > 0) {ans ++;a[1] -= (9 - a[2] % 9) * 4;         }}if (a[1] > 0) {ans += (a[1] + 35) /36;}printf("%d\n", ans);
}int main () {while (1) {ans = 0;for (int i = 1; i <= 6; i ++) {scanf("%d", &a[i]);}if (!a[1] && !a[2] && !a[3] && !a[4] && !a[5] && !a[6]) break;solve();}return 0;
}

大神的精辟解法:

#include<stdio.h>
int main()
{int n,a,b,c,d,e,f,x,y;int u[4]={0,5,3,1};while(1){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=d+e+f+(c+3)/4;y=5*d+u[c%4];//在已有n个的情况下,能装下y个2*2的if(b>y)n+=(b-y+8)/9;//把多的2*2的弄进来x=36*n-36*f-25*e-16*d-9*c-4*b;if(a>x)n+=(a-x+35)/36;//把1*1的弄进来printf("%d\n",n);}return 0;
}

转载于:https://www.cnblogs.com/CSLaker/p/7295047.html

【贪心算法】POJ-1017相关推荐

  1. 贪心算法-----poj 3253 Fence Repair(切木板)

    Description Farmer John wants to repair a small length of the fence around the pasture. He measures ...

  2. POJ 1017 Packets【贪心】

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

  3. 贪心算法—区间调度 电影节(POJ 4151)

    贪心算法--区间选取问题 或是区间调度问题 本文解决一个很经典的贪心算法问题 Interval Scheduling(区间调度问题).给你很多形如[start,end]的闭区间,请你设计一个算法,算出 ...

  4. 贪心算法—建立雷达(POJ 1328)

    贪心算法--区间选点问题 这也是贪心算法的经典问题,一般情况为:有n个闭区间[ai,bj],取尽量少的点,使得每个区间内都至少有一个点. 分析 如果区间i内已经有一个点被取到,则称此区间已经被满足. ...

  5. 信息学奥赛第十节 —— 贪心算法(渡河问题POJ 1700 Crossing River + 拦截导弹的系统数量求解)

    复习概念 贪心算法又叫贪婪算法,是指在对问题求解时,总是做出在当前看来是最好的选择.也就是说,贪心算法不从整体最优上加以考虑,它所做出的是在某种意义上的局部最优解. 无后效性:贪心算法不是对所有问题都 ...

  6. 贪心算法—圣诞老人的礼物(POJ 4110)

    贪心算法--物品可拆分情况求背包最大价值问题 描述 圣诞节来临了,在城市A中圣诞老人准备分发糖果,现在有多箱不同的糖果,每箱糖果有自己的价值和重量,每箱糖果都可以拆分成任意散装组合带走.圣诞老人的驯鹿 ...

  7. POJ 2709 Painter 【贪心算法】

    [原题链接] http://acm.pku.edu.cn/JudgeOnline/problem?id=2709 [题目大意] 要配置出n(3<=n<=12)种颜色的颜料,并配置出一定量的 ...

  8. 【贪心算法】poj 2431: Expedition(最优加油方法)

    题目描述(传送门) Description A group of cows grabbed a truck and ventured on an expedition deep into the ju ...

  9. POJ 3618 Best Cow Line(贪心算法)

    Best Cow Line Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 30454   Accepted: 8126 De ...

  10. POJ 3040 Allowance——经典贪心算法题目

    题目链接 题目大意: 约翰要给他的牛贝西发工资,每天不得低于C元,约翰有n种面值的钱币,第i种的面值为v_i,数量有b_i.问这些钱最多给贝西发多少天的工资.注意,每种面值的金钱都是下一种的面值的倍数 ...

最新文章

  1. Android 项目版本的修改
  2. UE中的几个极有用功能
  3. web项目性能优化--网络、js、渲染
  4. android 两个imageview重叠,在android中覆盖两个图像以设置imageview
  5. 记下来 关于InitCommonControls()
  6. AB1601spi传输慢的问题
  7. 关于css加div布局和表格布局,菜鸟学习笔记:表格布局和div+css布局
  8. QT的QScriptEngineDebugger类的使用
  9. scala使用reduce和fold方法遍历集合的所有元素
  10. VSCode从下载到配置Ubuntu系统
  11. idea 构建spring_以Spring方式构建企业Java应用程序
  12. 第二十一期:干货盘点!推荐程序员使用的5款工具软件
  13. ios 避免两个button同一时候被点击
  14. Recommended Browsers for Oracle E-Business Suite 11i/R12
  15. 矩阵乘法公式c语言,c语言矩阵相乘
  16. qlikview 地图插件制作教程
  17. K33 不是平面图_Edraw Max:一款简单好用的建筑平面图设计软件!
  18. 6款令人相见恨晚的在线搜索网站,成年后都会要用上,了解一下!
  19. 学会Python开发的第一步:写一个桌面小程序
  20. Navigation导航系统

热门文章

  1. int和Integer的比较
  2. java中servlet知识_jsp_Servlet常用知识总结
  3. 戴尔计算机windows未能启动,戴尔电脑windows7无法启动安装过程怎么办
  4. java接口测试工具_【分享】接口工具对比(apipost、jmeter、postman、swagger等)
  5. HTML+CSS+JS实现 ❤️爱心文字3D旋转动画特效❤️
  6. Mysql 零距离-入门(五)操作数据表
  7. Java 修改文件最后的创建日期
  8. C++ 查看输入流中的下一个字符
  9. oracle 触发器 行级,oracle的行级触发器使用
  10. Python实例 63,64