Cod sursa(job #3266232)

Utilizator bogdan1479Luca Bogdan Alexandru bogdan1479 Data 6 ianuarie 2025 17:59:32
Problema Triang Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.75 kb
#include <fstream>
#include <algorithm>
#include <cmath>

using namespace std;

const int NMAX = 1501;

ifstream fin("triang.in");
ofstream fout("triang.out");

struct pct
{
    double x, y;
} p[NMAX];

int n;
long long sol;

bool cmp(const pct& a, const pct& b)
{
    if (a.x == b.x) return a.y < b.y;
    return a.x < b.x;
}

void rotunjire(pct& a)
{
    a.x = (int)(a.x * 1000.0) / 1000.0;
    a.y = (int)(a.y * 1000.0) / 1000.0;
}

double dis(const pct& a, const pct& b)
{
    return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
}

bool cb(const pct& a)
{
    int st = 1, dr = n, mij;
    while (st <= dr)
    {
        int mij = (st + dr) / 2;
        if(p[mij].x == a.x)
            if(p[mij].y == a.y)
                return 1;
            else if(p[mij].y < a.y)
                st = mij + 1;
            else
                dr = mij - 1;
        else if(p[mij].x < a.x)
            st = mij + 1;
        else
            dr = mij - 1;
    }
    return 0;
}

int main()
{
    pct crt, crt2;
    fin >> n;
    for(int i = 1; i <= n; ++i)
    {
        fin >> p[i].x >> p[i].y;
        rotunjire(p[i]);
    }
    sort (p + 1, p + n + 1, cmp);
    for(int i = 1; i < n; ++i)
        for(int j = i + 1; j <= n; ++j)
        {
            crt = {(p[i].x + p[j].x) / 2, (p[i].y + p[j].y) / 2}, crt2 = crt;
            double l = dis(p[i], p[j]), dx = (p[j].y - p[i].y) / l, dy = (p[i].x - p[j].x) / l, coef = (sqrt(3) / 2) * l;
            crt.x += dx * coef, crt.y += dy * coef;
            crt2.x -= dx * coef, crt2.y -= dy * coef;
            rotunjire(crt), rotunjire(crt2);
            if(cb(crt)) ++sol;
            if(cb(crt2)) ++sol;
        }
    fout << sol / 3;
    return 0;
}