Cod sursa(job #2755539)

Utilizator Theo_FurtunaTheodor-Florentin Furtuna Theo_Furtuna Data 27 mai 2021 16:39:41
Problema Patrate 3 Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.56 kb
#include <bits/stdc++.h>
using namespace std;
ifstream in("patrate3.in");
ofstream out("patrate3.out");
#define eps 0.0001
struct punct
{
	float x, y;
};
int n, sol;
punct a[1024];
int cmp(punct a, punct b)
{
	return a.x < b.x - eps || (fabs(a.x - b.x) <= eps && a.y < b.y - eps);
}
int exista(punct p)
{
	int i, tmp = 1024;
    for (i = 0; tmp; tmp >>= 1)
    	if (i + tmp <= n && (a[i + tmp].x < p.x - eps || (fabs(a[i + tmp].x - p.x) <= eps && 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;
    punct mid, d, p1, p2;
    in >> n;
    for (i = 1; i <= n; i++)
    	in >> 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;
        }
    sol = sol / 2;
	out << sol;
    in.close();
    out.close();
    return 0;
}