Cod sursa(job #66331)

Utilizator M@2Te4iMatei Misarca M@2Te4i Data 17 iunie 2007 19:38:06
Problema Shop Scor 60
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.57 kb
#include<stdio.h>
#include<string.h>

int n, c, max,a[31],b[31],w, q[31], m[31], v[31], p;
/*long*/ long l;

void citire()
{
freopen("shop.in","r",stdin);
scanf("%d%d%ld", &n, &c, &l);
max=0;
for (int i=0; i<n; i++)
    {
    scanf("%d%d",&a[i], &b[i]);
    if (a[i]>max)
       max=a[i];
    q[i]=i;
    }
fclose(stdin);
}

int nr_pasi(int max)
{
int pas=0;
while (max>0)
      {
      ++pas;
      max>>=1;
      }
return pas;
}

void sortare()
{
int z, u, unu[31],bb[31], j, cc[31];
for (int i=0; i<w; i++)
    {
    z=-1;
    u=-1;
    for (j=0; j<n; j++)
	if ((a[j] >> i & 1)==0)
	   {
	   z++;
	   a[z]=a[j];
	   b[z]=b[j];
	   q[z]=q[j];
	   }
	   else {
		++u;
		unu[u]=a[j];
		bb[u]=b[j];
		cc[u]=q[j];
		}
    u=-1;
    for (j=z+1; j<n; j++)
	{
	a[j]=unu[++u];
	b[j]=bb[u];
	q[j]=cc[u];
	}
    }
}

void afisare()
{
freopen("shop.out","w",stdout);
printf("%d\n",p);
for (int i=0; i<n; i++)
    printf("%d ",b[i]);
fclose(stdout);
}

void lungime()
{
int q=0,e=1,i=0;
while (i<n)
      {
      if (q==a[i])
	 {
	 m[i]=e;
	 i++;
	 if (i==n)
	    break;
	 }
      q++;
      e*=c;
      }
}

void rezolvare(int w, long long e)
{
while (e>0)
      {
      if (m[w]>e || b[w]<=0)
	 w--;
	 else {
	      v[w]++;
	      b[w]--;
	      p++;
	      e-=m[w];
	      }
      }
}

void resortare()
{
for (int i=0; i<n; i++)
    b[q[i]]=v[i];
}

int main()
{
citire();
w=nr_pasi(max);
sortare();
lungime();
memset(v,0,sizeof(v));
p=0;
rezolvare(n-1,l);
resortare();
afisare();
return 0;
}