Cod sursa(job #425927)

Utilizator doru.nituNitu Doru Constantin doru.nitu Data 26 martie 2010 11:48:54
Problema Partitie Scor 50
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.22 kb
#include<stdio.h>
#include<algorithm>

using namespace std;

int i,j,n,d,rez,sol[300009],nrp;

struct nod
{
	int x,poz;
}a[300009];

struct part
{
	int fol,m,ma;
}c[300009];

bool comp(nod a,nod b)
{
	return a.x<b.x;
}

int cautbin(int x)
{
	int st=1,dr=rez,mij=0,ret=0;
	
	while(st<=dr)
	{
		mij=(st+dr)/2;
		
		if(x-c[mij].ma>=d)
		{
			if(c[mij].fol)
			{
				int j=mij;
				while(j>0&&c[j].fol) --j;
				ret=j;
				st=mij+1;
			}
			else 
			{
				ret=mij;
				st=mij+1;
			}
		}
		else dr=mij-1;
	}
	
	return ret;
}

int main()
{
	freopen("partitie.in","r",stdin);
	freopen("partitie.out","w",stdout);
	
	scanf("%d %d",&n,&d);
	
	for(i=1;i<=n;++i)
	{
		scanf("%d",&a[i].x);
		a[i].poz=i;
	}
	
	sort(a+1,a+n+1,comp);
	
	c[++rez].m=(++nrp);
	c[rez].ma=a[1].x;
	sol[ a[1].poz ]=nrp; 
	
	for(i=2;i<=n;++i)
	{
		int  k=cautbin(a[i].x);
		
		if(!k)
		{
			c[++rez].m=(++nrp);
			c[rez].ma=a[i].x;
			sol[ a[i].poz ]=nrp;
		}
		
		else 
		{
			c[++rez]=c[k];
			c[k].fol=1;
			c[rez].ma=a[i].x;
			sol[ a[i].poz]=c[k].m;
		}
	}
	
	printf("%d\n",nrp);
	
	for(i=1;i<=n;++i)
		printf("%d\n",sol[i]);
	
	fclose(stdin);
	fclose(stdout);
	

	return 0;
}