POJ - 1017 贪心模拟

因为4*4,5*5,6*6的必须单独放,所以先开辟这三个的盒子,然后往里面添加1*1和2*2的;

对于5*5的只能填1*1的,一个已经塞了5*5的盒子可以填11个1*1的格子;

对于4*4的,先填2*2的,再填1*1的;

然后再为3*3的开辟新盒子,每四个可以放一个盒子,不足四个的再新开辟一个,然后往里面塞1*1和2*2的,同理也是先塞2*2的;

如果新开辟了一个盒子里面只有一个3*3的,可以塞5个2*2的,注意一下就行了;

然后2*2的开辟,塞1*1的;

最后再为1*1的开辟空间即可;

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
typedef long long ll;
const int inf=0x3f3f3f3f;
const double epos=1e-8;


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 res=f+e+d;//直接为4,5,6的开辟空间;
        
        int t=e*11;
        a=(a<=t?0:(a-t));

        //为4*4的先塞2*2,再塞1*1的;
        t=d*20;
        int h=min(t/4,b);
        b-=h;
        t-=h*4;
        h=min(t,a);
        a-=h;
        t-=h;

        //为3*3的开辟空间;
        res+=c/4;
        h=c%4;
        int hh=36-h*9;
        if(h){//不足四个的新开空间;
            ++res;
            if(h==3){
                t=min(b,1);
                hh-=t*4;
                b-=t;
                t=min(a,hh);
                a-=t;
            }
            else if(h==2){
                t=min(b,3);
                hh-=t*4;
                b-=t;
                t=min(a,hh);
                a-=t;
            }
            else if(h==1){
                t=min(b,5);
                hh-=t*4;
                b-=t;
                t=min(a,hh);
                a-=t;
            }
        }

        //为2*2的开辟空间,塞1*1的;
        t=b/9;
        res+=t;
        if(t*9!=b){
            ++res;
            h=36-(b-t*9)*4;
            t=min(a,h);
            a-=t;
        }

        //最后为1*1的开辟;
        t=a/36;
        res+=t;
        if(t*36!=a)
            ++res;
        printf("%d\n",res);


    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/chenyume/article/details/84111238