Cod sursa(job #3140103)

Utilizator NiffSniffCojocaru Calin Marcu NiffSniff Data 3 iulie 2023 20:14:14
Problema Ghiozdan Scor 6
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.22 kb
#include <fstream>
#include <algorithm>
#include <bitset>
#include <vector>
using namespace std;
string file = "ghiozdan";
ifstream cin (file + ".in");
ofstream cout (file + ".out");
struct ura{
    int x, nr;
};
vector <ura> v[75001];
vector <int> available;
int obiecte[201];
bitset <75001> exista;
int main()
{
    int n,g,x;
    cin >> n >> g;
    for (int i=1; i<=n; i++)
    {
        cin >> x;
        obiecte[x]++;
    }
    for (int i=200; i>=1; i--)
    {
        while (obiecte[i]--)
        {
            vector <int> temp;
            x = i;
            for (int greutate : available)
            {
                if (greutate+x > g)
                    continue;
                if (!exista[greutate + x])
                {
                    temp.push_back(greutate+x);
                    exista[greutate+x] = 1;
                    v[greutate+x] = v[greutate];
                    if (v[greutate+x].back().x == x)
                    {
                        v[greutate+x].back().nr++;
                    }
                    else
                    {
                        v[greutate+x].push_back({x,1});
                    }
                }
                else if (v[greutate].size() + 1 < v[greutate+x].size())
                {
                    v[greutate+x] = v[greutate];
                    if (v[greutate+x].back().x == x)
                    {
                        v[greutate+x].back().nr++;
                    }
                    else
                    {
                        v[greutate+x].push_back({x,1});
                    }
                }
            }

            v[x] = {{x,1}};
            if (!exista[x])
            {
                exista[x] = 1;
                temp.push_back(x);
            }

            for (int x : temp)
            {
                available.push_back(x);
            }
        }
    }
    for (int i=g; i>=1; i--)
    {
        if (v[i].size())
        {
            cout << i << ' ' << v[i].size();
            for (ura x : v[i])
            {
                while (x.nr--)
                {
                    cout << '\n' << x.x;
                }
            }
        }
    }
}