uva 311 Packets

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 tex2html_wrap_inline37 . 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 tex2html_wrap_inline27 to the biggest size tex2html_wrap_inline37 . 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
大意:
给你六种物品的数量,尺寸分别是1*1,2*2,……6*6。你现在只有一个6*6的盒子,问你用最少的盒子能用多少去装下这些物品。

#include<iostream>
#include<algorithm>
#include<map>
#include<string>
#include<cstring>
#include<sstream>
#include<cstdio>
#include<vector>
#include<cmath>
#include<stack>
#include<queue>
#include<iomanip>
#include<set>
#include<fstream>
#include <climits>
using namespace std;
//fstream input,output;

int main()
{
    ios::sync_with_stdio(false);
    int ans,a,b,c,d,e,f,r,tmp;
    while(cin>>a>>b>>c>>d>>e>>f)
    {
        if(a+b+c+d+e+f==0)
            return 0;
        r=ans=0;
        ans+=f;//6*6
        ans+=e;//5*5
        if(a>=11*e)//
            a=a-11*e;
        else
            a=0;
        ans+=d;//4*4
        if(b>=5*d)
            b-=5*d;
        else
        {
            r=(5*d-b)*4;
            b=0;
            if(a>r)
                a=a-r;
            else
                a=0;
        }
// cout<<a<<" "<<b<<" "<<c<<" "<<d<<" "<<endl;
        r=0;
        if(c%4==0)
        ans+=c/4;
        else
        {
            ans+=c/4+1;
            r=36-(c%4)*9;
            if(r==27)
                tmp=5;
            if(r==18)
                tmp=3;
            if(r==9)
                tmp=1;

            if(b>=tmp)
            {
                b=b-tmp;
                r=r-tmp*4;
            }
            else
            {
                b=0;
                r=r-b*4;
            }
            if(a>r)
                a=a-r;
            else
                a=0;
        }
        if(b>0)
        {
            if((b*4)%36)
            {
                ans+=(b*4)/36+1;
                r=36-(b*4)%36;
                if(a>r)
                {
                    a=a-r;
                }
                else
                    a=0;
            }
            else
            ans+=(b*4)/36;
        }
        if(a>0)
        {
            if(a%36)
                ans+=a/36+1;
            else
                ans+=a/36;
        }
        cout<<ans<<endl;
    }
// input.close();
// output.close();
    return 0;
}




解答:
6*6的物品肯定是一样一个箱子。
一个6*6的箱子能装一个5*5的物品,还能剩下11个1*1的空隙。
一个6*6的箱子能装下一个4*4的物品,还能剩下5个2*2的空隙。
一个6*6的箱子能装下4个3*3的物品,注意。如果一个6*6的箱子只装了1个3*3的物品,剩下的空间能装下5个2*2的物品和若干1*1的物品,如果一个6*6的箱子装下2个3*3的物品,那么剩下的空间能装3个2*2的物品和若干1*1,最后如果一个6*6的箱子装了3个3*3的物品,那么还能装下1个2*2的物品和若干1*1的。
之前装过的1*1和2*2在的减去后,在找几个6*6的把剩下的2*2和1*1的装下即可。

你可能感兴趣的:(uva,贪心)