Cod sursa(job #2046045)

Utilizator patcasrarespatcas rares danut patcasrares Data 23 octombrie 2017 12:34:00
Problema Zebughil Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.1 kb
#include<fstream>
using namespace std;
ifstream fin("zebughil.in");
ofstream fout("zebughil.out");
int n,a[(1<<18)],b[(1<<18)],g,x[25],p,t,r,nr;
int main()
{
    for(int h=1;h<=3;h++)
    {
    fin>>n>>g;
    b[0]=0;
    for(int i=1;i<=n;i++)
        fin>>x[i];
    p=(1<<n)-1;
    for(int i=1;i<=p;i++)
    {
        t=i;
        a[i]=2000;
        b[i]=0;
        for(int j=0;j<n;j++)
            if(((1<<j)&i))
            {
                t=(i^(1<<j));
                r=a[t]+1;
                if(b[t]>=x[j+1])
                    r=a[t];
                if(r==a[i])
                {
                    a[i]=r;
                    if(b[t]>=x[j+1])
                        b[i]=max(b[i],b[t]-x[j+1]);
                    else
                        b[i]=max(b[i],g-x[j+1]);
                }
                if(r<a[i])
                {
                    a[i]=r;
                    if(b[t]>=x[j+1])
                        b[i]=b[t]-x[j+1];
                    else
                        b[i]=g-x[j+1];
                }
            }
    }
    fout<<a[p]<<'\n';
    }
}