Cod sursa(job #18568)

Utilizator TheCreeepIonita Andrei Lucian TheCreeep Data 18 februarie 2007 12:38:39
Problema Ghiozdan Scor 50
Compilator cpp Status done
Runda preONI 2007, Runda 2, Clasele 11-12 Marime 0.95 kb
#include <stdio.h>
#include <stdlib.h>
int g,n;
int y[75001],t,i,j,k,v;
char x[201];
struct nod
{
	int count;
	int val;
	char nr;
	struct nod * last;
} *q[75001], *tmp, *z[75001];
FILE *f;
int main (void)
{
	f=fopen("ghiozdan.in","r");
	fscanf(f,"%d %d",&n,&g);
	for(i=1;i<=n;i++) fscanf(f,"%d",&t),x[t]++;
	fclose(f);
	z[0]=new struct nod;
	z[0]->last=0;
	z[0]->count=1;
	z[0]->val=0;
	for(i=200;i>=0;i--)
	{
		if (x[i]!=0)
			for(j=g;j>=0;j--)		
			{
				

				if (z[j]!=0 && z[j]->val!=i)
				for(k=1;k<=x[i] && (v=j+k*i)<=g && (z[v]==0  || (z[v]->count > z[j]->count+k && z[v]->val!=i));k++) 
				{
					tmp = new struct nod;
					tmp->val=i;
					tmp->count = z[j]->count+k;
					tmp->nr = k;
					tmp->last=z[j];
					z[v]=tmp;
				}
			}
	}
	while (z[g]==0) g--;
	f=fopen("ghiozdan.out","w");
	tmp=z[g];
	fprintf(f,"%d %d\n",g,z[g]->count-1);
	while (tmp!=z[0])
	{
		for(i=1;i<=tmp->nr;i++) 
			fprintf(f,"%d\n",tmp->val);
		tmp=tmp->last;
	}
	fclose(f);
	return 0;
}