Cod sursa(job #2739063)

Utilizator MateiCatalinUrsache Matei MateiCatalin Data 6 aprilie 2021 19:12:43
Problema Patrate 3 Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 3.28 kb
#include <fstream>
#include <algorithm>
#include <math.h>
#include <iomanip>


using namespace std;

struct punct{double x,y;};
punct a[1001],b[1001];

bool cmp1(punct a,punct b)
{
    if(a.y==b.y)
        return a.x<b.x;
    return a.y<b.y;
}
bool cmp2(punct a,punct b)
{
    if(a.x==b.x)
        return a.y<b.y;
    return a.x<b.x;
}
int main()
{
    ifstream f("patrate3.in");
    ofstream g("patrate3.out");
    int n,i,j,rez=0,st,dr,mij,t;
    double x1,y1;
    f>>n;
    for(i=1;i<=n;i++)
    {
        f>>a[i].x>>a[i].y;
        b[i].x=a[i].x;
        b[i].y=a[i].y;
    }
    sort(a+1,a+n+1,cmp1);
    sort(b+1,b+n+1,cmp2);
    for(i=1;i<=n;i++)
    {
        for(j=i+1;j<=n;j++)
        {
            x1=a[j].y-a[i].y;
            if(a[i].x>a[j].x)
                x1=a[i].x+x1;
            else
                x1=a[i].x-x1;
            y1=a[i].x-a[j].x;
            if(y1<0)
                y1=-y1;
            y1=y1+a[i].y;
            st=1;
            dr=n;
            mij=(st+dr)/2;
            t=0;
            while(st<dr)
            {
                if(abs(b[mij].x-x1)<0.00001)
                {
                    t=mij;
                    while(b[t].y>y1)
                        t--;
                    while(b[t].y<y1)
                        t++;
                    st=dr;
                }
                else
                {
                    if(st==dr-1)
                    {
                        st=dr;
                    }
                    else
                    {
                        if(b[mij].x>x1)
                            dr=mij;
                        else
                            st=mij;
                    }
                    mij=(st+dr)/2;
                }
            }
            if(abs(b[t].x-x1)<0.00001 && abs(b[t].y-y1)<0.00001)
            {
                x1=a[j].y-a[i].y;
                if(a[i].x>a[j].x)
                    x1=a[j].x+x1;
                else
                    x1=a[j].x-x1;
                y1=a[i].x-a[j].x;
                if(y1<0)
                    y1=-y1;
                y1=y1+a[j].y;
                st=1;
                dr=n;
                mij=(st+dr)/2;
                t=0;
                while(st<dr)
                {
                    if(abs(b[mij].x-x1)<0.00001)
                    {
                        t=mij;
                        while(b[t].y>y1)
                            t--;
                        while(b[t].y<y1)
                            t++;
                        st=dr;
                    }
                    else
                    {
                        if(st==dr-1)
                        {
                            st=dr;
                        }
                        else
                        {
                            if(b[mij].x>x1)
                                dr=mij;
                            else
                                st=mij;
                        }
                        mij=(st+dr)/2;
                    }
                }
                if(abs(b[t].x-x1)<0.00001 && b[t].y-y1<0.00001)
                {
                    rez++;
                }
            }
        }
    }
    g<<rez/2;
    return 0;
}