Closest Pair
Time limit1.5sMemory limit512 MB
Both point sets lie on horizontal lines; find the Manhattan distance of the closest P-Q pair and count how many distinct pairs achieve it.
- Level
Medium6 of 10
- Topics
- Sorting, Two pointers, Array
- Solved
- No attempts yet
Problem
You are given two finite sets of points in the plane, and . A closest pair of and is a pair with and whose distance is the smallest among all pairs with and .
In this problem the distance between two points and is defined as follows.
Here and are the and coordinates of point , and and are the and coordinates of point . So a pair with and is a closest pair of and if and only if
Given and , write a program that computes the distance of a closest pair and the number of distinct closest pairs. Two pairs count as distinct when the point taken from differs or the point taken from differs.
The input points satisfy these two properties.
- There are integers and such that every point of lies on the line and every point of lies on the line .
- No two points of have the same coordinates, and no two points of have the same coordinates.
Input
Your program reads from standard input. The input consists of four lines.
The first line contains two integers and (, ), where is the number of points in and is the number of points in .
The second line contains two integers and () in this order, separated by a single space.
The third line contains distinct integers between and , separated by single spaces. They are the coordinates of the points in , whose coordinates all equal .
The fourth line contains distinct integers between and , separated by single spaces. They are the coordinates of the points in , whose coordinates all equal .
Output
Your program writes to standard output. Print exactly one line holding two integers separated by a single space: the distance of a closest pair of and , then the number of closest pairs of and .