Cod sursa(job #410568)

Utilizator cristiprgPrigoana Cristian cristiprg Data 4 martie 2010 14:47:24
Problema Patrate 3 Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.71 kb
#include<cstdio>
#include<algorithm>
using namespace std;
#include <vector>
#include <iostream>
#define eps 1e-5
#define DIM 1005
#define PI 3.14159265
double Mabs(double x)
{
	return x<0?-x:x;
}

struct point
{
	double x, y;
	point(){x = 0; y = 0;}
	point(double X, double Y){x = X; y = Y;}
	friend bool operator < (const point &a, const point &b)
	{
		if (a.x +eps < b.x || Mabs(a.x - b.x) <= eps && a.y + eps < b.y)
			return 1;
		return 0;
	}
	friend bool operator == (const point &a, const point &b)
	{
		if (Mabs(a.x - b.x) <= eps && Mabs(a.y - b.y) <= eps)	return 1;
		return 0;
	}
};

vector<point>P;
int n, sol;
void read()
{
	FILE *f = fopen("patrate3.in", "r");
	fscanf(f, "%d", &n);
	double x, y;
	for (int i = 1; i <= n; ++i)
		fscanf(f, "%lf%lf", &x, &y), P.push_back(point(x, y));
	fclose(f);
}


point cautat(point P0, point P1,int semn)
{
	return point(P0.x - semn * (P1.y - P0.y), P0.y + semn * (P1.x - P0.x));
}

void solve()
{
	bool gasit1, gasit2;
	for (int i = 0; i < n; ++i)
		for (int j = i + 1; j < n; ++j)
		{
			point P1 = cautat(P[i], P[j], -1);
			//printf ("se cauta 1 (%lf %lf)\n", P1.x, P1.y);
			gasit1 = binary_search(P.begin(), P.end(), P1);
			if (gasit1)
			{
				point P2 = cautat(P[j], P[i], 1);
			//	printf ("se cauta 2(%lf %lf)\n", P2.x, P2.y);
				gasit2 = binary_search(P.begin(), P.end(), P2);
				if (gasit2 && !(P1 == P2))
				{
				//	printf("(%lf %lf) (%lf %lf) (%lf %lf) (%lf %lf)\n", P[i].x, P[i].y, P[j].x, P[j].y, P1.x, P2.y, P2.x, P2.y);
					++sol;
				}
			}
		}
}

int main()
{
	read();
	sort(P.begin(), P.end());
	solve();
	FILE *f = fopen("patrate3.out", "w");
	fprintf(f, "%d\n", sol/2);
	fclose(f);
	return 0;
}