Cod sursa(job #153290)

Utilizator C_OvidiuCotletz Ovidiu C_Ovidiu Data 10 martie 2008 13:04:09
Problema Fractii Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.51 kb
#include<stdio.h>
#include<stdlib.h>
#define nmax 100
#define pmax 3 //cate hash-uri sa faca
//int long prim[6]={2,99971,99989,99991,99961,99929};
long prim[6]={1,23,17,61};
int long x,p,j,poz,s[nmax],urm[nmax],st[nmax],inc,sf,sol,k,n,i;
int long *h[nmax];
int main()
{
freopen("vnem.in","r",stdin);
freopen("vnem.out","w",stdout);
scanf("%ld%ld",&n,&k);
for(i=1;i<=prim[1];i++)
   {
	h[i]=(int long*)realloc(h[i],sizeof(int long));
	h[i][0]=0;
	}


for(i=1;i<=n;i++)
 {urm[i]=n+1;
  scanf("%ld",&s[i]);
  poz=s[i]%prim[1];
  j=1;
  /*
  for(j=1;j<=h[poz][0];j++)
	{
	 x=s[h[poz][j]];
	 for(p=2;p<=pmax;p++)
	   if(x%prim[p]!=s[i]%prim[p])
		   break;
	 if(p==pmax+1)
	   {urm[ h[poz][j] ]=i;h[poz][j]=i;}
	 else
	   {h[poz]=(int long*)realloc(h[poz],(h[poz][0]+1)*sizeof(int long));
		h[poz][++h[poz][0]]=i;
		}
	 }    */
  if(j==1)
	{h[poz]=(int long*)realloc(h[poz],(h[poz][0]+1)*sizeof(int long));
	 h[poz][++h[poz][0]]=i;
	}
  }

st[0]=1;
for(i=2,inc=sf=1,st[1]=1;i<=k;i++)
  {
  st[++sf]=i;
  j=sf;
  while(urm[st[j-1]]>urm[ st[j]])
	{
	//interschimb;
	st[j-1]=st[j-1]^st[j];	st[j]=st[j-1]^st[j]; st[j-1]=st[j-1]^st[j];
	}
  }
for(i=k+1,sol=0;i<=n;i++)
  {
  if(urm[st[inc]]==i)
	{inc++;sol++;}
  st[++sf]=i;
  j=sf;
  while(urm[st[j-1]]>urm[ st[j]])
	{
	//interschimb;
	st[j-1]=st[j-1]^st[j];	st[j]=st[j-1]^st[j]; st[j-1]=st[j-1]^st[j];
	}
  //sf--;


  }

freopen("vnem.out","w",stdout);
printf("%ld",n-sol);
fclose(stdout);





return 0;

}