Cod sursa(job #774451)

Utilizator deea101Andreea deea101 Data 4 august 2012 22:22:34
Problema Ghiozdan Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.16 kb
/*#include <fstream>
using namespace std;
ifstream f("ghiozdan.in");
ofstream g("ghiozdan.out");
int b[75001];
int main()
{
	int n,G;
	f>>n>>G;
	int i,p,j,smax=0,w[G],nmin;
	f>>w[0]; b[w[0]]=1; smax=w[0]; 
	for(i=1;i<n;i++)
	{
		f>>w[i];
		for(j=G-w[i];j>=0;j--)
		{
			if((b[j]||j==0) && j+w[i]<=G)
			{
				if(b[j+w[i]])
				{
					if (b[j]+1<b[j+w[i]]) 
					{
						b[j+w[i]]=b[j]+1;
					}
					else 1;
				}
				else b[j+w[i]]=b[j]+1;
				
			}
		}
		
	}
	for(i=1;i<=G;i++) if(b[i] && smax<i) smax=i;
	g<<smax<<' '<<b[smax]<<endl; 
	
}*/
#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;
}