Cod sursa(job #3372827)

Utilizator paul_gabryelPaul Buda paul_gabryel Data 5 noiembrie 2026 08:54:31
Problema Cele mai apropiate puncte din plan Scor 100
Compilator c-64 Status done
Runda Arhiva educationala Marime 3.32 kb
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

typedef long long i64;
typedef unsigned long long u64;
typedef struct { i64 x, y; } Point;

static Point *points;
static Point *merge_buffer;
static Point *strip;

static u64 squared_distance(Point a, Point b) {
    u64 delta_x = (u64)llabs(a.x - b.x);
    u64 delta_y = (u64)llabs(a.y - b.y);
    return delta_x * delta_x + delta_y * delta_y;
}

static int compare_by_x(const void *left, const void *right) {
    const Point *a = left, *b = right;
    if (a->x != b->x) return a->x < b->x ? -1 : 1;
    return (a->y > b->y) - (a->y < b->y);
}

static u64 closest_pair(int begin, int end) {
    u64 best = ~0ULL;

    if (end - begin <= 3) {
        for (int first = begin; first < end; first++)
            for (int second = first + 1; second < end; second++) {
                u64 distance = squared_distance(points[first], points[second]);
                if (distance < best) best = distance;
            }
        for (int current = begin + 1; current < end; current++) { /* insertion sort by y */
            Point key = points[current];
            int position = current - 1;
            while (position >= begin && points[position].y > key.y) {
                points[position + 1] = points[position];
                position--;
            }
            points[position + 1] = key;
        }
        return best;
    }

    int middle = (begin + end) / 2;
    i64 dividing_x = points[middle].x;
    u64 best_left = closest_pair(begin, middle);
    u64 best_right = closest_pair(middle, end);
    best = best_left < best_right ? best_left : best_right;

    int left = begin, right = middle, output = begin;
    while (left < middle && right < end)
        merge_buffer[output++] = (points[left].y <= points[right].y) ? points[left++] : points[right++];
    while (left < middle) merge_buffer[output++] = points[left++];
    while (right < end) merge_buffer[output++] = points[right++];
    for (int index = begin; index < end; index++) points[index] = merge_buffer[index];

    int strip_size = 0;
    for (int index = begin; index < end; index++) {
        u64 distance_to_line = (u64)llabs(points[index].x - dividing_x);
        if (distance_to_line * distance_to_line < best) strip[strip_size++] = points[index];
    }

    for (int first = 0; first < strip_size; first++)
        for (int second = first + 1; second < strip_size && second <= first + 7; second++) {
            u64 vertical_gap = (u64)(strip[second].y - strip[first].y);
            if (vertical_gap * vertical_gap >= best) break;
            u64 distance = squared_distance(strip[first], strip[second]);
            if (distance < best) best = distance;
        }
    return best;
}

int main(void) {
    FILE *input = fopen("cmap.in", "r");
    FILE *output = fopen("cmap.out", "w");
    int point_count;
    if (fscanf(input, "%d", &point_count) != 1) return 1;

    points = malloc(point_count * sizeof(Point));
    merge_buffer = malloc(point_count * sizeof(Point));
    strip = malloc(point_count * sizeof(Point));
    for (int index = 0; index < point_count; index++)
        if (fscanf(input, "%lld %lld", &points[index].x, &points[index].y) != 2) return 1;

    qsort(points, point_count, sizeof(Point), compare_by_x);
    fprintf(output, "%.6f\n", sqrt((double)closest_pair(0, point_count)));

    fclose(input);
    fclose(output);
    return 0;
}