Cod sursa(job #2443893)

Utilizator Carol_LucaCarol Luca Carol_Luca Data 29 iulie 2019 18:32:27
Problema Ghiozdan Scor 90
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.62 kb
#include <fstream>

#include <cstdio>

#define INF 2000000000

#define DIM 210

#define DIMG 80000

#define DIMBUFF 5000000

using namespace std;



FILE *fin = fopen ("ghiozdan.in","r");

FILE *fout = fopen ("ghiozdan.out","w");



int f[DIM],d[DIMG],ant[DIMG];

int g,n,i,j,t,maxi,x,pos,mx;

char buff[DIMBUFF];

inline int get_nr(){

    while(!(buff[pos] >= '0' && buff[pos] <= '9'))

        pos++;

    int nr = 0;

    while(buff[pos] >= '0' && buff[pos] <= '9'){

        nr = nr*10 + buff[pos] - '0';

        pos++;

    }

    return nr;

}

int main (){



    fread (buff,1,DIMBUFF,fin);

    n = get_nr(), g = get_nr();

    for (i=1;i<=n;i++){

        x = get_nr();

        f[x]++;

        mx = max (mx,x);

    }

    /// d[i][j] - nr min de obiecte pt a obtine greutatea j folosind\


    for (j=0;j<=g;j++)

        d[j] = INF;

    d[0] = 0;

    for (i=mx;i;i--){

        if (!f[i]) /// nu are sens sa il mai iau in calcul daca nu apare

            continue;

        for (j=g;j>=0;j--){

            if (d[j] != INF){

                for (t=1;t<=f[i];t++){

                    if (j+i*t > g)

                        break;

                    if (d[j+i*t] != INF)

                        continue;

                    maxi = max (maxi,j+i*t);

                    d[j+i*t] = d[j]+t;

                    ant[j+i*t] = i;

                }}}}



    fprintf(fout,"%d %d\n",maxi,d[maxi]);

    x = maxi;

    while (x){

        fprintf(fout,"%d\n",ant[x]);

        x -= ant[x];

    }





    return 0;

}