Continuous functions that attain local extrema at every point Let $f:[0,1]\to\mathbb R$ is a continuous function, and for all $x\in [0,1]$, $f(x)$ is either a local maximum or a local minimum. Then prove that $f$ is a constant.
Here is what I have tried, I don't know whether it is correct:
Assume $f$ is not a constant, by continuity, there exist $x_1$, $x_2$ such that $f(x_1)=m$ is a global minimum and $f(x_2)=M$ is a global maximum. Now if $m\ne M$, choose any $c\in(m,M)$ and define
$$x_0=\sup (x\in[x_1,x_2]:f(x)<c)$$
Then we have $f(x_0)$ NOT a local minimum nor a local maximum (since according to the definition of $x_0$, there are always some points $<c$ in the left neighborhood of $x_0$ and the points in right neighborhood are always $>c$). Contradiction arises and so $f$ must be a constant.
 A: It is a funny countable vs uncountable argument, I least expected that. My hint (in the comments) to use a nested sequence of intervals only made me confused. (But it made me think that if $C$ is the union of all open intervals on which $f$ is locally constant then $f(C)$ is countable. I tinkered a bit with ideas like considering cases, whether $[0,1]\setminus C$ contains an open interval (then the proof by the OP works), or whether $C$ is dense, looked at $f([0,1])\setminus f(C)$ and at some point figured it was easier than that and I may formally forget about $C$, and ignore some irrelevant details.) 
So say $f$ were not constant, hence $f([0,1])=[m,M]$ with $m<M$. For each $y\in[m,M]$ pick $x_y\in[0,1]$ with $f(x_y)=y$. Let $X=\{x_y:y\in[m,M]\}$. In other words we picked a set $X\subset[0,1]$ such that the restriction of $f$ to $X$ is one-to-one and onto $[m,M]$. 
Let $T=\{x\in X: f $ has a local maximum at $x\}$ and $S=\{x\in X: f $ has a local minimum at $x\}$. Since $X=T\cup S$ and (cardinality) $|X|=|[m,M]|=\mathfrak c = 2^{\aleph_0}$, at least one of $T$ and $S$ is uncountable. Say $T$ is uncountable. 
For each $x\in T$ pick $n_x\in\mathbb N$ such that $f(x)\ge f(z)$ whenever $z\in(x-\dfrac1n,x+\dfrac1n)\cap[0,1]$. Let $T_n=\{x\in X: n_x=n\}$. Clearly $T=\bigcup\limits_{n\in\mathbb N} T_n$, hence there is an $n$ for which $T_n$ is uncountable.  
Then $T_n$ must have a limit point, i.e. a $t\in[0,1]$ such that every neighborhood of $t$ contains infinitely many elements of $T_n$. Hence we may pick two different points $p,q\in T_n$ each very close to $t$ and hence (absolute value) $|p-q|<\dfrac1n$. But this means $f(p)\le f(q)$ and $f(q)\le f(p)$, thus $f(p)=f(q)$, contradicting that the restriction of $f$ to $X$ were one-to-one. 
A: One reason why I quite like the problem is that it starts with a nice magic trick (a misdirection if you like).  Since it
is posed for continuous functions one immediately starts by thinking about that, when in fact you should be simply investigating the
nature of the set of local extrema.  As others have pointed out it is just a countable vs. uncountable situation that resolves the
problem.  Continuity just jumps in at the last moment to finish the argument.
I don't have any seriously new ideas but I would like to remind readers of a standard device in real variable arguments that works quite 
simply here too.  It is worth keeping in your toolkit and has been used many, many times. So this is essentially the same proof as the others but with more of a real-variable feel, than a topological one.

Lemma.  Let $f:\mathbb{R}\to\mathbb{R}$ and let $E$ be the set of
  points at which $f$ has a local maximum or a local minimum. Then there
  is a denumerable decomposition of $E$ into a sequence of sets
  $\{E_n\}$ such that $f$ is constant on each $E_n$.

