Cod sursa(job #1173071)

Utilizator mvcl3Marian Iacob mvcl3 Data 18 aprilie 2014 16:17:45
Problema Ghiozdan Scor 20
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.56 kb
#include <fstream>
#include <vector>

#define in "ghiozdan.in"
#define out "ghiozdan.out"

class Ghiozdan
{
    protected :
        int N, G;
        std :: vector < int > V, HowMuch, DP;
        std :: vector < std :: pair < int ,int > > T;

    public :
        void Read_Input_Data(){
            std :: ifstream f(in);
            f >> N >> G;
            V.resize(N + 1), T.resize(G + 1), HowMuch.resize(G + 1), DP.resize(G + 1);
            for(int i = 1; i <= N; ++i)  f >> V[i];

            f.close();
        }

        void Solve()    {
            DP[0] = 1, HowMuch[0] = 0;
            for(int i = 1; i <= N; ++i)
                for(int j = G - V[i]; j >= 0; --j)
                    if(DP[j])   {
                        if(!DP[j + V[i]])   {
                            DP[j + V[i]] = 1;
                            HowMuch[j + V[i]] = HowMuch[j] + 1;
                            T[j + V[i]].first = j;
                            T[j + V[i]].second = V[i];
                         }
                    }
        }

        void Write_Output_Data()    {
            std :: ofstream g(out);
            int Gmax;

            for(int i = G; i >= 0; --i)
                if(DP[i])   {
                    Gmax = i;
                    break;
                }
            g << Gmax << ' ' << HowMuch[Gmax] << '\n';
            while(Gmax) {
                g << T[Gmax].second << '\n';
                Gmax = T[Gmax].first;
            }

            g.close();
        }
} obj;

int main()
{
    obj.Read_Input_Data();
    obj.Solve();
    obj.Write_Output_Data();

    return 0;
}