Why is it continuous? I came across this proof that I don't understand while reading the book "Majority Judgement" by Balinski and Laraki.
To simplify, we have a function $f$ which is continuous and monotonic. $f$ takes n arguments and give back one of them, so $f(r_1, ..., r_n) = r_k$ for some $k$. Also, the order of the arguments doesn't matter so we can assume $r_1 \geq ... \geq r_k$. We want to show that now matter what the inputs are, f always takes the same kth largest value. Here is how the proof goes.
Let $\textbf{r} = (r_1, ..., r_n)$, define $g(\textbf{r}) = k$ when $f(\textbf{r}) = r_k$ on the open set $R > r_1 > ... > r_n > 0$. The continuity of $f$ implies the continuity of $g$ on this set. Since $g$ only takes integer values, it must be a constant on this set. So $f(\textbf{r}) = r_k$ for the same $k$ everywhere on this set, hence everywhere by the continuity of $f$, completing the proof.
This is the proof of Theorem 10.1 in the book. I don't understand why the continuity of $f$ should imply the continuity of $g$, and why the continuity of $f$ should imply $f(\textbf{r}) = r_k$ everywhere.
 A: Maybe it will help to work out the concrete case $n = 2$.  We consider the open set $U = \{R > r_1 > r_2 > 0\}$; you can visualize this as the part of the square $(0, R)^2$ in the plane which is below the diagonal line $r_1 = r_2$.  Let $\pi_1$ and $\pi_2$ denote the projection functions restricted to this set, so that $\pi_1(r_1, r_2) = r_1$ and $\pi_2(r_1, r_2) = r_2$.
If I understand your hypotheses correctly, $f$ is assumed to be a continuous function on $U$ with the property that for every $(r_1, r_2)$ we have $f(r_1, r_2)$ is either equal to $r_1$ or $r_2$ and you want to prove that globally either $f = \pi_1$ or $f = \pi_2$.
So let's first consider the function $g$ that you introduced - you ask why it is continuous.  The cleanest answer uses the pasting lemma, which says that if we write $U$ as the union of two closed subsets and define a function on $U$ whose restriction to each subset is continuous then the function is continuous on $U$.
To apply this lemma, let $A_1$ denote the set of points on which $f$ agrees with $\pi_1$ and let $A_2$ denote the set of points on which $f$ agrees with $\pi_2$.  Note that $U = A_1 \cup A_2$ since $f$ agrees with either $\pi_1$ or $\pi_2$ everywhere on $U$.  Moreover $A_1$ and $A_2$ are closed subsets of $U$ since $A_i = (f - \pi_i)^{-1}(\{0\})$ (and the inverse image of a closed set under a continuous function is closed).  You have defined $g \colon U \to \mathbb{R}$ to be the function which restricts to the constant function $1$ on $A_1$ and the constant function $2$ on $A_2$.  Constant functions are continuous, so the hypotheses of the pasting lemma are satisfied and we conclude that $g$ is continuous.
Finally, you ask why the continuity of $g$ implies the conclusion of the theorem.  The answer uses the concept of connectedness, and specifically the fact that the continuous image of a connected set is connected.  Indeed $U$ is connected and thus the set $g(U)$ is a connected subset of $\mathbb{R}$.  But by definition $g(U)$ is a nonempty subset of the two point set $\{1, 2\}$, and the only connected subsets of a two point set are one point sets.  So $g$ is either the constant function $1$ or the constant function $2$ on $U$, which means that either $A_2$ or $A_1$ is the empty set.  But if $A_2$ is empty then $U = A_1$ and hence $f = \pi_1$ on $U$, and similarly if $A_1$ is empty then $f = \pi_2$.
(Note: since connectedness is involved anyway, we could have bypassed $g$ entirely.  The sets $A_1$ and $A_2$ are disjoint since $\pi_1$ is never equal to $\pi_2$ on $U$, so $A_1$ and $A_2$ form a separation of $U$ and hence one of them must be empty since $U$ is connected.  I'm not sure why the authors chose to write the argument the way they did.)
This completes the proof in the case $n=2$; you can imitate this argument for larger $n$, or just use induction.
