Pagini recente » Cod sursa (job #792668) | Cod sursa (job #3175590) | Cod sursa (job #1076571) | Cod sursa (job #2853358) | Cod sursa (job #227565)
Cod sursa(job #227565)
#include <cstdio>
FILE *in, *out;
int x[300002],v[300000],c[300000],n,d,max=-32000;
int abs (int x)
{
if (x<0)
return -x;
return x;
}
int main()
{
in = fopen ("partitie.in", "r");
out = fopen ("partitie.out", "w");
fscanf (in, "%d%d", &n, &d);
int i;
for (i=1; i<=n; i++)
fscanf (in, "%d", &x[i]),v[i]=1;
int gata=0,j;
while (!gata)
{
gata=1;
for (i=1; i<n; i++)
for (j=i+1; j<=n; j++)
if (v[i]==v[j])
if (abs (x[i]-x[j])<d)
{
v[j]++,gata=0;
if (v[j]>max)
max=v[j];
}
}
int gasit;
//vectoru c
for (i=1; i<=n; i++)
if (c[ v[i] ]<2)
c[ v[i] ]++;
unel:
//daca gasesc subm cu 1 el
gasit=0;
for (i=1; i<=n && !gasit; i++)
if (c[i]==1)
gasit=i;
//for (i=1; i<=n; i++)
// printf ("%d ",v[i]);
if (gasit)
{
c[gasit]++;
//pe ce poz se afla acel el
int poz;
for (i=1; i<=n; i++)
if (v[i]==gasit)
{poz=i;break;}
//gasesc un el ce sa il bag in multime cu ala singur
for (i=1; i<=n; i++)
if (v[i]!=gasit && x[i]>0)
if (abs (x[i]-x[poz])>=d)
{
v[i]=v[poz];
x[i]*=-1;
printf ("%d %d\n",x[i],x[poz]);
break;
}
}
if (gasit)
{
goto unel;
}
fprintf (out, "%d\n",max);
for (i=1; i<=n; i++)
fprintf (out,"%d\n",v[i]);
fclose (in);
fclose (out);
return 0;
}