Cod sursa(job #504496)

Utilizator mgntMarius B mgnt Data 27 noiembrie 2010 21:34:32
Problema Overlap Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.4 kb
#include<fstream>
#include<algorithm>
using namespace std;

struct P2
{	int x,y,i;
	bool operator<(P2 const&o)
	{return (x==o.x)?(y<o.y):(x<o.x);}
	bool operator==(P2 const&o)
	{return (x==o.x)&&(y==o.y);}
};

int const maxn=800;
P2 Z[maxn];
int P[maxn],M[maxn],V[maxn],L[maxn],N,R[maxn];

bool label()
{	int i,j,c;
	for(i=0;N>i;++i){V[i]=0;}
	for(i=0;N>i;++i)
	{	if(0==M[i])
		{	for(c=0,j=i;(-1!=j)&&(0==V[j]);j=P[j]){L[j]=1+c;c=1-c;V[j]=1;}
			if(0!=c){return false;}
		}
	}
	for(i=0;N>i;++i)
	{	if(0==V[i])
		{	for(c=0,j=i;(-1!=j)&&(0==V[j]);j=P[j]){L[j]=1+c;c=1-c;V[j]=1;}
			if(0!=c){return false;}
		}
	}
	return true;
}

void solve()
{	int r,i,rx,ry,j,k,a,dx,dy,xc,yc,p,q,s;
	P2 pc;
	make_heap(Z,Z+N);
	sort_heap(Z,Z+N);
	for(i=0;N>i;++i){R[Z[i].i]=i;}
	for(r=0;4>r;++r)
	{	rx=Z[0].x;ry=Z[0].y;
		for(j=0;r>j;++j){a=rx;rx=-ry;ry=a;}
		for(i=1;N>i;++i)
		{	dx=Z[i].x-rx;dy=Z[i].y-ry;
			for(j=0;N>j;++j){P[j]=-1;}
			for(j=0;N>j;++j){M[j]=0;}
			for(j=0;N>j;++j)
			{	xc=Z[j].x;yc=Z[j].y;
				for(k=0;r>k;++k){a=xc;xc=-yc;yc=a;}
				xc+=dx;yc+=dy;pc.x=xc;pc.y=yc;
				p=0;s=N-1;
				while(p<s)
				{	q=(p+s)/2;
					if(Z[q]<pc){p=q+1;}else{s=q;}
				}
				if(pc==Z[p]){P[j]=p;M[p]=1;}else{P[j]=-1;}
			}
			if(label()){return;}
		}
	}
}

int main()
{	ifstream is("overlap.in");
	ofstream os("overlap.out");
	int i;is>>N;
	for(i=0;N>i;++i){is>>Z[i].x>>Z[i].y;Z[i].i=i;}
	solve();
	for(i=0;N>i;++i){os<<L[R[i]]<<endl;}
	return 0;
}