裝箱問題(貪心)


Packets
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 28329   Accepted: 9291

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

 
【題意】:

一個工廠制造的產品形狀都是長方體盒子,它們的高度都是 h,長和寬都相等,一共有六個型號,分別為1*1, 2*2, 3*3, 4*4, 5*5, 6*6。

這些產品通常使用一個 6*6*h 的長方體箱子包裝然后郵寄給客戶。因為郵費很貴,所以工廠要想方設法的減小每個訂單運送時的箱子數量BoxNum.

【思路】:我tm一直以為是先裝小的啊啊啊,對啊,能裝盡量裝,把盡量多的產品放進去不就避免多開箱子,放盡量多的產品不就先放小的的么????然后我就不會做了,網上先放大的當時我???,然后推了推,如果 是8個1*1,和一個5*5,如果按我先放小的,那樣就放不進5*5了,如果先放5*5,就可以放進去8個1*1了。。。。所以千萬不要理所當然。。。(當然如果你一開始跟我一樣想的話QAQ)

【代碼君】

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstdlib>
 4 using namespace std;
 5 int room2[4]={0,5,3,1};
 6 int main()
 7 {    
 8     int s1,s2,s3,s4,s5,s6,n1,n2;
 9     while(scanf("%d%d%d%d%d%d",&s1,&s2,&s3,&s4,&s5,&s6)&&s1||s2||s3||s4||s5||s6)//能夠輸入並且都不為0時 
10     {
11         int sum=0;//計算箱子總數0 
12         sum+=s6+s5+s4+(s3+3)/4;//6*6和5*5和4*4一定是一個占一個箱子,而3*3 4個占一個箱子 
13         n2=5*s4+room2[s3%4];//剩余的能夠放2*2的空間,有5*5,6*6的箱子已經不能放2*2了,放了4*4的箱子可以放5個2*2+放3*3的箱子對應的2*2的空間 
14         if(s2>n2)//2*2的個數比我們留出來為2*2的空間個數多,就需要為2*2另開箱子 
15         {
16             sum+=(s2-n2+8)/9;//求出多需要幾個2*2空間,+8是向上取整,再除以9,(因為每個箱子可以放9個2*2) 
17         }
18         n1=36*(sum-s6)-s2*2*2-s3*3*3-s4*4*4-s5*5*5;//用減法計算1*1剩余的空間 
19         if(s1>n1)//如果1*1的個數,比我們留出的空間多就需要另開空間 
20         sum+=(s1-n1+35)/36;//+35是向上取整,將多出來的另開箱子 
21         printf("%d\n",sum);
22     }
23     return 0;
24 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM