1017 products packed

来源:互联网 发布:java中的锁 编辑:程序博客网 时间:2024/06/11 08:09

1017 Problem R

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. 

题意:装箱问题,有六种不同大小的箱子,将它们装入另一箱中,需要使用最少的箱子。

思路:装箱过程中,应采取由大到小的原则,大的占一个箱子,小的充分利用剩余空间。

感想:局部最优解,较大的箱子只能占一个大箱,而小的则可使用其他剩余空间,只有在较大箱全部装入后,小箱在放会最大程度上节省空间。

#include<iostream>

#include<algorithm>

using namespace std;

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

int main(){

   int a[7];

   int sum,i;

   while(1){

           sum=0;

           int p=0;

           for(i=1;i<=6;i++){  cin>>a[i];  p+=a[i]; }

           if(p==0)  break;

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

           int a2=a[4]*5+leave[(a[3]+3)%4];

           if(a[2]>a2)

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

           int a1=sum*36-a[6]*36-a[5]*25-a[4]*16-a[3]*9-a[2]*4;

           if(a[1]>a1)

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

           cout<<sum<<endl;

}

}

0 0
原创粉丝点击