Cod sursa(job #3331597)

Utilizator BuzdiBuzdugan Rares Andrei Buzdi Data 29 decembrie 2025 12:54:01
Problema Patrate 3 Scor 45
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 3.68 kb
#include <bits/stdc++.h>

#define ll long long
#define ld long double

using namespace std;

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

const int NMAX = 1000;
const ld INF = 1e18;
const ld EPS = 1e-5;

struct Simon {
    ld slope;
    ll dist;
    int mid_x2, mid_y2;
    bool operator < (const Simon& other) const {
        if(slope - other.slope < -EPS) {
            return true;
        }
        else if(abs(slope - other.slope <= EPS) && dist < other.dist) {
            return true;
        }
        if(abs(slope - other.slope <= EPS) && dist == other.dist && mid_x2 < other.mid_x2) {
            return true;
        }
        if(abs(slope - other.slope <= EPS) && dist == other.dist && mid_x2 == other.mid_x2 && mid_y2 < other.mid_y2) {
            return true;
        }
        return false;
    }

    bool operator == (const Simon& other) const {
        return !(*this < other) && !(other < *this);
    }
};

int n;
ll answer;
int x[NMAX + 1], y[NMAX + 1];
vector<Simon> v;

ld get_slope(int x1, int y1, int x2, int y2) {
    if(x2 - x1 == 0) {
        return INF;
    }
    return (ld)(y2 - y1) / (ld)(x2 - x1);
}

ll get_distance(int x1, int y1, int x2, int y2) {
    return (ll)(x2 - x1) * (x2 - x1) + (ll)(y2 - y1) * (y2 - y1);
}

bool is_equal(pair<ld, ll> a, pair<ld, ll> b) {
    if(abs(a.first - b.first) <= EPS && a.second == b.second) {
        return true;
    }
    return false;
}

int main()
{
    fin >> n;
    for(int i = 1; i <= n; i++) {
        ld xx, yy;
        fin >> xx >> yy;
        x[i] = round(xx * 10000);
        y[i] = round(yy * 10000);
    }

    for(int i = 1; i <= n; i++) {
        for(int j = i + 1; j <= n; j++) {
            ld slope = get_slope(x[i], y[i], x[j], y[j]);
            assert(slope <= INF);
            ll dist = get_distance(x[i], y[i], x[j], y[j]);
            int mid_x2 = (x[i] + x[j]);
            int mid_y2 = (y[i] + y[j]);
            v.push_back({slope, dist, mid_x2, mid_y2});
        }
    }

    sort(v.begin(), v.end());
//    for(auto x : v) {
//        fout << x.slope << ' ' << x.dist << ' ' << x.mid_x2 << ' ' << x.mid_y2 << '\n';
//    }
//    fout << '\n';
    for(int i = 1; i <= n; i++) {
        for(int j = i + 1; j <= n; j++) {
            ld slope = get_slope(x[i], y[i], x[j], y[j]);
            ll dist = get_distance(x[i], y[i], x[j], y[j]);
            int mid_x2 = (x[i] + x[j]);
            int mid_y2 = (y[i] + y[j]);
            ld need_slope;
            if(slope == INF) {
                need_slope = 0;
            }
            else {
                need_slope = -1 / slope;
            }

            Simon need = {need_slope, dist, mid_x2, mid_y2};
//            for(Simon ceva : v) {
//                if(ceva == need) {
//                    answer++;
//                    break;
//                }
//            }

            auto itl = lower_bound(v.begin(), v.end(), need);
            if(itl != v.end() && *itl == need) {
                answer++;
            }
//            fout << itr - itl << '\n';
//            if(itr - itl > 0) {
//                fout << i << ' ' << j << '\n';
//            }
//            if(i == 1 && j == 9) {
//                fout << slope << ' ' << need_slope << ' ' << dist << ' ' << mid_x2 << ' ' << mid_y2 << '\n';
//            }
//            if(i == 4 && j == 6) {
//                fout << slope << ' ' << need_slope << ' ' << dist << ' ' << mid_x2 << ' ' << mid_y2 << '\n';
//            }
//            if(*itl == make_pair(need_slope, dist)) {
//                answer += itr - itl;
//            }
        }
    }
    fout << answer / 2 << '\n';
    return 0;
}