Cod sursa(job #3372834)

Utilizator paul_gabryelPaul Buda paul_gabryel Data 5 noiembrie 2026 10:00:45
Problema Cele mai apropiate puncte din plan Scor 100
Compilator c-64 Status done
Runda Arhiva educationala Marime 5.56 kb

#include <inttypes.h>
#include <math.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef int64_t i64;
typedef uint64_t u64;
typedef uint32_t u32;
typedef struct { int32_t x, y; } Point;

enum {
    LEAF_SIZE = 8,
    RADIX_BITS = 11,
    RADIX_SIZE = 1 << RADIX_BITS,
    RADIX_MASK = RADIX_SIZE - 1
};

static Point *scratch;
static u64 best = UINT64_MAX;

static u64 squared(i64 value) { return (u64)(value * value); }

static u64 squared_distance(Point a, Point b) {
    return squared((i64)a.x - b.x) + squared((i64)a.y - b.y);
}

static u32 radix_key(Point p) { return (u32)p.x ^ 0x80000000u; }

static void radix_sort_by_x(Point *points, int count) {
    static u32 histogram[3][RADIX_SIZE];
    if (count < 2) return;
    memset(histogram, 0, sizeof histogram);
    for (int index = 0; index < count; index++) {
        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, *target = scratch;
    for (int pass = 0; pass < 3; pass++) {
        int shift = pass * RADIX_BITS;
        u32 *bucket = histogram[pass];
        if (bucket[(radix_key(source[0]) >> shift) & RADIX_MASK] == (u32)count)
            continue;
        u32 start = 0;
        for (int digit = 0; digit < RADIX_SIZE; digit++) {
            u32 size = bucket[digit];
            bucket[digit] = start;
            start += size;
        }
        for (int index = 0; index < count; index++) {
            Point p = source[index];
            target[bucket[(radix_key(p) >> shift) & RADIX_MASK]++] = p;
        }
        Point *previous_source = source;
        source = target;
        target = previous_source;
    }
    if (source != points) memcpy(points, source, (size_t)count * sizeof(Point));
}

static void merge_by_x(Point *points, int left_size, int count) {
    memcpy(scratch, points, (size_t)left_size * sizeof(Point));
    int left = 0, right = left_size, output = 0;
    while (left < left_size && right < count) {
        Point from_left = scratch[left], from_right = points[right];
        int 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++] = scratch[left++];
}

static void sort_by_x(Point *points, int count) {
    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);
}

typedef struct {
    Point points[8];
    int count;
} Band;

static void add_to_band(Band *band, Point p, int32_t dividing_x) {
    if (squared((i64)p.x - dividing_x) >= best) return;
    int oldest = band->count > 7 ? band->count - 7 : 0;
    for (int index = band->count - 1; index >= oldest; index--) {
        Point q = band->points[index & 7];
        if (squared((i64)p.y - q.y) >= best) break;
        u64 distance = squared_distance(p, q);
        if (distance < best) best = distance;
    }
    band->points[band->count & 7] = p;
    band->count++;
}

static void solve_leaf(Point *points, int count) {
    for (int first = 0; first < count; first++)
        for (int second = first + 1; second < count; second++) {
            if (squared((i64)points[second].x - points[first].x) >= best)
                break;
            u64 distance = squared_distance(points[first], points[second]);
            if (distance < best) best = distance;
        }
    for (int current = 1; current < count; current++) {
        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;
    }
}


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

    memcpy(scratch, points, (size_t)left_size * sizeof(Point));
    Band band;
    band.count = 0;
    int left = 0, right = left_size, output = 0;
    while (left < left_size && right < count) {
        Point from_left = scratch[left], from_right = points[right];
        int take_left = from_left.y <= from_right.y;
        Point p = take_left ? from_left : from_right;
        left += take_left;
        right += !take_left;
        points[output++] = p;
        add_to_band(&band, p, dividing_x);
    }
    while (left < left_size) {
        Point p = scratch[left++];
        points[output++] = p;
        add_to_band(&band, p, dividing_x);
    }
    while (right < count) add_to_band(&band, points[right++], dividing_x); /* already in place */
}

int main(void) {
    FILE *input_file = fopen("cmap.in", "r");
    int point_count;
    if (fscanf(input_file, "%d", &point_count) != 1) return 1;
    Point *points = malloc((size_t)point_count * sizeof(Point));
    scratch = malloc((size_t)(point_count + 1) / 2 * sizeof(Point));
    for (int index = 0; index < point_count; index++)
        if (fscanf(input_file, "%" SCNd32 " %" SCNd32, &points[index].x, &points[index].y) != 2) return 1;
    fclose(input_file);

    sort_by_x(points, point_count);
    solve(points, point_count);

    FILE *output_file = fopen("cmap.out", "w");
    fprintf(output_file, "%.6f\n", sqrt((double)best));
    fclose(output_file);
    free(points);
    free(scratch);
    return 0;
}