packets__ algorithm of greedy algorithm

Source: Internet
Author: User

Packets

Description

A factory producesproducts 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 are always delivered to customers inthe square parcels of the same height H as the products have and the Size6*6. Because of the expenses it is the interest of the factory as as a ofthe customer to minimize the number of parcels nece Ssary to deliver the orderedproducts from the factory to the customer. A Good program solving the problemof finding the minimal number of parcels necessary to deliver the givenproducts Accordin G to an order would save a lot. You are are asked to makesuch a.

Input

The input fileconsists of several lines specifying orders. Each line specifies the one order. Orders are described by six integers separated by one space representingsuccessively the number of packets of individual s Ize from the smallest size1*1 to the biggest size 6*6. The end of the input file is indicated by the linecontaining six zeros.

Output

The output filecontains one line for each line in the input file. This line contains theminimal number of parcels to which the order from the corresponding line ofthe input file can be P Acked. There is no. in the output file correspondingto 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

Interpretation of the title: there are 6 of different areas of the box, now input the number of six types of boxes, to ask a total of how many 6*6 boxes to fully install them.

The idea of solving problems: a typical greedy algorithm.

Boxes with an area of 6*6,5*5,4*4 must be packed in a separate box.

A box with a size of 3*3 up to four in the same box

Boxes with an area of 2*2 can be assembled in the 4*4 and 3*3 remaining boxes.

The remaining area is 1*1.

Apparently, for 4*4 's box, 5 2*2 boxes can be loaded.

For 3*3 box, if only one, can be installed 5 2*2, if there are two, you can install 3, if there are three, you can install 1, if there are four, you can only install 0.

Based on the above thought, the resulting code is as follows:

#include <iostream>

using namespace Std;

int main ()

{

Intvalue[7];//value[i] Indicates the number of i*i boxes

intkey[4]={0,5,3,1};

while (1)

{

intans=0;

for (inti=1;i<7;i++)

{

cin>>value[i];

Ans+=value[i];

}

if (ans==0)

{

Break

}

intsum=0;

sum=value[6]+value[5]+value[4]+ (value[3]+3)/4;

INTA2=VALUE[4]*5+KEY[VALUE[3]%4];

if (VALUE[2]>A2)

sum+= (value[2]-a2+8)/9;

inta1=sum*36-value[6]*36-value[5]*25-value[4]*16-value[3]*9-value[2]*4;//is more convenient to calculate the residual area

if (VALUE[1]>A1)

sum+= (value[1]-a1+35)/36;

cout<<sum<<endl;

}

Return0;

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.