POJ 1017-Packets

这本来是一个简单的贪心题,但是我写的有点罗嗦了,这题只要把握住三点就行了:

1. a/b是向下去整,我们要事先将a 变成 a+b-1,这样除法才不会错误。

2. 把握6 * 6的箱子能放几个 各种类型的物品。

3. 从大的放起,剩余空间要利用好,也就是要贪心。

#include<cstdio>
#include<cstring>
#include<cstdlib>

int a[7] = { 0};
int res[5] = { 0};
int ans;

int main()
{
while( scanf( "%d", &a[1]) == 1)
{
for( int i = 2; i < 7; i ++)
scanf( "%d", &a[i]);
if( a[1] + a[2] + a[3] + a[4] + a[5] + a[6] == 0)
break;
memset( res, 0, sizeof res);
ans = a[6];
if( a[5] != 0) {
ans += a[5];
res[1] += 11 * a[5];
}
if( a[4] != 0)
{
ans += a[4];
res[2] += 5 * a[4];
}
if( a[3] != 0)
{
ans += ( a[3] + 3 ) / 4;
res[3] += 4 - a[3] % 4;
if( res[3] == 1)
{
res[2] ++;
res[1] += 5;
}
else if( res[3] == 2)
{
res[2] += 3;
res[1] += 6;
}
else if( res[3] == 3)
{
res[2] += 5;
res[1] += 7;
}
}
if( a[2] != 0)
{
if( a[2] >= res[2])
{
a[2] -= res[2];
res[2] = 0;
}
else
{
res[2] -= a[2];
res[1] += res[2] * 4;
a[2] = 0;
}
if( a[2] > 0)
{
ans += (a[2] + 8) * 4 / 36;
res[1] += (9 - a[2] % 9) * 4;
}
}
if( a[1] != 0)
{
if( a[1] >= res[1])
a[1] -= res[1];
else
a[1] = 0;
}
ans += (a[1] + 35) / 36;
printf( "%d\n", ans);
}
return 0;

}

 

你可能感兴趣的:(poj)