Cod sursa(job #959692)

Utilizator sleepaholicNeculaescu Theodor sleepaholic Data 8 iunie 2013 15:04:22
Problema Ghiozdan Scor 100
Compilator cpp Status done
Runda Lista lui wefgef Marime 1.54 kb
#include <fstream>
#include <iostream>
#include <array>
#include <vector>
#include <map>
#include <iterator>
#include <algorithm>

#define MAX_SIZE 75001
#define MAX_WEIGHT 201

using namespace std;

array<short, MAX_SIZE> knapsack;
array<short, MAX_SIZE> solution;
array<short, MAX_WEIGHT> items;

int main()
{
    int N, G;
    fstream fin("ghiozdan.in", fstream::in);
    fstream fout("ghiozdan.out", fstream::out);

    fin >> N >> G;
    //cout << N << " " << G << endl;

    for (int i=0; i<N; ++i)
    {
        int item;
        fin >> item;

        items[item]++;
    }

    knapsack[0] = 1;
    items[0] = 1;
    for (int i=200; i>=0; --i)
    {
        if (items[i] == 0) continue;

        //cout << "Items " << i << " " << items[i] << endl;

        for (int w=G-i; w>=0; --w)
        {
            if (knapsack[w] == 0) continue;

            for (int k=1; k<=items[i] && w + k*i <= G && knapsack[w + k*i] == 0; ++k)
            {
                //cout << k << " " << i << " " << w << " " << knapsack[w + k*i] << endl;

                knapsack[w + k*i] = knapsack[w] + k;

                solution[w + k*i] = i;
            }
        }
    }

    for (int w=G; w>0; --w)
    {
        if (knapsack[w]-1 > 0)
        {
            fout << w << " " << knapsack[w]-1 << "\n";

            int i = w;
            while (i > 0)
            {
                fout << solution[i] << "\n";
                i -= solution[i];
            }

            return 0;
        }
    }

    return 0;
}