Cod sursa(job #1198435)

Utilizator AlexandruValeanuAlexandru Valeanu AlexandruValeanu Data 15 iunie 2014 18:04:57
Problema Patrate 3 Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.88 kb
#include <iostream>
#include <fstream>
#include <algorithm>

using namespace std;

const int Nmax = 1000 + 2;
const double EPS = 1e-6;

class Point
{
public:

    double x, y;

    Point( const double _x = 0, const double _y = 0 )
    {
        x = _x;
        y = _y;
    }

    bool operator < ( const Point &P ) const
    {
        if ( abs( x - P.x ) <= EPS )
                return y < P.y;
        else
                return x < P.x;
    }

    bool operator == ( const Point &P ) const
    {
        return ( abs( x - P.x ) < EPS && abs( y - P.y ) < EPS );
    }
};

int N;
Point P[Nmax];

bool find( Point A )
{
    int st = 1, dr = N, m;

    while ( st <= dr )
    {
        m = ( st + dr ) / 2;

        if ( A == P[m] )
                return true;

        if ( A < P[m] )
                dr = m - 1;
        else
                st = m + 1;
    }

    return false;
}

int main()
{
    ifstream in("patrate3.in");
    ofstream out("patrate3.out");

    in >> N;

    for ( int i = 1; i <= N; ++i )
    {
        in >> P[i].x >> P[i].y;
    }

    sort( P + 1, P + N + 1 );

    int sol = 0;

    for ( int i = 1; i < N; ++i )
    {
        for ( int j = i + 1; j <= N; ++j )
        {
            double mijx = ( P[i].x + P[j].x ) / 2.0;
            double mijy = ( P[i].y + P[j].y ) / 2.0;

            double dx = abs( mijx - P[i].x );
            double dy = abs( mijy - P[i].y );

            Point A, B;

            if ( P[i].y < P[j].y )
            {
                A.x = mijx + dy;
                A.y = mijy - dx;
                B.x = mijx - dy;
                B.y = mijy + dx;
            }
            else
            {
                A.x = mijx - dy;
                A.y = mijy - dx;
                B.x = mijx + dy;
                B.y = mijy + dx;
            }

            if ( find( A ) && find( B ) )
                    sol++;
        }
    }

    out << sol / 2 << "\n";

    return 0;
}