Cod sursa(job #1772298)

Utilizator nnnmmmcioltan alex nnnmmm Data 6 octombrie 2016 17:22:59
Problema Ghiozdan Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.14 kb
#include<cstdio>
#include<algorithm>
const int GMAX=75001,NMAX=20001;
int d[GMAX],v[NMAX],t[GMAX];
int main()
{
 FILE *in=fopen("ghiozdan.in","r");
 int g,n,max=0;
 fscanf(in,"%d %d ",&n,&g);
 for(int i=1;i<=n;i++)
     {
      int x=0;
      fscanf(in,"%d ",&x);
      v[x]++;
     }
 d[0]=1;
 for(int x=200;x>0;x--)
     {
      if(v[x]>0)
      {
      //d[x]=1;
      for(int j=g-x;j>=0;j--)
          {
           if(d[j]!=0 && d[j]!=NMAX)
              {
               for(int sq=1;sq<=v[x] && j+x*sq<=g && (d[j+x*sq]==0 || d[j+x*sq]==NMAX);sq++){
               d[j+x*sq]=d[j+x*sq]==0?NMAX:d[j+x*sq];
               if(d[j+x*sq]>d[j]+sq)
                  {
                   d[j+x*sq]=d[j]+sq;
                   t[j+x*sq]=x;
                   //d[j+x]=std::min(d[j+x],d[j]+1);
                  }
               max=std::max(max,j+x*sq);
              }}
          }
      }
     }
 fclose(in);
 FILE *out=fopen("ghiozdan.out","w");
 fprintf(out,"%d %d\n",max,d[max]-1);
 int poz=max;
 while(poz!=0)
       {
        fprintf(out,"%d\n",t[poz]);
        poz=poz-t[poz];
       }
 fclose(out);
 return 0;
}