Cod sursa(job #683288)

Utilizator ion_calimanUAIC Ion Caliman ion_caliman Data 20 februarie 2012 13:34:23
Problema Ghiozdan Scor 20
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.95 kb
#include <fstream>
#include <string>
#include <sstream>
using namespace std;
ifstream f("ghiozdan.in");
ofstream g("ghiozdan.out");

#define NMAX 20010
#define GMAX 75010

int D1[GMAX], D2[GMAX], B1[GMAX], B2[GMAX];
string S1[GMAX], S2[GMAX];
int N, G;

string i2s(int x)
{
    stringstream ss;
    string s;
    ss << x;
    ss >> s;
    return s;
}

int main()
{
    int i, j, x;
    f >> N >> G;
    for (i=1; i<=N; i++){
        f >> x;
        for (j=0; j<=G; j++){
            D2[j]=D1[j];
            B2[j]=B1[j];
            S2[j]=S1[j];
            if (x<=j){
                if (D1[j-x]+x>D1[j] || (D1[j-x]+x==D1[j] && B1[j-x]+1<B1[j])){
                    D2[j]=D1[j-x]+x;
                    B2[j]=B1[j-x]+1;
                    S2[j]=S1[j-x]+"\n"+i2s(x);
                }
            }
        }
        swap(D1,D2);
        swap(B1,B2);
        swap(S1,S2);
    }
    g << D1[G] << ' ' << B1[G] << S1[G];
}