Proof.  Let $A$ be the set of points where $f$ has a local maximum.  For each $x\in A$ there is a $ \delta(x)>0$ so
that $f(z)\leq f(x)$ for all $z\in (x-\delta(x),x+\delta(x))$.
Define the collection
$$
A_{nj} = \left\{
 x\in A: \frac{1}{n} \leq \delta(x) < \frac1{n-1}
  \right\}
\cap \left[ \frac{j}n, \frac{j+1}n \right)
$$
for $n=1,2,3 \dots$ and $j=0, \pm 1, \pm 2, \pm 3, \dots$.
Suppose that $x$ and $y$ with $x<y$ belong to the same set $A_{nj}$.  Then
$$
0< y-x < \frac1n \leq \delta(x)  \implies f(y) \leq f(x)
$$
while 
$$
0< y-x < \frac1n \leq \delta(y)  \implies f(x) \leq f(y)
.$$
 Thus $f$ is constant on each set $A_{nj}$.  The same argument handles the set of points where $f$ has a local minimum.  QED
Then, in order to misdirect our easily mislead students, we pose the problem this way:

Problem.  Suppose $f:[0,1]\to\mathbb{R}$ is a continuous function and
  every point of $(0,1)$ with at most countably many exceptions is
  either a local maximum or a local minimum.  Must $f$ be constant on
  $[0,1]$?


Comments on the method.
I am by no means an historian of mathematics, but that has never stopped me from speculating on the sources of ideas in analysis.  I can trace this little technique back at least to

Beppo Levi, Richerche sulle funzioni derivate, Atti della Accademia
  Reale (Nazionale) dei Lincei, (1906).

So, as a particularly lazy historian, I am going to award the title to the Italian mathematician Beppo Levi [1875-1961] (not, it should be noted, one of the Marx brothers).
His theorem was this: 

Theorem (Levi) Suppose that   $f:\mathbb{R}\to\mathbb{R}$ has at each point $x$   of a set  $E$ a finite
  right-derivative $f_+'(x)$ and a finite left derivative $f_-'(x)$. 
  Then $f_-'(x)=f_+'(x)$ at all but countably many point of $E$.

His proof is just this (expressed similarly to the technique above).  We can analyze the set where $f_-'(x)<f_+'(x)$
 by considering the set
 $$E^r= \{x\in E:   f_-'(x)< r < f_+'(x)\}$$
 for rationals $r$.
For each $x\in E^r$ there is a $  \delta(x)>0$ so
 that $[f(z)-f(x)/[z-x]> r $ for all $z\in (x,x+\delta(x))$
 and at the same time
  $[f(z)-f(x)/[z-x]< r $ 
 for all $z\in (x-\delta(x),x)$.
Define the collection
 $$
 A_{nj} = \left\{
 x\in E^r: \frac{1}{n} \leq \delta(x) < \frac1{n-1}
 \right\}
 \cap \left[ \frac{j}n, \frac{j+1}n \right)
 $$
 for $n=1,2,3 \dots$ and $j=0, \pm 1, \pm 2, \pm 3, \dots$.
Now show that there cannot be two or more points in any set
 $ A_{nj}$.  That means that $E^r$ is countable and hence (since the rationals are countable)   the set
 $\{x\in E: f_-'(x) < f_+'(x)\}$ is countable.
A very nice simple proof but, more importantly, a technique that can be and has been used a great many times.
A: For any $x\in [0,1]$ there is a open set $U_x \subset [0,1]$ such that $f$ restricted to $U_x$ is minimal or maximal at $x$. Thus the image of $f$ consists of all the existing  minimal and maximal values of $f$ restricted the $U_x$. 
Now since $[0,1]$ has a countable base we can choose the $U_x$ in a way that only countable many of them are distinct. 
This shows that the image of $f$ is countable, but since it is also connected it has to be a point. 
I found this proof here. 
