Cod sursa(job #832449)

Utilizator ericptsStavarache Petru Eric ericpts Data 10 decembrie 2012 17:54:36
Problema Ghiozdan Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.4 kb
#include <cstdio>
using namespace std;
char *p;
const int maxn = 75001;
int best[maxn];
int ap[201];
int before[maxn];
void read (int &a)
{
    a ^= a;
    while (*p < '0' || *p > '9')
        ++p;
    while (*p >= '0' && *p <= '9')
        a = (a<<1) * 5 + *(p++)-'0';
}
int main ()
{
    int N, G, i, j, k;
    long long len;
    freopen ("ghiozdan.in", "r", stdin);
    freopen ("ghiozdan.out", "w", stdout);
    fseek (stdin, 0, SEEK_END);
    len = ftell (stdin);
    p = new char [len];
    rewind (stdin);
    fread (p, 1, len, stdin);
    read (N), read (G);
    for (i = 1; i <= N; i ++)
    {
        read (j);
        ++ap[j];
    }
    best[0] = 1;
    for (i = 200;i;--i)
    {

        if (ap[i])
        {
            for (j = G - i; j >= 0;--j)
            {
                if (best[j])
                {
                    for (k = 1;k<= ap[i]&&(j + i * k) <=G && !best[j + i * k];++k)
                    {
                        if (!best[j + i * k])
                        {
                            best[j + i * k] = best[j] + k;
                            before[j + i * k] = i;
                        }
                    }
                }
            }
        }
    }
    for (i = G; !best[i];--i);
    printf ("%d %d\n", i, best[i] - 1);
    while (i){
        printf ("%d\n", before[i]);
        i -= before[i];
    }
    return 0;
}