Cod sursa(job #46611)

Utilizator znakeuJurba Andrei znakeu Data 2 aprilie 2007 19:44:32
Problema Shop Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.93 kb
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
struct ban
{
	long long c,p,u;
};
ban v[35];

int comp(const void *a, const void *b)
{
	ban *aa=(ban*) a, *bb=(ban*) b;
	ban x=*aa, y=*bb;
	int xx=x.p,yy=y.p;
	if (xx<yy)
		return -1;
	if (xx>yy)
		return 1;
	return 0;
}

int main()
{
	long long i,c,l,n,cused=0,max,x,y,z=0;

	FILE *in=fopen("shop.in","r"),*out=fopen("shop.out","w");
	fscanf(in,"%lld%lld%lld",&n,&c,&l);
	for (i = 0 ; i < n; i++)
		fscanf(in,"%lld%lld",&v[i].p,&v[i].c);
	fclose(in);
	
	while (l && cused<=n)
	{
		max=n;
		for (i=0; i<n; i++)
			if (v[i].p>=v[max].p && v[i].c!=0)
				max=i;
		x=(long long)pow(c,v[max].p);
		y=l/x;
		if (v[max].c<y)
			y=v[max].c;
		z+=y;
		v[max].u=y;
		l-=x*y;
		v[max].c=0;
		cused++;
	}
	
	fprintf(out,"%lld\n",z);
	for (i=0; i<n-1; i++)
		fprintf(out,"%lld ",v[i].u);
	fprintf(out,"%lld\n",v[n-1].u);
	fclose(out);
	return 0;
}