挑战程序设计竞赛 第2章习题 poj 1017 Packets 贪心模拟
地址 http://poj.org/problem?id=1017
题目大意是 有个6*6的面积的集装箱,装上1*1 2*2 3*3 4*4 5*5 6*5的箱子,询问最少需要集装箱的数量 多行输入 每行 6个数字以空格隔开 表示1~6面积的箱子的数目 6个数字全0 表示输入结束 按照输入的行数 输出多行答案 每行为最少需要集装箱的数量
解答 贪心解法 先填入最大的箱子,剩余的空间填入小箱子
可知 6*6可填充一个集装箱 无缝隙可填充其余箱子
5*5可填充一个集装箱 剩余缝隙可以填充6*6-5*5 =11个1*1的箱子
4*4可填充一个集装箱 剩余缝隙可填充5个2*2的箱子 其余可填充1*1的箱子
3*3比较麻烦 4个3*3可以装满一个集装箱
1~3个3*3的箱子填充如下 配图 填充1到3个3*3的箱子后 ,可以填充2*2的箱子的个数是5 ,3,1
按照这种情况模拟的代码
// 1123555.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。 // #include <iostream> #include <algorithm> using namespace std; /* Sample Input 99 0 0 0 0 0 0 99 0 0 0 0 0 0 99 0 0 0 0 0 0 99 0 0 0 0 0 0 99 0 0 0 0 0 0 99 99 0 99 0 99 0 99 99 2 0 99 0 0 2 0 99 0 0 0 3 7 99 0 0 9 3 7 99 0 0 9 9 9 99 0 0 9 9 9 0 99 0 99 99 9 0 99 0 99 99 9 0 0 0 0 0 0 0 0 0 Sample Output 2 1 */ int arr[6]; int ans; void fill11(int left) { int fillcount = min(arr[0], left); arr[0] -= fillcount; left -= fillcount; } void solve() { ans = 0; ans += arr[5]; //6*6 if (arr[4] != 0) { int count = arr[4];arr[4] = 0; for (int i = 0; i < count; i++) { ans++;fill11(6 * 6 - 5 * 5); } } if (arr[3] != 0) { int count = arr[3];arr[3] = 0; for (int i = 0; i < count; i++) { ans++; int fill22 = min(arr[1], 5); arr[1] -= fill22; fill11(6 * 6 - 4 * 4 - fill22*2*2); } } if (arr[2] != 0) { int count = arr[2]; arr[2] = 0; ans += count / 4; count = count % 4; if (count != 0) { ans++; int fill22 = min(arr[1], (3- count)*2+1); arr[1] -= fill22; fill11(6 * 6 - 3 * 3*count - fill22 * 2 * 2); } } if (arr[1] != 0) { int count = arr[1]; arr[1] = 0; ans += count / 9; count = count % 9; if (count != 0) { ans++; fill11(6 * 6 - count * 4); } } if (arr[0] != 0) { ans += arr[0] / 36; arr[0] = arr[0] % 36; if(arr[0] != 0){ ans++; fill11(6 * 6); } } } int main() { while (1) { ans = 0; int isAllZero = 1; for (int i = 0; i <= 5; i++) { cin >> arr[i]; if (arr[i] != 0) isAllZero = 0; } if (1 == isAllZero) break; solve(); cout << ans << endl; } return 0; }
作 者: itdef
欢迎转帖 请保持文本完整并注明出处
技术博客 http://www.cnblogs.com/itdef/
B站算法视频题解
https://space.bilibili.com/18508846
qq 151435887
gitee https://gitee.com/def/
欢迎c c++ 算法爱好者 windows驱动爱好者 服务器程序员沟通交流
如果觉得不错,欢迎点赞,你的鼓励就是我的动力
欢迎转帖 请保持文本完整并注明出处
技术博客 http://www.cnblogs.com/itdef/
B站算法视频题解
https://space.bilibili.com/18508846
qq 151435887
gitee https://gitee.com/def/
欢迎c c++ 算法爱好者 windows驱动爱好者 服务器程序员沟通交流
如果觉得不错,欢迎点赞,你的鼓励就是我的动力


【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
· 25岁的心里话
2018-03-25 leveldb 源码编译 vs版本
2015-03-25 写点C++ 学习记录 充数