Cod sursa(job #1984842)

Utilizator Y.MalmsteenB.P.M. Y.Malmsteen Data 26 mai 2017 11:07:50
Problema Patrate 3 Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.59 kb
#include <iostream>
#include <fstream>
#include <algorithm>
#include <cmath>

using namespace std;

const double EPS = 1e-5;

struct punct
{
    double x, y;
};

int N;
punct p[1001], v[1001];

ifstream f("patrate3.in");
ofstream g("patrate3.out");

int egal(double a, double b)
{
    double d = a - b;
    if(d < -EPS)return -1;
    if(d > +EPS)return 1;
    return 0;
}

int egal(const punct &A, const punct &B)
{
    int ex = egal(A.x, B.x);
    int ey = egal(A.y, B.y);
    if(ex == 0) return ey;
    return ex;
}

bool cmp(const punct &A, const punct &B)
{
    return egal(A, B) <= 0;
}

bool cautbin(punct P)
{
    int st = 1, dr = N;
    while(st <= dr)
    {
        int m = (st + dr) / 2;
        int e = egal(p[m], P);
        if(e == 0)
            return 1;
        if(e == 1)
            dr = m - 1;
        else
            st = m + 1;
    }
    return 0;
}

void solve()
{
    int nrp = 0;
    for(int i = 1; i < N; i++)
        for(int j = i + 1; j <= N; j++) //punctele p[i] si p[j] formeaza o diagonala
        {
            punct M, A, B;
            M.x = (p[i].x + p[j].x) / 2;
            M.y = (p[i].y + p[j].y) / 2;
            A.x = M.x - p[j].y + M.y;
            A.y = M.y + p[j].x - M.x;
            B.x = M.x + p[j].y - M.y;
            B.y = M.y - p[j].x + M.x ;
            if(cautbin(A) && cautbin(B))
                nrp++;
        }
    g << nrp / 2;
}

int main()
{
    f >> N;
    for(int i = 1; i <= N; i++)
        f >> p[i].x >> p[i].y;
    sort(p + 1, p + N + 1, cmp);
    solve();
    return 0;
}