Cod sursa(job #10897)

Utilizator crawlerPuni Andrei Paul crawler Data 29 ianuarie 2007 21:08:44
Problema NextSeq Scor 20
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.84 kb
#include <stdio.h>

int x[10001], a[10001], b[10001], u[10001], v[10001], n, m, p;


void qsort(long st,long dr)
{
  long i,j,sc,aux;
  i=st;j=dr;
  sc=x[(st+dr)/2];
  do
   {
     while(x[i]<sc)++i;
     while(x[j]>sc)--j;
     if(i<=j)
      {
        aux=x[i];
        x[i]=x[j];
        x[j]=aux;
        i++;j--;
      }
   }while(!(i>j));
   
if(i<dr)qsort(i,dr);

if(j>st)qsort(st,j);

}

void trans()
{
  int i, j, tmp, g, y;

  tmp=128;
  while(tmp>m)
   tmp>>=1;

  for(i=1;i<=n;++i)
   {
     g=tmp;
     j=g;
     do
     {
       if(x[j]<a[i])
       j+=g;
       if(x[j]>a[i])
        {
         j-=g;
         g>>=1;
        }
     }while(a[i]!=x[j] & g!=0);
   v[i]=j;
   }

  for(i=1;i<=p;++i)
   {
     g=tmp;
     j=g;
     do
      {
        if(x[j]<b[i])
        j+=g;
        if(x[j]>b[i])
        {
          j-=g;
          g>>=1;
        }
       }while(b[i]!=x[j] & g!=0);
     u[i]=j;
   }

  v[0]=n;
  u[0]=p;
   
}

void sub()
{
    int i, t = 0;
 
    for (i = 1; i <= u[0]; i++)
      {
        u[i]-=v[i]+t;
        if(u[i]<1 && i^u[0])
         {
          u[i]+=m;
          t=1;
         }
          else
        if(u[i]<1 && i==u[0])
         {
          --u[0];
         }
          else
         t=0;
      }
}


int main()
{
  freopen("nextseq.in","r",stdin);
  freopen("nextseq.out","w",stdout);

  int i, j, tmp, g, y;

  scanf("%i %i %i",&m,&n,&p);

  for(i=1;i<=m;++i)
   scanf("%i", &x[i]);

  for(i=1;i<=n;++i)
   scanf("%i", &a[n-i+1]);

  for(i=1;i<=p;++i)
   scanf("%i", &b[p-i+1]);
 
  qsort(1,m);
  
  trans();
  
  sub();

  tmp=1;
  int s=0;
  
  for(i=1;i<=u[0];++i)
   {
     s+=u[i]*tmp;
     tmp*=m;
   }

  --s;
  if(s>=0)
   printf("%i\n",s);
    else
   printf("0\n");
  
  return 0;
}