Cod sursa(job #2168426)

Utilizator trz59lollMurariu Iulian trz59loll Data 14 martie 2018 10:54:15
Problema Zebughil Scor 40
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.36 kb
#include<bits/stdc++.h>
#define maxn 20
using namespace std;
ifstream f ("zebughil.in");
ofstream gout ("zebughil.out");

int v[maxn],n,g;
bool vz[maxn],ok;

int main()
{
    size_t t,i,gc,nr = 1,j;
    for(t = 1; t <= 3; t ++)
    {
        nr = 1;
        f>>n>>g;
        for(i = 1; i <= n; i ++)
            f>>v[i];
        sort(v + 1, v + n + 1);
        gc = g;
        memset(vz , false , n + 1);
        for(i = n; i >= 1; i --)
           {
               ok = false;
               //gout<<gc<<" "<<v[i]<<'\n';
               if(v[i] <= gc && vz[i] == false)
                {
                    gc -= v[i];
                    vz[i] = true;
                    ok = true;
                }
                else
                {
                    for(j = 1; j <= n; j ++)
                        if(v[j] <= gc && vz[j] == false)
                        {
                            gc -= v[j];
                            vz[j] = true;
                            ok = true;
                            break;
                        }
                }
                if(ok == false)
                {
                    //gout<<'\n'<<gc<<" "<<nr<<'\n'<<'\n';
                    gc = g - v[i];
                    vz[i] = true;
                    nr ++;
                }

        }
    gout<<nr<<'\n';
    }


}