Cod sursa(job #2746797)

Utilizator icnsrNicula Ionut icnsr Data 28 aprilie 2021 15:23:31
Problema Patrate 3 Scor 50
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.47 kb
#include <fstream>
#include <vector>
#include <unordered_map>
#include <string>

using i64 = std::int64_t;

struct Point
{
        static inline i64 dist(const Point& p1, const Point& p2)
        {
                const i64 d1 = p1.x - p2.x;
                const i64 d2 = p1.y - p2.y;
                const i64 dist = d1 * d1 + d2 * d2;

                return dist;
        }

        i64 x, y;
};

struct Segment
{
        Point p1, p2;
};

inline bool same_dist(const Point& p, const Point& p1, const Point& p2)
{
        return Point::dist(p, p1) == Point::dist(p, p2);
}

bool form_square(const Segment& s1, const Segment& s2)
{
        return same_dist(s1.p1, s2.p1, s2.p2) && same_dist(s1.p2, s2.p1, s2.p2) &&
               same_dist(s2.p1, s1.p1, s1.p2) && same_dist(s2.p2, s1.p1, s1.p1);
}

int main()
{
        std::ifstream fin("patrate3.in");
        std::ofstream fout("patrate3.out");

        unsigned N;
        fin >> N;

        std::vector<Point> points;
        {
                std::string x, y;
                x.reserve(10);
                y.reserve(10);
                for(unsigned i = 0; i < N; ++i)
                {
                        std::string x;
                        std::string y;
                        fin >> x >> y;

                        x.erase(x.size() - 5, 1);
                        y.erase(y.size() - 5, 1);

                        points.push_back({std::stoi(x), std::stoi(y)});
                }
        }

        std::unordered_map<int, std::vector<Segment>> table;
        for(unsigned i = 0; i < N - 1; ++i)
        {
                for(unsigned j = i + 1; j < N; ++j)
                {
                        // fmt::print(stderr, "err\n");
                        const i64 dist = Point::dist(points[i], points[j]);
                        table[dist].push_back({points[i], points[j]});
                }
        }

        unsigned res = 0;
        for(const auto& dist_bucket : table)
        {
                const auto& current_segments = dist_bucket.second;

                if(current_segments.size() < 2)
                {
                        continue;
                }

                for(unsigned i = 0; i < current_segments.size() - 1; ++i)
                {
                        for(unsigned j = i + 1; j < current_segments.size(); ++j)
                        {
                                res += form_square(current_segments[i], current_segments[j]);
                        }
                }
        }
        fout << res << '\n';
}