Cod sursa(job #2483914)

Utilizator BAlexandruBorgovan Alexandru BAlexandru Data 30 octombrie 2019 15:46:59
Problema Triang Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 3.4 kb
#include <fstream>
#include <cmath>
#include <algorithm>

using namespace std;

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

struct punct
{
    double x,y;
};
punct v[1501];

int n,i,j,k;
int nr;
double X0,Y0, X1,Y1, X2,Y2, X3,Y3, X4,Y4;
double l,h, sinus, cosinus;

int st, dr, mij;
bool gasit;

bool cmp(punct a, punct b)
{
    return (a.x<b.x || (a.x==b.x && a.y<b.y));
}

double tf(double x)
{
    return int(x*10000)/10000.0;
}

int main()
{
    f>>n;
    for (i=1;i<=n;i++)
    {
        f>>v[i].x>>v[i].y;
        v[i].x=tf(v[i].x);
        v[i].y=tf(v[i].y);
    }

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

    for (i=1;i<n;i++)
        for (j=i+1;j<=n;j++)
        {
            X1=tf(v[i].x), Y1=tf(v[i].y);
            X2=tf(v[j].x), Y2=tf(v[j].y);

            X0=tf((X1+X2)/2), Y0=tf((Y1+Y2)/2);

            l=tf(sqrt((X1-X2)*(X1-X2)+(Y1-Y2)*(Y1-Y2)));
            h=tf(sqrt(3)/2*l);

            if (X1<=X2 && Y1<=Y2)
            {
                sinus=tf((Y2-Y1)/l);
                cosinus=tf((X2-X1)/l);

                X3=tf(X0-h*sinus);
                Y3=tf(Y0+h*cosinus);

                X4=tf(X0+h*sinus);
                Y4=tf(Y0-h*cosinus);

            }
            else if (X1<=X2 && Y1>=Y2)
            {
                sinus=tf((Y1-Y2)/l);
                cosinus=tf((X2-X1)/l);

                X3=tf(X0-h*sinus);
                Y3=tf(Y0-h*cosinus);

                X4=tf(X0+h*sinus);
                Y4=tf(Y0+h*cosinus);
            }
            else if (X1>=X2 && Y1>=Y2)
            {
                sinus=tf((Y1-Y2)/l);
                cosinus=tf((X1-X2)/l);

                X3=tf(X0-h*sinus);
                Y3=tf(Y0+h*cosinus);

                X4=tf(X0+h*sinus);
                Y4=tf(Y0-h*cosinus);
            }
            else if (X1>=X2 && Y1<=Y2)
            {
                sinus=tf((Y2-Y1)/l);
                cosinus=tf((X1-X2)/l);

                X3=tf(X0-h*sinus);
                Y3=tf(Y0-h*cosinus);

                X4=tf(X0+h*sinus);
                Y4=tf(Y0+h*cosinus);
            }

            st=1, dr=n, gasit=0;
            while (st<=dr && !gasit)
            {
                mij=(st+dr)/2;
                if (abs(v[mij].x-X3)<=0.001)
                {
                    if (abs(v[mij].y-Y3)<=0.001)
                    {
                        nr++;
                        gasit=1;
                    }
                    else if (v[mij].y<Y3)
                        st=mij+1;
                    else
                        dr=mij-1;
                }
                else if (v[mij].x<X3)
                    st=mij+1;
                else
                    dr=mij-1;
            }

            st=1, dr=n, gasit=0;
            while (st<=dr && !gasit)
            {
                mij=(st+dr)/2;
                if (abs(v[mij].x-X4)<=0.001)
                {
                    if (abs(v[mij].y-Y4)<=0.001)
                    {
                        nr++;
                        gasit=1;
                    }
                    else if (v[mij].y<Y4)
                        st=mij+1;
                    else
                        dr=mij-1;
                }
                else if (v[mij].x<X4)
                    st=mij+1;
                else
                    dr=mij-1;
            }
        }

    g<<nr/3;

    return 0;
}