贪心之Packets

时间: 1ms        内存:128M

描述:

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.

输入:

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.

输出:

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.

示例输入:

0 0 4 0 0 1
7 5 1 0 0 0
0 0 0 0 0 0

示例输出:

2
1

提示:

参考答案(内存最优[1092]):

#include <stdio.h>
int main()
{
    int N, a, b, c, d, e, f, y, x;
    int u[4]= {0, 5, 3, 1};
    while(1)
    {
        scanf("%d%d%d%d%d%d", &a, &b, &c, &d, &e, &f);
        if (a == 0 && b == 0 && c == 0 && d == 0 && e == 0 && f == 0) 
            break;
        N = f + e + d + (c + 3) / 4;
        y = 5 * d + u[c % 4];
        if(b > y) N += (b - y + 8 ) / 9;
        x = 36 * N - 36 * f - 25 * e - 16 * d - 9 * c - 4 * b;
        if(a > x) N += ( a - x + 35 ) / 36;
        printf("%d\n", N);
    }
    return 0;
}

参考答案(时间最优[0]):

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int main()
{
    int a,b,c,d,e,f;
    while(scanf("%d%d%d%d%d%d",&a,&b,&c,&d,&e,&f)!=EOF)
    {
        if(a==0&&b==0&&c==0&&d==0&&e==0&&f==0)
            break;
        int ans=f+e+d+c/4;
        if(e>0)
            a-=e*11;
        if(d>0)
        {
            int x=20*d;
            if(4*b<=x)
            {
                b=0;
                a-=x-4*b;
            }
            else
                b-=x/4;
        }
        c%=4;
        if(c>0)
        {
            ans++;
            if(c==1)
            {
                if(b>=5)
                    b-=5,a-=7;
                else
                    b=0,a-=27-b*4;
            }
            else if(c==2)
            {
                if(b>=3)
                    b-=3,a-=6;
                else
                    b=0,a-=18-b*4;
            }
            else
            {
                if(b>0)
                    b-=1,a-=5;
                else
                    a-=9;
            }
        }
        if(b>0)
        {
            ans+=b/9;
            b=b%9;
            if(b>0)
            {
                ans++;
                a-=36-b*4;
            }
        }
        if(a>0)
        {
            ans+=a/36;
            a=a%36;
            if(a>0)
                ans++;
        }
        printf("%d\n",ans);
    }
    return 0;
}

题目和答案均来自于互联网,仅供参考,如有问题请联系管理员修改或删除。