这周的问题终于有些难度了,终于不是一眼看过去就有能写出大概的题目了,还有就是,我又找不到题目了,所以去网上扒了一下,英语的:

Packets
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 28329   Accepted: 9291

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 = 36 的空间,然后看货物,细细一想,发现 边长大于3的货物都需要单独开一个箱子,所以便有了第一行代码
sum += goods4 + goods5 + goods6;

  又反复的思考了一下3的情况 一个 6*6的箱子能够装4个3*3的箱子。所以上面的算法就可以修改成

 sum += goods4 + goods5 + goods6 + ((goods3 - 1) / 4 + 1);//向上取整,多出来的一个必须多开一个箱子

  为什么多出来的3*3货物必须多开一个箱子呢,分析一下空间就可以了,题目中说物品高度相同,所以只需要考虑占地面积,5*5 和 4*4 的货物所占的盒子虽然还有空间,但是它们余下的空间只能够存放1*1或者2*2的货物,所以多出来的3*3必须额外开辟一个空间。到了这一步问题就来了,3*3的货物是4的倍数那最好,如果3*3的货物不是4的倍数,那么就必须额外开一个箱子,用来存放余下的3*3的货物,那么存放多余的的组成不成一件3*3的货物肯定会有多余空间,而且余数不止一种,我们就需要进行分类讨论。嗯好,我们来继续分析,现在3*3的货物是被全部放下了,但是由于3*3的货物不是 4 的倍数, 难么肯定就会有多余的空间,而且根据余数的不同肯定有不同的情况。那么余数有几种可能呢 ?唔,让我们来想一想,一个 箱子可以放4个3*3,那么x%4的可能只有4种吧,0、1、2、3 。

  现在我们来分析一下 3*3 的情况,无剩余的情况就不分析了

    画个图就可以直观的看到结果了:

      由图可知:

    1、当余下1个的时候还能装7个1*1和5个2*2,

    2、当余下2个的时候还能装6个1*1和3个2*2,

    2、当余下3个的时候还能装5个1*1和1个2*2,

  得出相应的代码:

if (goods3 % 4 == 1){spaceby1 += 7;spaceby2 += 5;} //余下1个3*3所余下的1*1和2*2的空间if (goods3 % 4 == 2){spaceby1 += 6;spaceby2 += 3;} //余下2个3*3所余下的1*1和2*2的空间if (goods3 % 4 == 3){spaceby1 += 5;spaceby2 += 1;} //余下3个3*3所余下的1*1和2*2的空间

处理完3*3的就简单了,我们按顺序去处理2*2的货物. 在这之前们需要求出有多少地方可以存储2*2的货物,5*5是无法存储2*2 的货物的,但是4*4的可以4*4的里面还可以存放5个2*2 所以我们需要加上goods2

