Cod sursa(job #18419)

Utilizator flo_demonBunau Florin flo_demon Data 18 februarie 2007 12:06:47
Problema Ghiozdan Scor 24
Compilator cpp Status done
Runda preONI 2007, Runda 2, Clasele 11-12 Marime 2.26 kb
#include <stdio.h>
#include <map>
#include <iostream>
#include <vector>

using namespace std;

struct stare {
    int N, G;
};

int n, sum, ob, aux, Nmin, curr;
map<int, int> pack, t;
map<int, int>::iterator start, end;
vector<stare> val_to_add;
vector<stare>::iterator start2, end2;
stare v_aux;

int main()
{
    FILE *fin = fopen("ghiozdan.in", "r");
    fscanf(fin, "%d%d", &n, &sum);
    pack[0] = 0;
    val_to_add.reserve(n+6);
    for (int i = 1; i <= n; ++i)
    {
        fscanf(fin, "%d", &ob);
        start = pack.begin();
        end = pack.end();
        val_to_add.clear();
        for (; start != end; ++start)
        {
            aux = (*start).first + ob;
            if (pack.count(aux))
            {
                if (pack[aux] > (*start).second + 1)
                {
                    v_aux.G = aux;
                    v_aux.N = (*start).second + 1;
                    val_to_add.push_back(v_aux);
                }
            }
            else
            {
                v_aux.G = aux;
                v_aux.N = (*start).second + 1;
                val_to_add.push_back(v_aux);
            }
        }
        start2 = val_to_add.begin();
        end2 = val_to_add.end();
        for (; start2 != end2; ++start2)
        {
            if (pack.count((*start2).G))
            {
                if (pack[(*start2).G] > (*start2).N)
                {
                    pack[(*start2).G] = (*start2).N;
                    t[(*start2).G] = ob;
                }
            }
            else
            {
                pack[(*start2).G] = (*start2).N;
                t[(*start2).G] = ob;
            }
        }
    }
    fclose(fin);
    
    FILE *fout = fopen("ghiozdan.out", "w");
    for (int j = sum; j >= 0; --j)
    {
        if (pack.count(j))
        {
            Nmin = pack[j];
            fprintf(fout, "%d %d\n", j, Nmin);
            for (int i = 1; i <= Nmin; ++i)
                fprintf(fout, "0\n");
           /* curr = j;
            while (curr)
            {
                ob = t[curr];
                fprintf(fout, "%d\n", ob);
                curr -= ob;
            }*/
            break;
        }
    }
    fclose(fout);
    
    
    return 0;
}