Time limit: 1000MS Memory Limit: 10000K Total submissions: 51306 accepted: 17391
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 is always delivered to customers in the square parcels of the same height h as the products has and of The size 6*6. Because of the expenses it is the interest of the factory as well as a customer to minimize the number of parcels NEC Essary 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 accord ing to an order would save a lot of money. you is asked to make such a program.
Input
The input file consists of several lines specifying orders. Each line specifies one order. Orders is 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 to which the order from the corresponding line of the input file can Packed. There is no line with the output file corresponding to the last "null" line of the input file.
Sample Input
Sample Output
First, the main topic
The company has a total floor area of 1*1, 2*2, 3*3, 4*4, 5*5, 6*6, the height of the same as H of six products, now need to pack with the fewest boxes, the bottom area of the box is 6*6, height is H.
Second, the idea of solving problems
Simple brute-force greedy algorithm, different products have different strategies, according to the order from large to small packaging products, the strategy is as follows:
6*6: 1 products in 1 boxes
5*5: 1 products to occupy 1 boxes, with 1*1 boxes can be filled (11 filled 1 cases)
4*4: 1 products to occupy 1 boxes, the remaining space with 2*2 and 1*1 box filling (first fill 2*2, then fill 1*1)
3*3: 4 products can be filled with 1 boxes, if there are less than 1 cases, divided by 1*1 and 2*2 products filled
2*2: 9 products can fill 1 boxes, if there are less than 1 cases, filled with 1*1 products
1*1: 36 products can fill a box
third, the specific code
1#include <cstdio>2 3 intMax_ (intAintb) {4 if(A>B)returnA;5 Else returnb;6 }7 8 intMain () {9 intS1, S2, S3, S4, S5, S6;Ten while(SCANF ("%d%d%d%d%d%d", &s1, &s2, &s3, &s4, &S5, &S6) && s1+s2+s3+s4+s5+S6) { One intPackets =0; APackets + = S6;//6*6 's Products one box - -Packets + = S5;//5*5 's Products one box theS1 = Max_ (0, s1- One*S5);//spare space with 1*1 products as far as possible to fill - -Packets + = S4;//4*4 's Products one box - if(s2<5*S4) S1 = Max_ (0, S1-(5*S4-S2));//If there is still space after 2*2 's product is filled, fill it with 1*1 +S2 = Max_ (0, s2-5*S4);//try to fill up with 2*2 products - +Packets + = (s3+3)/4;//3*3 's products four a box AS3%=4;//If the 3*3 box is not four times the size of the number, then the first filled with 2*2 and then 1*1 filled with at if(s3==1){ - if(s2<5) S1 = Max_ (0, S1-( --4*s2)); - ElseS1 = Max_ (0, s1-7); -S2 = Max_ (0, s2-5); - } - Else if(s3==2){ in if(s2<3) S1 = Max_ (0, S1-( --4*s2)); - ElseS1 = Max_ (0, s1-6); toS2 = Max_ (0, s2-3); + } - Else if(s3==3){ the if(s2<1) S1 = Max_ (0, S1-(9-4*s2)); * ElseS1 = Max_ (0, s1-5); $S2 = Max_ (0, s2-1); Panax Notoginseng } - thePackets + = (s2+8)/9;//2*2 's products nine a box +S2%=9;//If the 2*2 box is not nine times the number, then fill with 1*1 A if(s2) S1 = Max_ (0, S1-( $-4*S2)); the +Packets + = (s1+ *)/ $;//1*1 's products 36 a box - $printf"%d\n", packets); $ } - - return 0; the}View Code
POJ1017 Packets (greedy algorithm training)