See More
MAXimal
home
algo
bookz
forum
about
���������� ���� ��������� ����� ���������� ��������-�-��������
Page source on a
TeX
-like language:
\h1{ ���������� ���� ��������� ����� } \h2{ ���������� ������ } ���� $n$ ����� $p_i$ �� ���������, �������� ������ ������������ $(x_i,y_i)$. ��������� ����� ����� ��� ����� ��� �����, ���������� ����� �������� ����������: $$ \min_{\scriptstyle i,j=0 \ldots n-1, \atop \scriptstyle i \ne j} \rho (p_i,p_j). $$ ���������� �� ���� ������� ���������: $$ \rho(p_i,p_j) = \sqrt{ (x_i-x_j)^2 + (y_i-y_j)^2 }. $$ ����������� �������� --- ������� ���� ��� � ���������� ���������� ��� ������ --- �������� �� $O(n^2)$. ���� ����������� ��������, ���������� �� ����� $O(n \log n)$. ���� �������� ��� ��������� ���������� (Preparata) � 1975 �. ��������� � ����� ����� ��������, ��� � ������ ������ ������� ���� �������� �������������� ���������. \h2{ �������� } �������� �������� �� ����� ����� ���������� \bf{"��������-�-��������"}: �������� ��������� � ���� ����������� �������, ������� ��������� ��������� �����; ��� ����������� ������� ��������� ��� ��������� �������, �������� ���� ���������� �� ������ ��������, � ����� ��������� �����-�� �������� �� ����������� �������. �������� ����������� ����������� � ����������� �������, ����� ���� ����� ������������ ������� ������ � ���� ��������, � ������ ����� --- � ������ (� ���� ������ ����������� ������ �� ������ �� ��������� �������� ���������� ��� ����, �������, �� ������). �������� ���������, ��� ������, ����������� � ����������� ���������� ���� ������ �����������. ���� ����������� ������� ��������� ��������� �� $n$ �����, �� ������ ����������� ������ �������� �� �����, ��� $O(n)$, ����� ����������� ����� ��������� $T(n)$ ����� ���������� �� ���������: $$ T(n) = 2 T(n/2) + O(n). $$ �������� ����� ���������, ��� ��������, �������� $T(n) = O (n \log n)$. ����, ������� � ���������� ���������. ����� � ������� ������ � ����������� ���������� ������ �����������, ��������� ��������� ����� �� ��� ����� �������� �� $x$-�����������: ���������� �� �������� ��������� ������������ ������, ����������� ��������� ����� �� ��� ������������ �������� ���������� ��������. ����� ��������� ������ ���������� ��������� �������: ����������� ����� ���������� ��� ���� �����, �.�.: $$ p_i < p_j \Longleftrightarrow (x_i < x_j) \lor \Bigl( (x_i = x_j) \land (y_i < y_j) \Bigr). $$ ����� ������ ������� ����� ���������� ����� $p_m$ ($m = \lfloor n/2 \rfloor$), � ��� ����� �� �� � ���� $p_m$ ������ � ������ ��������, � ��� ����� ����� �� --- �� ������ ��������: $$ A_1 = \{ p_i\ |\ i = 0 \ldots m \}, $$ $$ A_2 = \{ p_i\ |\ i = m+1 \ldots n-1 \}. $$ ������, ���������� ���������� �� ������� �� �������� $A_1$ � $A_2$, �� ����� ������ $h_1$ � $h_2$ ��� ������ �� ���������. ������ ������ �� ���: $h = \min (h_1, h_2)$. ������ ��� ���� ���������� \bf{������ �����������}, �.�. ���������� ���������� ����� ���� �����, ���������� ����� �������� ������ $h$, ������ ���� ����� ����� � $A_1$, � ������ --- � $A_2$. ��������, ��� ��� ����� ���������� ������������� ������ �� �����, ������� ������� �� ������������ ������ ������� �� ����������, ������� $h$, �.�. ��������� $B$ ��������������� �� ���� ������ ����� �����: $$ B = \{ p_i\ |\ | x_i - x_m | < h \}. $$ ��� ������ ����� �� ��������� $B$ ���� ���������� ����� �����, ����������� � ��� �����, ��� $h$. ��������, ���������� ������������� ������ �� �����, ���������� $y$ ������� ���������� �� ����� ��� �� $h$. ����� ����, �� ����� ������ ������������� �� �����, � ������� $y$-���������� ������ $y$-���������� ������� �����. ����� �������, ��� ������ ����� $p_i$ ��������� ��������� ��������������� ����� $C(p_i)$ ��������� �������: $$ C(p_i) = \{ p_j\ |\ p_j \in B,\ \ y_i - h < y_j \le y_i \}. $$ ���� �� ����������� ����� ��������� $B$ �� $y$-����������, �� �������� $C(p_i)$ ����� ����� �����: ��� ��������� ����� ������ �� ����� $p_i$. ����, � ����� ������������ \bf{������ �����������} �������� ��������� �������: ��������� ��������� $B$, ������������� � ��� ����� �� $y$-����������, ����� ��� ������ ����� $p_i \in B$ ����������� ��� ����� $p_j \in C(p_i)$, � ������ ���� $(p_i,p_j)$ ��������� ���������� � �������� � ������� ��������� �����������. �� ������ ������, ��� ��-�������� ������������� ��������: �������, ��� ������� �������� $C(p_i)$ ����� ������� $n$, � ��������� ����������� ����� �� ���������. ������, ��� ��� �� �����������, ����� ��������, ��� ������ ������� �� �������� $C(p_i)$ ���� �������� $O(1)$, �.�. �� ����������� ��������� ����� ��������� ��� ����������� �� ����� �����. �������������� ����� ����� ��������� � ��������� �������. �������, ������� �������� �� ����������, ������� ������������� �������� �������� ����� ���: ������� ���������� �� ����� ($x$,$y$), � ����� ���������� ��������� ��������� $B$ �� $y$. �� ����� ����, �� ����� ���� ���������� ������ ����������� ������� ����� ���������� (����� �� �� �� �������� ������ $O(n)$ ��� ������ �����������, � ����� ����������� ��������� ���������� �� $O(n \log^2 n)$). �� ������ ���������� ���������� ����� --- ���������� ��������������, �� ������� ��������, ��������� ��� ����������: ���� ������ �������� ���� �������� �� ��������, ������� ��� ������� ������������� ��������� ���������� ������. �� ������ ����������� ���� �������, ��������� � �������������� �� ���������. ����, �������� \bf{���������� ��������} (merge sort), ������� ���� �������� �� �������� ��������-�-��������, ����� ������ �������� ��� ���������� � ���� ��������. ����� ��������, �������� �����-�� ��������� ����� (��� �� ������, ������������� �� ����� $(x,y)$) ���������� ��� �� ���������, �� ��������������� ��� �� ���������� $y$. ��� ����� ���������� ������ ��������� ������� (�� $O(n)$) ���� �����������, ������������ ������������ ��������. ��� ����� ��������� ��������������� �� $y$ ���������. \h2{ ������ ����������� } ����� ��������, ��� ������������� �������� ������������� ����������� �� $O(n \log n)$, ��� �������� �������� ��������� ����: $|C(p_i)| = O(1)$. ����, ����� �� ������������� �����-�� ����� $p_i$; ��������, ��� ��������� $C(p_i)$ --- ��� ��������� �����, $y$-���������� ������� ����� � ������� $[y_i-h; y_i]$, �, ����� ����, �� ���������� $x$ � ���� ����� $p_i$, � ��� ����� ��������� $C(p_i)$ ����� � ������ ������� $2h$. ����� �������, ��������������� ���� ����� $p_i$ � $C(p_i)$ ����� � �������������� ������� $2h \times h$. ���� ������ --- ������� ������������ ���������� �����, ������� ����� ������ � ���� �������������� $2h \times h$; ��� ����� �� ������ � ������������ ������ ��������� $C(p_i)$. ��� ���� ��� ������ ���� �� ��������, ��� ����� ����������� ������������� �����. ��������, ��� $h$ ���������� ��� ������� �� ���� ����������� ����������� ������� --- �� �������� $A_1$ � $A_2$, ������ $A_1$ �������� ����� ����� �� ����� ������� � �������� �� ���, $A_2$ --- ���������� ����� ����� ������� � ����� ������ �� ��. ��� ����� ���� ����� �� $A_1$, ����� ��� � �� $A_2$, ���������� �� ����� ��������� ������ $h$ --- ����� �� ��� �������� �������������� ������ ����������� �������. ��� ������ ������������� ���������� ����� � �������������� $2h \times h$ �������� ��� �� ��� �������� $h \times h$, � ������� �������� ������ ��� ����� $C(p_i) \cap A_1$, � �� ������� --- ��� ���������, �.�. $C(p_i) \cap A_2$. �� ���������� ���� ����������� �������, ��� � ������ �� ���� ��������� ���������� ����� ������ ����� ������� �� ����� $h$. �������, ��� � ������ �������� \bf{�� ����� ������} �����. ��������, ��� ����� ������� ��������� �������: �������� ������� �� 4 ����������� �� ��������� $h/2$. ����� � ������ �� ���� ������������ �� ����� ���� ������ ����� ����� (�.�. ���� ��������� ����� $h / \sqrt{2}$, ��� ������ $h$). �������������, �� ��� �������� �� ����� ���� ����� 4 �����. ����, �� ��������, ��� � �������������� $2h \times h$ �� ����� ���� ������ $4 \cdot 2 = 8$ �����, �, �������������, ������ ��������� $C(p_i)$ �� ����� ������������ $7$, ��� � ����������� ��������. \h2{ ���������� } ����� ��������� ������ ��� �������� ����� (� ���������� � ����� �����) � ��������� ���������, ����������� ��� ���� ����� ����������: \code struct pt { int x, y, id; }; inline bool cmp_x (const pt & a, const pt & b) { return a.x < b.x || a.x == b.x && a.y < b.y; } inline bool cmp_y (const pt & a, const pt & b) { return a.y < b.y; } pt a[MAXN]; \endcode ��� ������� ���������� �������� ����� ��������������� ������� $upd\_ans()$, ������� ����� ��������� ���������� ����� ����� ������� � ���������, �� ����� �� ��� �������� ������: \code double mindist; int ansa, ansb; inline void upd_ans (const pt & a, const pt & b) { double dist = sqrt ((a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y) + .0); if (dist < mindist) mindist = dist, ansa = a.id, ansb = b.id; } \endcode �������, ���������� ����� ��������. ��������������, ��� ����� � ������� ������ $a[]$ ��� ������������ �� $x$-����������. �������� ��������� ������ ��� ��������� $l$, $r$, ������� ���������, ��� ��� ������ ������ ����� ��� $a[l \ldots r]$. ���� ���������� ����� $r$ � $l$ ������� ����, �� �������� ���� ����������, � ��������� ����������� �������� ������ ��������� ���� � ����� ������������� ��������� �� $y$-����������. ��� ������� ���� �������� �����, ���������� �� ����������� �������, � ���� (������������� �� $y$-����������), �� ���������� ����������� ������� STL $merge()$, � ������ ��������������� ����� $t[]$ (���� �� ��� ����������� ������). (������������ $inplace\_merge()$ ���������������, �.�. ��� � ����� ������ �������� �� �� �������� �����). �������, ��������� $B$ �������� � ��� �� ������� $t$. \code void rec (int l, int r) { if (r - l <= 3) { for (int i=l; i<=r; ++i) for (int j=i+1; j<=r; ++j) upd_ans (a[i], a[j]); sort (a+l, a+r+1, &cmp_y); return; } int m = (l + r) >> 1; int midx = a[m].x; rec (l, m), rec (m+1, r); static pt t[MAXN]; merge (a+l, a+m+1, a+m+1, a+r+1, t, &cmp_y); copy (t, t+r-l+1, a+l); int tsz = 0; for (int i=l; i<=r; ++i) if (abs (a[i].x - midx) < mindist) { for (int j=tsz-1; j>=0 && a[i].y - t[j].y < mindist; --j) upd_ans (a[i], t[j]); t[tsz++] = a[i]; } } \endcode ������ ������, ���� ��� ���������� �����, �� �� ����� ������ �������� ����� ������ �� ���������� � ������� ���������, � ������� � $mindist$ ������� ������������ ����������. � �������� ��������� �������� �������� ������� ���: \code sort (a, a+n, &cmp_x); mindist = 1E20; rec (0, n-1); \endcode \h2{ ���������: ����� ������������ � ����������� ���������� } ��������� ���� �������� ��������� ���������� � �� ��� ������: ����� ��������� ��������� ����� ������� ��� ��������� ����� ���, ����� ����� �������� ���������� ����� ���� ���� ����������. �� ����, ��� ������� ���� ������ �������� ������� �������: �� ��������� ���� �� ��� ��������� ������������ ������, �������� ������� ���������� �� ����� ���������, �������� ������� $minper$ �� ��������� ����������, ������ ������� �������� $minper / 2$, � � ��� ���������� ��� ������������, ��������� �������� �����. (�������, ��� � ������������ � ���������� $\le minper$ ���������� ������� $\le minper/2$.) \h2{ ������ � online judges } ������ �����, ������� �������� � ������ ���� ��������� �����: \ul{ \li \href=http://uva.onlinejudge.org/index.php?option=onlinejudge&page=show_problem&problem=1186{ UVA 10245 \bf{"The Closest Pair Problem"} ~~~~~~ [���������: ������] } \li \href=http://www.spoj.pl/problems/CLOPPAIR/{ SPOJ #8725 CLOPPAIR \bf{"Closest Point Pair"} ~~~~~~ [���������: ������] } \li \href=http://codeforces.ru/contest/120/problem/J{ CODEFORCES ��������� ��������� ���������� �������� - 2011 \bf{"����������� �����"} ~~~~~~ [���������: �������] } \li \href=http://code.google.com/codejam/contest/311101/dashboard#s=a&a=1{ Google CodeJam 2009 Final \bf{"Min Perimeter"} ~~~~~~ [���������: �������] } \li \href=https://www.spoj.pl/problems/CLOSEST/{ SPOJ #7029 CLOSEST \bf{"Closest Triple"} ~~~~~~ [���������: �������] } }