Cod sursa(job #799396)

Utilizator cbanu96Banu Cristian cbanu96 Data 18 octombrie 2012 21:39:07
Problema Patrate 3 Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.48 kb
#include <fstream>
#include <cmath>
#include <algorithm>


#define EPS 0.000001
#define NMAX 10002

using namespace std;
struct point
{
    double x;
    double y;
};
int n,i,j;
point V[NMAX];
inline int cmp ( point p1, point p2 )
{
    if(fabs(p1.x - p2.x) < EPS)
        return (p1.y < p2.y);
    return (p1.x < p2.x);
}

inline int caut_bin(point A)
{
    int st, end, mid, i;
    st = 1; end = n;
    while ( st <= end )
    {
        mid = (st+end)/2;
        if ( fabs(A.x - V[mid].x) < EPS && fabs(A.y - V[mid].y) < EPS )
            return 1;

        if ( cmp(V[mid], A) )
            st = mid + 1;
        else
            end = mid - 1;
    }

    return 0;
}



int main()
{
    point A;
    int nr = 0;
    point p1,p2,p3,p4;
    ifstream f("patrate3.in");
    f >> n;
    for (i = 1; i <= n; ++i)
        f >> V[i].x >> V[i].y;
    f.close();
    sort(V+1, V+n+1, cmp);
    for (i = 1; i < n; ++i)
    {
        for(j = i + 1; j <= n; ++j)
        {
            p1 = V[i]; p2 = V[j];
            p3.x = p1.x + p1.y - p2.y;
            p3.y = p1.y + p2.x - p1.x;
            p4.x = p2.x + p1.y - p2.y;
            p4.y = p2.y + p2.x - p1.x;

            A = p3;
            if(caut_bin(A))
            {
                A = p4;
                if (caut_bin(A))
                    ++nr;
            }
        }
    }

    nr /= 2;

    ofstream g("patrate3.out");
    g << nr << '\n';
    g.close();

    return 0;
}