Cod sursa(job #3372835)

Utilizator paul_gabryelPaul Buda paul_gabryel Data 5 noiembrie 2026 10:03:50
Problema Cele mai apropiate puncte din plan Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 6.46 kb
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <limits>
#include <memory>
#include <utility>

using i64 = std::int64_t;
using u64 = std::uint64_t;
using u32 = std::uint32_t;

struct Point { std::int32_t x, y; };

namespace {

constexpr int LEAF_SIZE = 8;
constexpr int RADIX_BITS = 11;
constexpr int RADIX_SIZE = 1 << RADIX_BITS;
constexpr u32 RADIX_MASK = RADIX_SIZE - 1;

inline u64 squared(i64 value) { return static_cast<u64>(value * value); }

inline u64 squared_distance(Point a, Point b) {
    return squared(static_cast<i64>(a.x) - b.x) + squared(static_cast<i64>(a.y) - b.y);
}

inline u32 radix_key(Point p) { return static_cast<u32>(p.x) ^ 0x80000000u; }

class Band {
public:
    void add(Point p, std::int32_t dividing_x, u64 &best) {
        if (squared(static_cast<i64>(p.x) - dividing_x) >= best) return;
        const int oldest = count_ > 7 ? count_ - 7 : 0;
        for (int index = count_ - 1; index >= oldest; --index) {
            const Point q = points_[index & 7];
            if (squared(static_cast<i64>(p.y) - q.y) >= best) break;
            const u64 distance = squared_distance(p, q);
            if (distance < best) best = distance;
        }
        points_[count_ & 7] = p;
        ++count_;
    }

private:
    Point points_[8];
    int count_ = 0;
};

class ClosestPair {
public:
    explicit ClosestPair(int point_count) : scratch_(new Point[(point_count + 1) / 2]) {}

    void sort_by_x(Point *points, int count) {
        const int left_size = count / 2;
        radix_sort_by_x(points, left_size);
        radix_sort_by_x(points + left_size, count - left_size);
        merge_by_x(points, left_size, count);
    }

    u64 smallest_squared_distance(Point *points, int count) {
        best_ = std::numeric_limits<u64>::max();
        solve(points, count);
        return best_;
    }

private:
    void radix_sort_by_x(Point *points, int count) {
        if (count < 2) return;
        u32 histogram[3][RADIX_SIZE] = {};
        for (int index = 0; index < count; ++index) {
            const u32 key = radix_key(points[index]);
            ++histogram[0][key & RADIX_MASK];
            ++histogram[1][(key >> RADIX_BITS) & RADIX_MASK];
            ++histogram[2][key >> (2 * RADIX_BITS)];
        }

        Point *source = points;
        Point *target = scratch_.get();
        for (int pass = 0; pass < 3; ++pass) {
            const int shift = pass * RADIX_BITS;
            const auto digit_of = [shift](Point p) { return (radix_key(p) >> shift) & RADIX_MASK; };
            u32 *bucket = histogram[pass];
            if (bucket[digit_of(source[0])] == static_cast<u32>(count)) continue;
            u32 start = 0;
            for (int digit = 0; digit < RADIX_SIZE; ++digit) {
                const u32 size = bucket[digit];
                bucket[digit] = start;
                start += size;
            }
            for (int index = 0; index < count; ++index) {
                target[bucket[digit_of(source[index])]++] = source[index];
            }
            std::swap(source, target);
        }
        if (source != points) std::memcpy(points, source, count * sizeof(Point));
    }

    void merge_by_x(Point *points, int left_size, int count) {
        Point *left_run = scratch_.get();
        std::memcpy(left_run, points, left_size * sizeof(Point));
        int left = 0, right = left_size, output = 0;
        while (left < left_size && right < count) {
            const Point from_left = left_run[left], from_right = points[right];
            const bool take_left = from_left.x <= from_right.x;
            points[output++] = take_left ? from_left : from_right;
            left += take_left;
            right += !take_left;
        }
        while (left < left_size) points[output++] = left_run[left++];
    }

    void solve_leaf(Point *points, int count) {
        for (int first = 0; first < count; ++first) {
            for (int second = first + 1; second < count; ++second) {
                if (squared(static_cast<i64>(points[second].x) - points[first].x) >= best_) {
                    break;
                }
                const u64 distance = squared_distance(points[first], points[second]);
                if (distance < best_) best_ = distance;
            }
        }
        for (int current = 1; current < count; ++current) {
            const Point key = points[current];
            int position = current - 1;
            while (position >= 0 && points[position].y > key.y) {
                points[position + 1] = points[position];
                --position;
            }
            points[position + 1] = key;
        }
    }

    void solve(Point *points, int count) {
        if (count <= LEAF_SIZE) {
            solve_leaf(points, count);
            return;
        }
        const int left_size = count / 2;
        const std::int32_t dividing_x = points[left_size].x;
        solve(points, left_size);
        solve(points + left_size, count - left_size);

        Point *left_run = scratch_.get();
        std::memcpy(left_run, points, left_size * sizeof(Point));
        Band band;
        int left = 0, right = left_size, output = 0;
        while (left < left_size && right < count) {
            const Point from_left = left_run[left], from_right = points[right];
            const bool take_left = from_left.y <= from_right.y;
            const Point p = take_left ? from_left : from_right;
            left += take_left;
            right += !take_left;
            points[output++] = p;
            band.add(p, dividing_x, best_);
        }
        while (left < left_size) {
            const Point p = left_run[left++];
            points[output++] = p;
            band.add(p, dividing_x, best_);
        }
        while (right < count) band.add(points[right++], dividing_x, best_);
    }

    std::unique_ptr<Point[]> scratch_;
    u64 best_ = std::numeric_limits<u64>::max();
};

}

int main() {
    std::ifstream input("cmap.in");
    int point_count = 0;
    input >> point_count;
    std::unique_ptr<Point[]> points(new Point[point_count]);  // not zero-filled: memory is used only when written
    for (int index = 0; index < point_count; ++index) input >> points[index].x >> points[index].y;

    ClosestPair closest_pair(point_count);
    closest_pair.sort_by_x(points.get(), point_count);
    const u64 best = closest_pair.smallest_squared_distance(points.get(), point_count);

    std::FILE *output = std::fopen("cmap.out", "w");
    std::fprintf(output, "%.6f\n", std::sqrt(static_cast<double>(best)));
    std::fclose(output);
}