[BOJ 23205] What’s Our Vector, Victor?
[BOJ 23205] What’s Our Vector, Victor?
Task
For $\b{x}=(x_1,\ldots,x_D)\in\mathbb{R}^D$, define $\abs{\b{x}}:=\sqrt{\sum_{j=1}^{D}x_j^2}$.
$N$ vectors $\b{x}_1,\ldots,\b{x}_N\in\mathbb{R}^D$ are given, along with $N$ nonnegative real numbers $e_1,\ldots,e_N \in \mathbb{R}_{\geq 0}$. Here, $e_i$ is the Euclidean distance from $\b{x}_i$ to a deleted vector. Define the set of all vectors consistent with the training results by
\[ \mathcal{S} := \Set{ \b{y}\in\mathbb{R}^D | \abs{\b{y}-\b{x}_i}=e_i \quad (1\leq i\leq N) } \]
The goal is to find any vector $\b{y}\in\mathcal{S}$.
Solution
For $1\leq k\leq N$, let
\[ \mathcal{S}_k := \Set{ \b{y}\in\mathbb{R}^D | \abs{\b{y}-\b{x}_i}=e_i \quad (1\leq i\leq k) } \]
We maintain an orthonormal set $C=\Set{\b{u}_1,\ldots,\b{u}_m}$, a center $\b{c}$, and a radius $r$. Writing $W:=\operatorname{span}(C)^\perp$, the invariant is
\[ \mathcal{S}_k = \Set{ \b{c}+\b{z} | \b{z}\in W,\; \abs{\b{z}}=r } \]
For $k=1$, set $\b{c}:=\b{x}_1$, $r:=e_1$, and $C:=\emptyset$. Then $W=\mathbb{R}^D$, so the invariant holds. Suppose the invariant holds for $\mathcal{S}_k$. Let $\b{a}:=\b{x}_{k+1}$ and $R:=e_{k+1}$. Decompose $\b{d}:=\b{c}-\b{a}$ into its components in $W$ and $W^\perp$:
\[ \begin{align*} \b{p} &:= \b{d} - \sum_{j=1}^{m} (\b{d}\cdot\b{u}_j)\b{u}_j \in W, \nl \b{q} &:= \b{d}-\b{p} \in W^\perp \end{align*} \]
The projection of $\b{a}$ onto the affine subspace $\b{c}+W$ is $\b{a}^\prime:=\b{c}-\b{p}$. Hence, for every $\b{y}\in\b{c}+W$,
\[ \abs{\b{y}-\b{a}}^2 = \abs{\b{y}-\b{a}^\prime}^2 + \abs{\b{q}}^2 \]
Therefore, on $\b{c}+W$, the constraint $\abs{\b{y}-\b{a}}=R$ is equivalent to
\[ \abs{\b{y}-\b{a}^\prime}=\rho, \qquad \rho:=\sqrt{R^2-\abs{\b{q}}^2} \]
We should now intersect two spheres in the same affine subspace:
\[ \abs{\b{y}-\b{c}}=r, \qquad \abs{\b{y}-(\b{c}-\b{p})}=\rho \]
Let $\ell:=\abs{\b{p}}$. If $\ell=0$, the two centers coincide. Since the input is consistent, the new constraint is redundant. Assume $\ell>0$, and let $\b{u}:=\b{p}/\ell$. Write $\b{y}=\b{c}+\b{z}$, where $\b{z}\in W$ and $\abs{\b{z}}=r$. The new constraint is $\abs{\b{z}+\b{p}}=\rho$. Subtracting the squared equations gives $2\b{z}\cdot\b{p}+\ell^2=\rho^2-r^2$.
Thus, $\b{z}\cdot\b{u}=-t$, where $t:=(\ell^2-\rho^2+r^2)/(2\ell)$. Hence,
\[ \b{z} = -t\b{u}+\b{w}, \qquad \b{w}\in W\cap\b{u}^\perp, \qquad \abs{\b{w}}=\sqrt{r^2-t^2} \]
Define
\[ \b{c}^\prime:=\b{c}-t\b{u}, \qquad r^\prime:=\sqrt{r^2-t^2}, \qquad W^\prime:=W\cap\b{u}^\perp \]
Then
\[ \mathcal{S}_{k+1} = \Set{ \b{c}^\prime+\b{w} | \b{w}\in W^\prime,\; \abs{\b{w}}=r^\prime } \]
Replacing $(\b{c},r,W)$ with $(\b{c}^\prime,r^\prime,W^\prime)$ preserves the invariant. Equivalently, append $\b{u}$ to $C$. By induction, after all constraints are processed,
\[ \mathcal{S} = \mathcal{S}_N = \Set{ \b{c}+\b{z} | \b{z}\in W,\; \abs{\b{z}}=r } \]
If $W\neq\Set{\b{0}}$, choose any unit vector $\b{v}\in W$ and output $\b{y}:=\b{c}+r\b{v}$. If $W=\Set{\b{0}}$, consistency implies $r=0$, so output $\b{y}:=\b{c}$. In either case, $\b{y}\in\mathcal{S}$.
At most $D$ vectors are appended to $C$. Each projection takes $O(D^2)$ time, so the time complexity is $O(ND^2)$, more precisely $O(ND\min(N,D))$. The memory complexity is $O(D^2)$.