Packets
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 Source |
拿到这题,开始把题目的意思理解错了,以为是用题目给出的六种方块,组成最多的6*6的格子,尝试性的按照这个思路去写了几次,结果全WA,而且这个题目给出的样例并没有什么实质性的用处。后来去discuss里面看了看才知道这个题目的意思,即:有六组数据a,b,c,d,e,f,分别表示1*1,2*2,3*3,4*4,5*5,6*6盒子的个数,现在要求出最少需要几个6*6的盒子才能把上面所有的盒子打包起来。好吧,既然这样就简单多了。
6*6独占一个格子
5*5需要额外添加11个1*1的格子
4*4需要7个2*2的格子(这里用1*1或者2*2效果一样)
3*3先4个4个堆好,剩下的用1*1和2*2的格子补全
最后再考虑1*1和2*2的格子堆在一起。
值得注意的是3*3的格子很特殊,剩余1个,需要5个2*2格子,剩余2个,需要3个2*2格子,剩余3个需要1个2*2格子。
以下是我的代码,没别的特殊解法,常规思路。
#include <stdio.h>
int main()
{
int arr[7];
//freopen("in.txt","r",stdin);
while(scanf("%d%d%d%d%d%d",&arr[1],&arr[2],&arr[3],&arr[4],&arr[5],&arr[6]))
{
int flag=0,x=0,y=0; //x表示需要的1*1的格子,y表示需要的2*2的格子
for(int i=1; i<7; i++)
if(arr[i]==0) flag++;
if(flag==6) return 0;
int sum=0;
sum=arr[6]+arr[5]+arr[4]+(arr[3]+3)/4;
y+=5*arr[4];
int t=arr[3]%4;
if(t==1) y+=5;
if(t==2) y+=3;
if(t==3) y++;
if(y<arr[2]) sum=sum+((arr[2]-y)+8)/9;
x=36*sum-36*arr[6]-25*arr[5]-16*arr[4]-9*arr[3]-4*arr[2];
if(x<arr[1]) sum=sum+((arr[1]-x)+35)/36;
printf("%d\n",sum);
}
return 0;
}