Cod sursa(job #1065483)

Utilizator mirceadinoMircea Popoveniuc mirceadino Data 23 decembrie 2013 13:34:17
Problema Patrate 3 Scor 70
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.11 kb
#include<cstdio>
#include<cmath>
#include<algorithm>

using namespace std;

struct Point
{
    double x,y;
    bool operator()(Point A,Point B)
    {
        if(A.x==B.x) return (A.y<B.y);
        return (A.x<B.x);
    }
};

int N,sol;
Point P[1005],A,B;

inline double dist(Point A,Point B)
{
    return sqrt((A.x-B.x)*(A.x-B.x) + (A.y-B.y)*(A.y-B.y));
}

void determine(Point A,Point &B,Point C,Point &D)
{
    double mAC,mBD,nBD,d,a,b,c;
    Point M;
    M.x=(A.x+C.x)/2;
    M.y=(A.y+C.y)/2;
    if(A.x != C.x && A.y != C.y)
    {
        mAC=(A.y-C.y)/(A.x-C.x);
        mBD=-1/mAC;
        a=mAC;
        b=-1;
        c=A.y-mAC*A.x;
        d=dist(A,M)*sqrt(a*a+b*b);
        nBD=M.y-mBD*M.x;
        B.x=(d-b*nBD-c)/(a+b*mBD);
        B.y=mBD*B.x+nBD;
        D.x=(-d-b*nBD-c)/(a+b*mBD);
        D.y=mBD*D.x+nBD;
        return;
    }
    if(A.x == C.x)
    {
        d=dist(A,M);
        B.x=M.x-d;
        B.y=M.y;
        D.x=M.x+d;
        D.y=M.y;
        return;
    }
    if(A.y == C.y)
    {
        d=dist(A,M);
        B.x=M.x;
        B.y=M.y-d;
        D.x=M.x;
        D.y=M.y+d;
        return;
    }
}

inline bool equals(double A,double B)
{
    return ((A-B)<1e-4 && (B-A)<1e-4);
}

bool binary_s(Point A)
{
    int st=1,dr=N,md;
    for(;st<=dr;)
    {
        md=(st+dr)/2;
        if(equals(A.x,P[md].x) && equals(A.y,P[md].y)) return 1;
        if((A.x) < (P[md].x)) {dr=md-1; continue;}
        if((A.x) > (P[md].x)) {st=md+1; continue;}
        if(equals(A.x,P[md].x) && (A.y) < (P[md].y)) {dr=md-1; continue;}
        if(equals(A.x,P[md].x) && (A.y) > (P[md].y)) {st=md+1; continue;}

    }
    return 0;
}

int main()
{
    int i,j;
    freopen("patrate3.in","r",stdin);
    freopen("patrate3.out","w",stdout);
    scanf("%d",&N);
    for(i=1;i<=N;i++)
        scanf("%lf%lf",&P[i].x,&P[i].y);
    sort(P+1,P+N+1,Point());
    for(i=1;i<=N;i++)
        for(j=i+1;j<=N;j++)
        {
            determine(P[i],A,P[j],B);
            if(binary_s(A) && binary_s(B)) sol++;
        }
    printf("%d\n",sol/2);
    return 0;
}