Packets
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 42945 | Accepted: 14466 |
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的盒子,然后你要装的物品分别为1*1,2*2...6*6 ,问你装这些物品最少需要多少盒子。
思路:建立两个方法,一个是处理剩余位置能装1*1的盒子的情况,一个是装2*2盒子的情况,在5*5的情况下可以装11个1*1的,4*4的情况可以装5个2*2的,3*3的情况比较多,见代码吧。
这样的题目我竟然能够一A,感觉自己萌萌哒。
AC代码如下:
#include<cstdio>
#include<cstring>
using namespace std;
int num[10],ans;
void cut1(int n)
{ num[1]-=n;
if(num[1]<0)
num[1]=0;
}
void cut2(int n)
{ if(num[2]>=n)
num[2]-=n;
else
{ cut1(4*(n-num[2]));
num[2]=0;
}
}
int main()
{ int i,j,k;
while(true)
{ k=0;ans=0;
for(i=1;i<=6;i++)
{ scanf("%d",&num[i]);
k+=num[i];
}
if(k==0)
break;
ans+=num[6]+num[5]+num[4];
cut1(11*num[5]);
cut2(5*num[4]);
ans+=num[3]/4+1;
num[3]%=4;
if(num[3]==0)
ans--;
else if(num[3]==1)
{ cut2(5);
cut1(7);
}
else if(num[3]==2)
{ cut2(3);
cut1(6);
}
else if(num[3]==3)
{ cut2(1);
cut1(5);
}
ans+=num[2]/9+1;
num[2]%=9;
if(num[2]==0)
ans--;
else
cut1(36-4*num[2]);
ans+=num[1]/36+1;
num[1]%=36;
if(num[1]==0)
ans--;
printf("%d\n",ans);
}
}