Cod sursa(job #761318)

Utilizator GrimpowRadu Andrei Grimpow Data 25 iunie 2012 15:13:29
Problema Ghiozdan Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.58 kb
#include <cstdio>
#include <algorithm>

#define GMax 75005
#define WMax 205

using namespace std;

int N[WMax], DP[GMax], Object[GMax], Quantity[GMax], MaxW, MaxG, GS, NS;

void Read ()
{
    freopen ("ghiozdan.in", "r", stdin);
    int n;
    scanf ("%d %d", &n, &MaxG);
    for (; n>0; --n)
    {
        int CurrentW;
        scanf ("%d", &CurrentW);
        ++N[CurrentW];
        MaxW=max (MaxW, CurrentW);
    }
}

void Print ()
{
    freopen ("ghiozdan.out", "w", stdout);
    printf ("%d %d\n", GS, NS);
    while (GS>0)
    {
        int NextG=GS-Object[GS]*Quantity[GS];
        for (; Quantity[GS]>0; --Quantity[GS], printf ("%d\n", Object[GS]));
        GS=NextG;
    }
}

void Initialize ()
{
    for (int G=1; G<=MaxG; ++G)
    {
        DP[G]=GMax;
    }
}

void Solve ()
{
    Initialize ();
    for (int W=MaxW; W>0; --W)
    {
        if (N[W]==0)
        {
            continue;
        }
        for (int G=MaxG; G>=0; --G)
        {
            if (DP[G]==GMax)
            {
                continue;
            }
            for (int K=1; K<=N[W] and G+K*W<=MaxG; ++K)
            {
                if (DP[G]+K<DP[G+K*W])
                {
                    DP[G+K*W]=DP[G]+K;
                    Object[G+K*W]=W;
                    Quantity[G+K*W]=K;
                }
                else
                {
                    break;
                }
            }
        }
    }
    for (int G=MaxG; G>=0; --G)
    {
        if (DP[G]<GMax)
        {
            GS=G, NS=DP[G];
            return;
        }
    }
}

int main()
{
    Read ();
    Solve ();
    Print ();
    return 0;
}