4. Let the sequence of real numbers $a_{0}, a_{1}, \cdots$ satisfy $a_{0}=0, a_{1}=1$, and for each positive integer $n \geqslant 2$, there exists a positive integer $k$ $(1 \leqslant k \leqslant n)$, such that
$$
a_{n}=\frac{a_{n-1}+a_{n-2}+\cdots+a_{n-k}}{k} .
$$

Find the maximum value of $a_{2018}-a_{2017}$.
4. The maximum value is $\frac{2016}{2017^{2}}$.

When $a_{1}=a_{2}=\cdots=a_{2016}=1$,
$a_{2017}=\frac{a_{2016}+a_{2015}+\cdots+a_{0}}{2017}=1-\frac{1}{2017}$,
$a_{2018}=\frac{a_{2017}+a_{2016}+\cdots+a_{1}}{2017}=1-\frac{1}{2017^{2}}$,
then $a_{2018}-a_{2017}=\frac{2016}{2017^{2}}$.
Let $S(n, k)=a_{n-1}+a_{n-2}+\cdots+a_{n-k}$,

where, non-negative integers $k \leqslant n$.
In particular, $S(n, 0)=0, S(n, 1)=a_{n-1}$.
Then for each positive integer $n \geqslant 2$, there exists a positive integer $k(1 \leqslant k \leqslant n)$, such that $a_{n}=\frac{S(n, k)}{k}$.
For each positive integer $n$, define:
$$
\begin{array}{l}
M_{n}=\max _{1 \leqslant k \leqslant n}\left\{\frac{S(n, k)}{k}\right\}, \\
m_{n}=\min _{1 \leqslant k \leqslant n}\left\{\frac{S(n, k)}{k}\right\}, \\
\Delta_{n}=M_{n}-m_{n} \geqslant 0 .
\end{array}
$$

By definition, for all positive integers $n \geqslant 2$, we have $a_{n} \in\left[m_{n}, M_{n}\right]$.
Since $a_{n-1}=\frac{S(n, 1)}{1} \in\left[m_{n}, M_{n}\right]$, thus, $a_{2018}-a_{2017} \leqslant M_{2018}-m_{2018}=\Delta_{2018}$. By definition, for all positive integers $k \leqslant n$, we have $k m_{n} \leqslant S(n, k) \leqslant k M_{n}$,
and when $k=0$, the above inequality still holds.
Lemma For each positive integer $n>2$, we have $\Delta_{n} \leqslant \frac{n-1}{n} \Delta_{n-1}$.
Proof Select appropriate positive integers $k, l \leqslant n$, such that $M_{n}=\frac{S(n, k)}{k}, m_{n}=\frac{S(n, l)}{l}$.
By $S(n, k)=a_{n-1}+S(n-1, k-1)$,
$S(n-1, k-1) \leqslant(k-1) M_{n-1}$,
then $k\left(M_{n}-a_{n-1}\right)=S(n, k)-k a_{n-1}$
$=S(n-1, k-1)-(k-1) a_{n-1}$
$\leqslant(k-1)\left(M_{n-1}-a_{n-1}\right)$.
Similarly,
$l\left(a_{n-1}-m_{n}\right)=(l-1) a_{n-1}-S(n-1, l-1)$
$\leqslant(l-1)\left(a_{n-1}-m_{n-1}\right)$.
Since $m_{n-1} \leqslant a_{n-1} \leqslant M_{n-1}, k, l \leqslant n$, thus,
$M_{n}-a_{n-1} \leqslant \frac{k-1}{k}\left(M_{n-1}-a_{n-1}\right)$
$\leqslant \frac{n-1}{n}\left(M_{n-1}-a_{n-1}\right)$,
$a_{n-1}-m_{n} \leqslant \frac{l-1}{l}\left(a_{n-1}-m_{n-1}\right)$
$\leqslant \frac{n-1}{n}\left(a_{n-1}-m_{n-1}\right)$.
Hence $\Delta_{n}=\left(M_{n}-a_{n-1}\right)+\left(a_{n-1}-m_{n}\right)$
$\leqslant \frac{n-1}{n}\left(\left(M_{n-1}-a_{n-1}\right)+\left(a_{n-1}-m_{n-1}\right)\right)$
$=\frac{n-1}{n} \Delta_{n-1}$.
The lemma is proved.
If for all positive integers $n \leqslant 2017$, we have $a_{n}=1$, then $a_{2018} \leqslant 1$.
Thus, $a_{2018}-a_{2017} \leqslant 0$.
If there exists a positive integer $q(2 \leqslant q \leqslant 2017)$, such that $a_{q}<1$, and $q$ is the smallest positive integer, then for all $i=1,2, \cdots, q-1$, we have
$S(q, i)=i$, and $S(q, q)=q-1$.
Thus $a_{q}=\frac{S(q, q)}{q}=1-\frac{1}{q}<1$.
Therefore, for each $i=1,2, \cdots, q$, we have
$S(q+1, i)=i-\frac{1}{q}$,
$S(q+1, q+1)=q-\frac{1}{q}$.
Thus $m_{q+1}=\frac{S(q+1,1)}{1}$
$=\frac{S(q+1, q+1)}{q+1}=\frac{q-1}{q}$,
$M_{q+1}=\frac{S(q+1, q)}{q}=\frac{q^{2}-1}{q^{2}}$,
$\Delta_{q+1}=M_{q+1}-m_{q+1}=\frac{q-1}{q^{2}}$.
Let $N=2017 \geqslant q$, for $n=q+2, q+3, \cdots$
$N+1$, by the lemma we get
$$
\begin{array}{l}
\Delta_{N+1} \leqslant \frac{q-1}{q^{2}} \cdot \frac{q+1}{q+2} \cdot \frac{q+2}{q+3} \cdot \cdots \cdot \frac{N}{N+1} \\
=\frac{1}{N+1}\left(1-\frac{1}{q^{2}}\right) \\
\leqslant \frac{1}{N+1}\left(1-\frac{1}{N^{2}}\right)=\frac{N-1}{N^{2}},
\end{array}
$$

i.e., $\Delta_{2018} \leqslant \frac{2016}{2017^{2}}$.
In summary, the maximum value of $a_{2018}-a_{2011}$ is $\frac{2016}{2017^{2}}$.