Cod sursa(job #216357)

Utilizator hadesgamesTache Alexandru hadesgames Data 24 octombrie 2008 00:35:26
Problema Patrate 3 Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.96 kb
#include <cstdio>
#include <vector>
#include <queue>
#include <stack>
#include <list>
#include <set>
#include <algorithm>
#include <utility>
#include <string>
#include <functional>
#include <sstream>
#include <fstream>
#include <iostream>
using namespace std;
#define FOR(i,a,b) for (i=a;i<=b;i++)
#define fori(it,v) for (it=(v).begin();it!=(v).end();it++)
#define pb push_back
#define mp make_pair
#define fs first
#define ss second
#define all(c) c.begin(),c.end()
#define pf push_front
#define popb pop_back
#define popf pop_front
#define marja 0.0000001
int n;
typedef pair<double,double> DD;
vector<DD> a;
pair<DD,DD> puncte(int i,int j)
{
	DD x=a[i],y=a[j],z,t;
	z=mp((x.fs+y.fs-(y.ss-x.ss))/2,(y.ss+x.ss+(y.fs-x.fs))/2);
	t=mp((x.fs+y.fs+(y.ss-x.ss))/2,(y.ss+x.ss-(y.fs-x.fs))/2);
	return mp(z,t);
}
double mod (double x)
{
	return x<0?-x:x;
}
int egal(DD x,DD y)
{
	if (mod(x.fs-y.fs)>marja)
		return 0;
	if (mod(x.ss-y.ss)>marja)
		return 0;
	return 1;
}
/*int cauta(DD x)
{
	vector<DD>::iterator it=lower_bound(all(a),x);
	return (egala(x,*it)||egal(*(--it),x));
}*/
int ver(int x,DD y)
{
	if (x>n-1)
		return 1;
	if (mod(a[x].fs-y.fs)<=marja)
		if (mod(a[x].ss-y.ss)<=marja||a[x].ss>y.ss)
			return 1;
		else 
			return 0;
	if (a[x].fs>y.fs)
		return 1;
	return 0;

}
int cauta(DD x)
{
	int s,i=0,j=0;
	for(s=1<<10;s;s>>=1)
	{
		i+=s;
		if (ver(i-1,x))
			i-=s;
	}
	if (i<=n-1&&egal(a[i],x))
	{
	//	printf("(%lf %lf)=(%lf %lf)\n",a[i].fs,a[i].ss,x.fs,x.ss);
		return 1;
	}
	return 0;
	
}
int search()
{
	int i,j;
	int nr=0;
	FOR(i,0,n-1)
		FOR(j,i+1,n-1)
		{
			pair<DD,DD > rez=puncte(i,j);
			if (cauta(rez.fs)&&cauta(rez.ss))
			{
				nr++;
	//			printf("diagonala(%d %d)\n",i,j);
			}
		}
	return nr/2;
}
int main()
{
	FILE *in,*out;
	int i;
	double x,y;
	in=fopen("patrate3.in","r");
	out=fopen("patrate3.out","w");
	fscanf(in,"%d",&n);
	FOR(i,1,n)
	{
		fscanf(in,"%lf%lf",&x,&y);
		a.pb(mp(x,y));

	}
	sort(all(a));
	fprintf(out,"%d\n",search());
	fclose(in);
	fclose(out);	
	return 0;
}