use std::fs;
#[derive(Clone, Copy, Default)]
struct Point {
x: i32,
y: i32,
}
const LEAF_SIZE: usize = 8;
const RADIX_BITS: u32 = 11;
const RADIX_SIZE: usize = 1 << RADIX_BITS;
const RADIX_MASK: u32 = (1 << RADIX_BITS) - 1;
fn squared(value: u32) -> u64 {
let value = u64::from(value);
value * value
}
fn squared_distance(a: Point, b: Point) -> u64 {
squared(a.x.abs_diff(b.x)) + squared(a.y.abs_diff(b.y))
}
fn radix_key(p: Point) -> u32 {
(p.x as u32) ^ 0x8000_0000
}
fn radix_sort_by_x(points: &mut [Point], scratch: &mut [Point]) {
let count = points.len();
if count < 2 {
return;
}
let scratch = &mut scratch[..count];
let mut histogram = [[0u32; RADIX_SIZE]; 3];
for &p in points.iter() {
let key = radix_key(p);
histogram[0][(key & RADIX_MASK) as usize] += 1;
histogram[1][((key >> RADIX_BITS) & RADIX_MASK) as usize] += 1;
histogram[2][(key >> (2 * RADIX_BITS)) as usize] += 1;
}
let mut sorted_in_scratch = false;
for (pass, bucket) in histogram.iter_mut().enumerate() {
let shift = pass as u32 * RADIX_BITS;
let digit_of = |p: Point| ((radix_key(p) >> shift) & RADIX_MASK) as usize;
let (source, target): (&[Point], &mut [Point]) = if sorted_in_scratch {
(&*scratch, &mut *points)
} else {
(&*points, &mut *scratch)
};
if bucket[digit_of(source[0])] as usize == count {
continue; // every key has the same digit: nothing would move
}
let mut start = 0;
for size in bucket.iter_mut() {
let next_start = start + *size;
*size = start;
start = next_start;
}
for &p in source {
let slot = &mut bucket[digit_of(p)];
target[*slot as usize] = p;
*slot += 1;
}
sorted_in_scratch = !sorted_in_scratch;
}
if sorted_in_scratch {
points.copy_from_slice(scratch);
}
}
fn merge_by_x(points: &mut [Point], left_size: usize, scratch: &mut [Point]) {
let left_run = &mut scratch[..left_size];
left_run.copy_from_slice(&points[..left_size]);
let (mut left, mut right, mut output) = (0, left_size, 0);
while left < left_size && right < points.len() {
let (from_left, from_right) = (left_run[left], points[right]);
let take_left = from_left.x <= from_right.x;
points[output] = if take_left { from_left } else { from_right };
output += 1;
left += take_left as usize;
right += !take_left as usize;
}
points[output..output + left_size - left].copy_from_slice(&left_run[left..]);
}
fn sort_by_x(points: &mut [Point], scratch: &mut [Point]) {
let left_size = points.len() / 2;
{
let (left, right) = points.split_at_mut(left_size);
radix_sort_by_x(left, scratch);
radix_sort_by_x(right, scratch);
}
merge_by_x(points, left_size, scratch);
}
struct Band {
points: [Point; 8],
count: usize,
}
impl Band {
fn new() -> Band {
Band {
points: [Point::default(); 8],
count: 0,
}
}
fn add(&mut self, p: Point, dividing_x: i32, best: &mut u64) {
if squared(p.x.abs_diff(dividing_x)) >= *best {
return; // too far from the line
}
let oldest = self.count.saturating_sub(7);
for index in (oldest..self.count).rev() {
let q = self.points[index & 7];
if squared(p.y.abs_diff(q.y)) >= *best {
break;
}
*best = (*best).min(squared_distance(p, q));
}
self.points[self.count & 7] = p;
self.count += 1;
}
}
struct ClosestPair {
scratch: Vec<Point>,
best: u64,
}
impl ClosestPair {
fn solve(&mut self, points: &mut [Point]) {
let count = points.len();
if count <= LEAF_SIZE {
self.solve_leaf(points);
return;
}
let left_size = count / 2;
let dividing_x = points[left_size].x;
{
let (left, right) = points.split_at_mut(left_size);
self.solve(left);
self.solve(right);
}
let left_run = &mut self.scratch[..left_size];
left_run.copy_from_slice(&points[..left_size]);
let best = &mut self.best;
let mut band = Band::new();
let (mut left, mut right, mut output) = (0, left_size, 0);
while left < left_size && right < count {
let (from_left, from_right) = (left_run[left], points[right]);
let take_left = from_left.y <= from_right.y;
let p = if take_left { from_left } else { from_right };
left += take_left as usize;
right += !take_left as usize;
points[output] = p;
output += 1;
band.add(p, dividing_x, best);
}
while left < left_size {
let p = left_run[left];
left += 1;
points[output] = p;
output += 1;
band.add(p, dividing_x, best);
}
for &p in &points[right..] {
band.add(p, dividing_x, best);
}
}
fn solve_leaf(&mut self, points: &mut [Point]) {
for first in 0..points.len() {
for second in first + 1..points.len() {
if squared(points[second].x.abs_diff(points[first].x)) >= self.best {
break;
}
self.best = self.best.min(squared_distance(points[first], points[second]));
}
}
for current in 1..points.len() {
let key = points[current];
let mut position = current;
while position > 0 && points[position - 1].y > key.y {
points[position] = points[position - 1];
position -= 1;
}
points[position] = key;
}
}
}
fn main() {
let input = fs::read_to_string("cmap.in").expect("cannot read cmap.in");
let mut numbers = input
.split_ascii_whitespace()
.map(|token| token.parse::<i32>().unwrap());
let point_count = numbers.next().unwrap() as usize;
let mut points: Vec<Point> = (0..point_count)
.map(|_| Point { x: numbers.next().unwrap(), y: numbers.next().unwrap() })
.collect();
drop(input);
let mut scratch = vec![Point::default(); (point_count + 1) / 2];
sort_by_x(&mut points, &mut scratch);
let mut closest_pair = ClosestPair { scratch, best: u64::MAX };
closest_pair.solve(&mut points);
let distance = (closest_pair.best as f64).sqrt();
fs::write("cmap.out", format!("{:.6}\n", distance)).expect("cannot write the output file");
}