Cod sursa(job #2312611)

Utilizator miruna1224Floroiu Miruna miruna1224 Data 5 ianuarie 2019 05:10:31
Problema Patrate 3 Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.7 kb
#include <iostream>
#include <fstream>
#include <cmath>
#include <algorithm>

using namespace std;

struct Punct
{
    double x, y;
};

const int N = 1001;
Punct *v;
int n;

int Cautare ( int st, int dr, Punct pct )
{
    int m;
    while(st <= dr)
    {
        m = ( st + dr ) / 2;
        if(abs(pct.x - v[m].x) < 0.00001 && abs(pct.y - v[m].y) < 0.00001)
            return 1;
        if (abs(pct.x - v[m].x) < 0.00001)
        {
            if(pct.y < v[m].y  )
                dr = m - 1;
            else
                st = m + 1;
        }
       else
        {
            if( pct.x < v[m].x)
                dr = m - 1;
            else
                st = m + 1;
        }
    }
    return 0;
}

int Numar ()
{
    int i, j, cnt = 0;

    for( i = 1; i < n; i++ )
        for(j = i + 1 ; j <= n; j++ )
        {
            Punct A, B, m;
            m.x = (v[i].x + v[j].x) / 2;
            m.y = (v[i].y + v[j].y) / 2;
            A.x = m.x - v[j].y + m.y;
            A.y = m.y + v[j].x - m.x;
            B.x = m.x + v[j].y - m.y;
            B.y = m.y - v[j].x + m.x;
            if(Cautare(1, n, A) && Cautare(1, n, B))
                ++cnt;
        }

    return cnt/2;
}

bool cmp ( Punct A, Punct B)
{
    if ( A.x < B.x || (A.x == B.x && A.y <= B.y))
        return true;
    return false;
}

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

    int i;

    in >> n;
    v = new Punct [n + 1];
    for ( i = 1; i <= n; i++ )
        in >> v[i].x >> v[i].y;

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

    out << Numar ();

    delete ( v );

    in.close();
    out.close();

    return 0;
}