Cod sursa(job #1104703)

Utilizator alexalghisiAlghisi Alessandro Paolo alexalghisi Data 10 februarie 2014 22:40:54
Problema Zebughil Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.12 kb
#include <iostream>
#include <fstream>
#include <cstring>

#define DN (1<<17) + 13
using namespace std;

int g[20];
long long max_capacity[DN];
int dp[DN];


/// max_capacity [ st ] = cat e ocupat din ultimul camion daca am transportat deja toti bitii din stare
/// dp[ st ] = nr minim de camioane


int main()
{
    int t = 3;
    ifstream f("zebughil.in");
    ofstream gg("zebughil.out");

    for(;t--;){

        memset(dp,127,sizeof(dp));
        memset(max_capacity,0,sizeof(max_capacity));
        int n,G,rez=22;
        f>>n>>G;

        for(int i=0;i<n;++i)
            f>>g[i];

        dp[0]=1;

        for(int st = 0; st < (1<<n) ; ++st ){ /// am rezolvat toate pietrele din stare


            for(int i=0;i<n;++i){ /// vin cu o piatra noua

                if( ((st<<i)&1) == 0 ){ /// nu am transportat piatra i

                        int next_st = (  st|( 1<<i ) );

                        if( dp[st] + ( G < max_capacity[ st ] + g[ i ] ) < dp[next_st] ){


                            if( G < max_capacity[ st ] + g[ i ] ){

                                max_capacity[ next_st ] = g[i];
                                dp[ next_st ] = dp[ st ] + 1;
                            }
                            else{
                                max_capacity[ next_st ] = max_capacity[ st ] + g[i];
                                dp[ next_st ] = dp[st];
                            }
                        }else
                            if(  dp[st] + ( G < max_capacity[ st ] + g[ i ] ) == dp[next_st] ){ /// test

                                long long p ;

                                if( G < max_capacity[ st ] + g[ i ] )
                                    p = g[i];
                                    else
                                        p = max_capacity[ st ] + g[i];
                                max_capacity[ next_st ] = min(max_capacity[ next_st ] , p);
                            }

                    //cout<<"Stare :" <<next_st<<" -> "<< max_capacity[next_st]<<" nr de camioane:"<<dp[next_st]<<"\n";

                }
            }



        }
        gg<<dp[ (1<<n) - 1 ]<<"\n";

    }

    return 0;
}