Cod sursa(job #952379)

Utilizator claudiumihailClaudiu Mihail claudiumihail Data 23 mai 2013 11:01:42
Problema Ghiozdan Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.64 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;
}