1. 程式人生 > >POJ - 1017 貪心模擬

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;
}