Cod sursa(job #23137)

Utilizator sims_glAlexandru Simion sims_gl Data 28 februarie 2007 11:19:52
Problema Patrate 3 Scor 70
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.63 kb
#include <stdio.h>
#include <algorithm>
#include <math.h>

using namespace std;

#define nm 1024
#define eps 0.0001

struct point
{
	double x, y;
};

int n, sol;
point a[nm];

int cmp(point a, point b)
{
	return a.x < b.x || (fabs(a.x - b.x) <= eps && a.y < b.y);
}

int exista(point p)
{
	int i, tmp = 1024;

    for (i = 0; tmp; tmp >>= 1)
    	if (i + tmp <= n && (cmp(a[i + tmp], p) || (fabs(a[i + tmp].x - p.x) <= eps && fabs(a[i + tmp].y - p.y) <= eps)))
        	i += tmp;

    if (fabs(a[i].x - p.x) <= eps && fabs(a[i].y - p.y) <= eps)
    	return 1;
    else
    	return 0;
}

int main()
{
	int i, j;
    point mid, d, p1, p2;

	freopen("patrate3.in", "r", stdin);
    freopen("patrate3.out", "w", stdout);

    scanf("%d", &n);

    for (i = 1; i <= n; ++i)
    	scanf("%lf%lf", &a[i].x, &a[i].y);

    sort(a + 1, a + n + 1, cmp);

    for (i = 1; i <= n; ++i)
    	for (j = i + 1; j <= n; ++j)
        {
        	mid.x = (a[i].x + a[j].x) / 2;
            mid.y = (a[i].y + a[j].y) / 2;
            d.x = fabs(mid.x - a[i].x);
            d.y = fabs(mid.y - a[i].y);

            if (a[i].y < a[j].y)
            {
            	p1.x = mid.x + d.y;
                p1.y = mid.y - d.x;

                p2.x = mid.x - d.y;
                p2.y = mid.y + d.x;
            }
            else
            {
            	p1.x = mid.x - d.y;
                p1.y = mid.y - d.x;

                p2.x = mid.x + d.y;
                p2.y = mid.y + d.x;
            }

            if (exista(p1) && exista(p2))
            	++sol;
        }

	printf("%d\n", sol / 2);

	return 0;
}