Cod sursa(job #2625492)

Utilizator anacomoAna-Maria Comorasu anacomo Data 5 iunie 2020 23:51:21
Problema Patrate 3 Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.27 kb
#include <bits/stdc++.h>
using namespace std;
ifstream fin("patrate3.in");
ofstream fout("patrate3.out");

int n, i, j, nr, dx, dy;
struct punct
{
    int x, y;
} v[1005], mij;

bool cmp(punct a, punct b)
{
    if (a.x == b.x)
        return a.y <= b.y;
    return a.x < b.x;
}
int cautareBinara(int a, int b)
{
    int st = 1, dr = n, mij;
    while (st <= dr)
    {
        mij = (st + dr) / 2;
        if (v[mij].x == a && v[mij].y == b)
        {
            return 1;
        }
        if ((a == v[mij].x && v[mij].y < b) || v[mij].x < a)
        {
            st = mij + 1;
        }
        else
        {
            dr = mij - 1;
        }
    }
    return 0;
}
int main()
{
    fin >> n;
    double vx, vy;
    for (i = 1; i <= n; i++)
    {
        fin >> vx >> vy;
        v[i].x = round(vx * 10000);
        v[i].y = round(vy * 10000);
    }

    sort(v + 1, v + n + 1, cmp);
    for (i = 1; i < n; i++)
    {
        for (j = i + 1; j <= n; j++)
        {
            mij.x = (v[i].x + v[j].x) / 2;
            mij.y = (v[i].y + v[j].y) / 2;
            dx = mij.x - v[i].x;
            dy = mij.y - v[i].y;
            if (cautareBinara(mij.x - dy, mij.y + dx) == 1 && cautareBinara(mij.x + dy, mij.y - dx) == 1)
                nr++;
        }
    }
    fout << nr / 2;
    return 0;
}