4*5。好了2*2的空间算完,那还得分类论。

  很显然分为3总情况:

    1、刚好存下,皆大欢喜

    2、存不下,还得开空间

    3、有剩余,计算剩余空间

  让2*2的空间数与货物数进行比较够的话,就不用额外开空间了不够的话直接开空间

  代码如下:

  

        spaceby2 += 5 * goods4; //装一个4*4的时候余下的1*1和2*2的空间if (spaceby2 < goods2) //2*2的空间小于2*2的盒子
        {//额外的2*2空间需要的箱子sum += ((goods2 - spaceby2 - 1) / 9 + 1);}

  余下1*1 的模仿2*2的流程即可,1*1剩余空间建议用总箱子数*36 - 被占用的空间这样就可以快速的得出1*1的空间而且不容易错,(2*2不用这个方法是因为5*5的碎片空间虽然足够但是呢,它边长就……6*6,放不下2*2)1*1的就不单独贴代码了

  下面贴一下完整的代码(能自己写出来的先自己写一遍),记得分析数据大小:

  

#include <stdio.h>
/*** @author 杨文蓁的小迷弟* @note   算法作业3* @date   2019/09/05*/
int main()
{//北大OJ数据超过了正常的int,估计有一个50000的数据unsigned int goods1, goods2, goods3, goods4, goods5, goods6; //1,2,3,4,5,6while (scanf("%d %d %d %d %d %d", &goods1, &goods2, &goods3, &goods4, &goods5, &goods6) != EOF){unsigned int sum = 0, spaceby1 = 0, spaceby2 = 0; //sum总箱子数,n余下放1x1的位置,m余下放2x2的位置if (goods1 == 0 && goods2 == 0 && goods3 == 0 && goods4 == 0 && goods5 == 0 && goods6 == 0){return 0;}sum += goods4 + goods5 + goods6 + ((goods3 +3) / 4); //最少需要开的箱子if (goods3 % 4 == 1){spaceby1 += 7;spaceby2 += 5;} //余下1个3*3所余下的1*1和2*2的空间if (goods3 % 4 == 2){spaceby1 += 6;spaceby2 += 3;} //余下2个3*3所余下的1*1和2*2的空间if (goods3 % 4 == 3){spaceby1 += 5;spaceby2 += 1;}           //余下3个3*3所余下的1*1和2*2的空间spaceby2 += 5 * goods4; //装一个4*4的时候余下的1*1和2*2的空间if (spaceby2 <= goods2) //2*2的空间小于2*2的盒子
        {//额外的2*2空间需要的箱子sum += ((goods2 - spaceby2 + 8) / 9);}//余下了多少 1*1 的空间spaceby1 = 36 * sum - 36*goods6 - 25*goods5 - 16*goods4 - 9*goods3 - 4*goods2;if (spaceby1 <= goods1){sum += ((goods1 - spaceby1 + 35) / 36);}printf("%d\n", sum);}return 0;
}

C

贴个python的代码:

#author: 杨文蓁的小迷弟
#date:   2019/9/5while (1):s=input().split()box_sum = spaceby1 = spaceby2 = 0flag = Truefor i in range(len(s)):if int(s[i]) != 0:flag = Falseif flag == True:exit()box_sum = int(s[5]) + int(s[4]) + int(s[3]) + (int(s[2])-1) // 4+1if int(s[2])%4 == 1:spaceby1 += 5spaceby2 += 7if int(s[2])%4 == 2:spaceby1 += 6spaceby2 += 3if int(s[2])%4 == 3:spaceby1 += 5spaceby2 += 1spaceby2 += 5 * int(s[3])if spaceby2 <= int(s[1]):box_sum += ((int(s[1])-spaceby2-1)//9 + 1)spaceby1 = 36*box_sum - 36*int(s[5]) - 25*int(s[4]) - 16*int(s[3]) - 9*int(s[2]) - 4*int(s[1]);if spaceby1 <= int(s[0]):box_sum += ((int(s[0])-spaceby1-1)//36 + 1)print(box_sum);

Python

转载于:https://www.cnblogs.com/daker-code/p/11456717.html

算法作业 (三)——— 装箱问题相关推荐

  1. 深蓝学院《从零开始手写VIO》作业三

    深蓝学院<从零开始手写VIO>作业三 深蓝学院<从零开始手写VIO>作业三 1. 代码修改 2. 公式推导 3. 公式证明: 深蓝学院<从零开始手写VIO>作业三 ...

  2. 代数与逻辑:作业三 贝叶斯决策

    代数与逻辑:作业三 贝叶斯决策 文章目录 代数与逻辑:作业三 贝叶斯决策 一.作业要求 二.基于高斯分布的贝叶斯分类器与朴素贝叶斯分类器的原理介绍 1.贝叶斯定理 2.朴素贝叶斯分类 3.高斯朴素贝叶 ...

  3. CUMTOJ算法作业二

    CUMTOJ算法作业二 问题 A: 单词排序 题目描述 小红学会了很多英文单词,妈妈为了帮小红加强记忆,拿出纸.笔,把 N 个单词写在纸上的一行里,小红看了几秒钟后,将这张纸扣在桌子上.妈妈问小红:& ...

  4. 機器學習基石(Machine Learning Foundations) 机器学习基石 作业三 课后习题解答

    今天和大家分享coursera-NTU-機器學習基石(Machine Learning Foundations)-作业三的习题解答.笔者在做这些题目时遇到很多困难,当我在网上寻找答案时却找不到,而林老 ...

  5. K-means聚类算法的三种改进(K-means++,ISODATA,Kernel K-means)介绍与对比

    原文:http://www.cnblogs.com/yixuan-xu/p/6272208.html K-means聚类算法的三种改进(K-means++,ISODATA,Kernel K-means ...

  6. 20165301 预备作业三:Linux安装及命令入门

    预备作业三:Linux安装及命令入门 VirtualBox虚拟机的安装 在进行安装之前,原本以为有了娄老师的安装教程会是一件很容易的事情.万万没想到,在自己实际动手操作中,还是遇到了许多困难.通过与同 ...

  7. leetcode旋转数组 c语言,leetcode explore 初级算法第三题,旋转数组代码实现

    leetcode explore 初级算法第三题,旋转数组代码实现.原题链接: 题目分析 因为题目不是很长,这里把题目贴出来: 给定一个数组,将数组中的元素向右移动 k 个位置,其中 k 是非负数. ...

  8. 理解GBDT算法(三)——基于梯度的版本

     理解GBDT算法(三)--基于梯度的版本 标签: GBDT梯度残差代价函数回归树 2015-03-31 16:13 1395人阅读 评论(3) 收藏 举报 本文章已收录于: 分类: Machin ...

  9. 淮海工学院linux实验报告三,作业三 实验报告

    作业三 实验报告 网络通信 文件传输 实验一 1-1 实验题目 服务器端和客户端各传递1次字符串.考虑到使用TCP协议,所以传递字符串前先以4字节整数型方式传递字符串长度.连接时服务器端和客户端数据传 ...

最新文章

  1. -16 | 12 等于多少
  2. VMWARE错误-“VirtualInfrastructure.Utils.ClientsXml“的类型初始值设定项引发异常
  3. CLion:JetBrains 正式推出的 C/C++ IDE
  4. 下位机和上位机是什么意思_焊锡机是什么?焊锡机有几种分类?
  5. Tiny Wings 为什么能迅速成为 iOS App Store 付费应用第一名?
  6. work2的code和问题
  7. Git生成patch及打patch到源代码
  8. 移动端海洋实时仿真技术研究与实现
  9. web文件上传(三)--webapi后台接收参数和文件
  10. BE THE PIONEER FROM APSARADB——2018云栖大会·深圳峰会·云数据库在线直播分论坛
  11. CSRF - 跨站请求伪造
  12. 云专网和云专线的区别_云专线网络接入解决方案
  13. 一首《你莫走》把已经远去的疫情呼唤回来,新一年的路要怎么走?
  14. ArcGIS最详细的地图制作教程
  15. Python戏说NBA:谁是季后赛最强得分手
  16. 如何查看一个vs工程使用的vs版本是哪一个?
  17. linux之ps aux、ps -aux、ps -ef命令的区别
  18. 畅邮(DM Pro)-一款强悍、纯净而稳定的重量级电子邮箱客户端(支持分发、追踪)...
  19. php授权微信自动扣款,【微信支付】微信代扣开发者文档
  20. 机器人工程专业学习金字塔

热门文章

  1. 【超详细】多元线性回归模型statsmodels_ols
  2. 手动搭建 React 项目
  3. Python适合0基础菜鸟学吗
  4. matlab求矩阵特征值和特征向量、行列式
  5. 微服务治理之分布式链路追踪--3.zipkin实战
  6. 数据通信之信道与编码
  7. jQueryXML笔记
  8. [转]软件开发基本原则(二)典型错误
  9. js 模拟from提交post
  10. 每日算法面试题,大厂特训二十八天——第十天(位运算)