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 66. 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 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
反例
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include<string.h>
typedef long long int ll;
int main(){
ll n,m,x,y,i,j,k;
ll ar[4]={0,5,3,1};//一个箱子放a3的数量决定了可以放22的个数
ll a1,a2,a3,a4,a5,a6;
ll y1,y2;
ll rem2,rem1;
while(1){//m为第一次箱子数, y代表22,x代表11
scanf("%lld%lld%lld%lld%lld%lld",&a1,&a2,&a3,&a4,&a5,&a6);
i=0;j=0;k=0;
if(a1+a2+a3+a4+a5+a6==0)break;
i=a6+a5+a4+(a3+3)/4;
// i=a6+a5+a4+a3/4;//先放a4,a5,a6,a3;
// if(a3%4!=0)i++;//如果a3不为4的倍数,那么加一个有剩余空间箱子
y2=a4*5+ar[a3%4];//能盛放22的空间
y1=11*a5+a4*20+(36-(a3%4)*9);//能成放11的空间,即全部剩余空间
// printf("22=%lld\n11=%lld\n",y2,y1);
rem2=a2-y2;//剩余的22
if(a2<y2)
{
x=y1-a2*4;//22填完,则剩下的空间都给11
}
else
{
k=(rem2+8)/9;
// j=rem2/9;//如果剩余的空间不足以存放剩余的22,那么加箱子 j个
// if(rem2%9!=0)j++;//如果a2不为9的倍数,那么加一个有剩余空间箱子
x=y1-4*y2+(36-(rem2%9)*4);//剩余的11空间
}
rem1=a1-x;//剩余的11
if(a1<x)
{
printf("%lld\n",i+j);
}
else
{
k=(rem1+35)/36;
// k=rem1/36;//如果剩余的空间不足以存放剩余的11,那么加箱子 k个
// if(rem1%36!=0)k++;//如果a1不为36的倍数,那么加一个有剩余空间箱子
printf("%lld\n",i+j+k);
}
// printf("i=%lld\n",i);
// printf("j=%lld\n",j);
// printf("k=%lld\n",k);
}
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(a1+a2+a3+a4+a5+a6==0)break;
n=f+e+d+(c+3)/4;
y=5*d+u[c%4];
if(b>y)
{
n+=(b-y+8)/9;
}
x=36*n-4*b-9*c-16*d-25*e-36*f;
if(a>x)
{
n+=(a-x+35)/36;//a-x/36
} //if(a-x/36!=0)n++
printf("%d\n",n);
}
return 0;
}
总结
贪心算法,先填大的。
刚开用分类讨论,然后一个下午就这样过去了
后来发现对每个小箱子进行总体讨论要方便的多。
学到了n+=(a-x+35)/36可以直接代替
a-x/36
if(a-x/36!=0)n++
对于类似问题比较方便