Cod sursa(job #1594569)

Utilizator bogdan10bosBogdan Sitaru bogdan10bos Data 9 februarie 2016 16:20:12
Problema Ghiozdan Scor 80
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.66 kb
#include <bits/stdc++.h>

using namespace std;

const int INF = 1 << 30;
const long long LLINF = 1LL << 62;
const int mod = 1000000007;

int buffer = 1 << 13;
int cnt = buffer - 1;
char buff[10005];

char gchar()
{
    if(++cnt == buffer)
    {
        cnt = 0;
        fread(buff, buffer, 1, stdin);
    }
    return buff[cnt];
}

int gint()
{
    int x = 0;
    char c = gchar();
    while(c < '0' || '9' < c)
        c = gchar();
    while('0' <= c && c <= '9')
    {
        x = x * 10 + c - '0';
        c = gchar();
    }
    return x;
}

int n, g, i, j, k, x;
int f[205], dp[75005], lst[75005];
int prv[75005], nxt[75005];

int main()
{
    freopen("ghiozdan.in", "r", stdin);
    freopen("ghiozdan.out", "w", stdout);

    n = gint();
    g = gint();
    while(n--)
    {
        x = gint();
        f[x]++;
    }

    for(i = 1; i <= g; i++)
    {
        prv[i] = i - 1;
        nxt[i] = i + 1;
    }
    prv[g + 1] = g;

    dp[0] = 1;
    for(i = 200; i >= 1; i--)
        if(f[i])
        for(j = 1; j <= f[i]; j++)
            for(k = prv[g + 1]; k >= i; k = prv[k])
                if(dp[k - i])
                {
                    dp[k] = dp[k - i] + 1;
                    lst[k] = k - i;
                    nxt[ prv[k] ] = nxt[k];
                    prv[ nxt[k] ] = prv[k];
                }

    for(i = g; i >= 1; i--)
        if(dp[i])
        {
            printf("%d %d\n", i, dp[i] - 1);
            j = i;
            while(j > 0)
            {
                printf("%d\n", j - lst[j]);
                j = lst[j];
            }
            return 0;
        }

    return 0;
}