A (not necessarily continuous) function on a compact metric space attaining its maximum. I am studying for an exam and my study partners and I are having a dispute about my reasoning for $f$ being continuous by way of open and closed pullbacks (see below).  Please help me correct my thinking.  Here is the problem and my proposed solution:
Let $(K, d)$ be a compact metric space, and let $f: K \rightarrow \mathbb{R}$ be a function satisfying that for each $\alpha \in \mathbb{R}$ the set {$x \in K: f(x) \ge \alpha$} is a closed subset of $K$.  Show that $f$ attains a maximum value on $K$.
Proof: Notice that $A :=$ {$x \in K: f(x) \ge \alpha$} is precisely $f^{-1}[\alpha, \infty)$.  Since $[\alpha, \infty)$ is closed in $\mathbb{R}$ and $A$ is assumed to be closed in $K$, then it follows that $f$ is continuous on $A$.  On the other hand, $K-A = f^{-1}(-\infty, \alpha)$ is open in $K$ since $A$ is closed in $K$.  And since $(\alpha, \infty)$ is open in $\mathbb{R}$ and $K - A$ is open in $K$, then if follow that $f$ is continuous on $K - A$, hence $f$ is continuous on $K$.  Since $K$ is compact and $f$ is continuous, then $f(K)$ is compact in $\mathbb{R}$.  Compact sets in $\mathbb{R}$ are closed and bounded intervals.  Thus $\sup{f(K)} = \max{f(K)} = f(x_0)$ for some $x_0 \in K$.  Thus $f$ indeed attains its maximum value on $K$.  $\blacksquare$
 A: Nope, for continuity, you'd need to know that $f^{-1}(C)$ is closed for all closed sets $C$. You don't know that, you only know it for some closed sets $C$.
Hint for correct solution: Show that $f^{-1}(-\infty,\alpha)$ is open for any $\alpha$. Cover $X$ with sets of the form $f^{-1}(-\infty,\alpha)$. Then it must have  a finite sub-cover.
A: Here is a complete proof with sequential compactness:
Suppose that $f$ has no maximum on $K$. Then there are two cases:
Case 1: $\sup_{x \in K} f(x) = \infty$. 
Then for any $n \in \mathbb{N}$ there is some $x_n \in K$ such that $f(x_n) > n$. Since $K$ is compact there exists a subsequence $x_{n_k} \in K$ that converges to some $x \in K$. Let $A = \{y \in K| f(y) > f(x)+1\}$. Then $x_{n_k} \in A$ for sufficiently large $k$, but $x \not \in A$. Therefore $A$ is not closed.
Case 2: $\sup_{x \in K} f(x) = L \in \mathbb{R}$. 
Then for any $n \in \mathbb{N}$ there is some $x_n \in K$ such that $f(x_n) > L - \frac{1}{n}$. Again since $K$ is compact there exists a subsequence $x_{n_k} \in K$ that converges to some $x \in K$. Because $f$ attains no maximum, we have $f(x) < L$. Let $A = \{y \in K| f(y) > \frac{f(x)+L}{2}\}$. Then $x_{n_k} \in A$ for sufficiently large $k$, but again $x \not \in A$. Hence $A$ is not closed as in Case 1. $\blacksquare$
A: Consider the function $f: [0,1] \to \mathbb R$
\[f(x) = \begin{cases}
 1 &\text{if }x \le 1/2 \\
 0 &\text{otherwise}
\end{cases}\]
$f$ is defined from a compact metric space, and is certainly not continuous (e.g. because the preimage of $(0,2)$ is not open, or because the preimage of $\{0\}$ is not closed). However, the set $\{ x \in [0,1] : f(x) \ge \alpha \}$ is:


*

*empty if $\alpha > 1$,

*$[0,1/2]$ if $0 < \alpha \le 1$,

*$[0,1]$ if $\alpha \le 0$.


In all three cases it is closed. So it is possible for $f$ not to be continuous, so your argument can't possibly work.
In order to prove that $f$ is continuous on $A$, you want to prove that for any open (or closed) subset of $A$, the corresponding preimage $f^{-1}(A)$  is open (or closed). You've only proven that for $A$ itself, but in general $A$ may have many open/closed subsets that you will need to also check.
As for actually answering your question: let's say $C_\alpha = \{ x : f(x) \ge \alpha \}$. Every $C_\alpha$ is closed. Now in a compact space, if a collection of closed sets is such that every finite subcollection has non-empty intersection, then the whole collection has non-empty intersection (this is equivalent to "every open cover has a finite subcover", by taking complements and the contrapositive). Apply this to the collection of non-empty $C_\alpha$.
