Cod sursa(job #125063)

Utilizator RobytzzaIonescu Robert Marius Robytzza Data 20 ianuarie 2008 11:11:22
Problema Partitie Scor 40
Compilator cpp Status done
Runda preONI 2008, Runda 3, Clasa a 9-a Marime 1.15 kb
#include <fstream.h>

ifstream fin("partitie.in");
ofstream fout("partitie.out");

int a[300010],b[300010];
int sir[300010];
int n,d,nr;

void citire()
{
   fin>>n>>d;
  for (int i=0;i<n;i++)
  {
      fin>>a[i];
      b[i]=i+1;
  }
  fin.close();
}

void ord()
{
  int ok=0,p=n;
  while (!ok)
  {
     ok=1;
     p--;
	for (int i=0;i<p;i++)
	   if (a[i]>a[i+1])
	   {
	      int aux=a[i];
	      a[i]=a[i+1];
	      a[i+1]=aux;
	      aux=b[i];
	      b[i]=b[i+1];
	      b[i+1]=aux;
	      ok=0;
	   }
  }
}

void numarare()
{
  nr=1;
   for (int i=0;i<n;i++)
     if (a[i]!=-11111)
     {
      sir[b[i]]=nr;
      int aux=a[i];
      for (int j=i+1;j<n;j++)
	  if (a[j]-aux>=d)
	  {
	     aux=a[j];
	     a[j]=-11111;
	     sir[b[j]]=nr;
	  }
      nr++;
}
}

int main()
{
   citire();
   ord();
   numarare();
   fout<<nr-1<<"\n";
   int numar=-1;
   for (int i=1;i<=n;i++)
      if (sir[i]>0)
      {
      for (int j=i+1;j<=n;j++)
	  if (sir[j]==sir[i])
	      sir[j]=numar;
     sir[i]=numar--;
   }
      for (int k=1;k<=n;k++)
	 fout<<sir[k]*(-1)<<"\n";
   fout.close();
   return 0;
}