/*
translation:
有6*6的箱子,以及1*1,2*2,3*3,4*4,5*5,6*6的产品若干。要把这些产品全部装进箱子,求能用到的箱子的最小个数?
solution:
贪心加上模拟,具体做法见代码注释。
note:
# 开始想法是一个箱子一个箱子地装,每次从大到小拿出产品放进去。想法没错,但是怎样判断当前产品能否装进去就成了
问题。后来采取网上的做法,从大到小一次性把当前产品全部装完,而不是一个个箱子地装!同时每装完一种产品后,用
1*1,2*2的产品对空隙补充,同时更新ans。
* 这类“可能装不满的容器问题”可以采用此题的方法解决。
date:
2016.10.23
*/
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int p[10];
int main()
{
//freopen("in.txt", "r", stdin);
while(~scanf("%d%d%d%d%d%d", &p[1], &p[2], &p[3], &p[4], &p[5], &p[6]))
{
if(p[1]+p[2]+p[3]+p[4]+p[5]+p[6] == 0) break;
int ans = 0;
ans += p[6]; //6*6的产品刚好一个一箱
ans += p[5]; //5*5的产品一个一箱
p[1] = max(0, p[1] - p[5]*11); //用1*1填空隙
ans += p[4]; //4*4的也一个一箱
if(p[2] < 5*p[4])
{
p[1] = max(0, p[1] - (5*p[4] - p[2]));
p[2] = 0;
}
else p[2] = max(0, p[2] - 5*p[4]);
ans += p[3]/4; //3*3的4个刚好一箱
p[3] = p[3] % 4;
if(p[3]) ans++;
if(p[3] == 3) //余下有几个p[3],分别处理一下
{
if(p[2] >= 1) p[1] = max(0, p[1] - 5);
else p[1] = max(0, p[1] - 9);
p[2] = max(0, p[2] - 1);
}
else if(p[3] == 2)
{
int t = min(3, p[2]); //此时最多装3个p[2]
p[2] = max(0, p[2] - t);
p[1] = max(0, p[1] - (18 - t*4));
}
else if(p[3] == 1)
{
int t = min(5, p[2]); //此时最多装5个p[2]
p[2] = max(0, p[2] - t);
p[1] = max(0, p[1] - (27 - t*4));
}
if(p[2]) //如果p[2],p[1]还有剩余就直接填充
{
ans += p[2] / 9;
p[2] = p[2] % 9;
if(p[2]) ans++;
p[1] = max(0, p[1] - (36 - p[2]*4));
p[2] = 0;
}
if(p[1])
{
ans += p[1] / 36;
p[1] = p[1] % 36;
if(p[1]) ans++;
}
printf("%d\n", ans);
}
return 0;
}
poj1017(一般贪心,容器类)
最新推荐文章于 2023-01-19 18:13:02 发布