Small fixes
This commit is contained in:
parent
e79e59a839
commit
6a9049f35b
|
@ -283,7 +283,7 @@ For example, consider the following operation:
|
||||||
First, we calculate the minimum distance
|
First, we calculate the minimum distance
|
||||||
from the white cell marked with * to a black cell.
|
from the white cell marked with * to a black cell.
|
||||||
The minimum distance is 2, because we can move
|
The minimum distance is 2, because we can move
|
||||||
to steps left to a black cell.
|
two steps left to a black cell.
|
||||||
Then, we paint the white cell black:
|
Then, we paint the white cell black:
|
||||||
|
|
||||||
\begin{center}
|
\begin{center}
|
||||||
|
@ -316,7 +316,8 @@ $O(\sqrt n)$ \emph{batches}, each of which consists
|
||||||
of $O(\sqrt n)$ operations.
|
of $O(\sqrt n)$ operations.
|
||||||
At the beginning of each batch,
|
At the beginning of each batch,
|
||||||
we perform Algorithm 1.
|
we perform Algorithm 1.
|
||||||
Then, we use Algorithm 2 to process the batches.
|
Then, we use Algorithm 2 to process the operations
|
||||||
|
in the batch.
|
||||||
We clear the list of Algorithm 2 between
|
We clear the list of Algorithm 2 between
|
||||||
the batches.
|
the batches.
|
||||||
At each operation,
|
At each operation,
|
||||||
|
@ -341,13 +342,12 @@ if a positive integer $n$ is represented as
|
||||||
a sum of positive integers,
|
a sum of positive integers,
|
||||||
such a sum always contains at most
|
such a sum always contains at most
|
||||||
$O(\sqrt n)$ \emph{distinct} numbers.
|
$O(\sqrt n)$ \emph{distinct} numbers.
|
||||||
|
|
||||||
The reason for this is that to construct
|
The reason for this is that to construct
|
||||||
a sum that contains a maximum number of distinct
|
a sum that contains a maximum number of distinct
|
||||||
numbers, we should choose \emph{small} numbers.
|
numbers, we should choose \emph{small} numbers.
|
||||||
If we choose the numbers $1,2,\ldots,k$,
|
If we choose the numbers $1,2,\ldots,k$,
|
||||||
the resulting sum is
|
the resulting sum is
|
||||||
\[\frac{k(k+1)}{2} \le n.\]
|
\[\frac{k(k+1)}{2}.\]
|
||||||
Thus, the maximum amount of distinct numbers is $k = O(\sqrt n)$.
|
Thus, the maximum amount of distinct numbers is $k = O(\sqrt n)$.
|
||||||
Next we will discuss two problems that can be solved
|
Next we will discuss two problems that can be solved
|
||||||
efficiently using this observation.
|
efficiently using this observation.
|
||||||
|
@ -371,19 +371,20 @@ $\{1,3,3\}$, the possible sums are as follows:
|
||||||
|
|
||||||
Using the standard knapsack approach (see Chapter 7.4),
|
Using the standard knapsack approach (see Chapter 7.4),
|
||||||
the problem can be solved as follows:
|
the problem can be solved as follows:
|
||||||
we define a function $f(k,s)$ whose value is 1
|
we define a function $\texttt{possible}(x,k)$ whose value is 1
|
||||||
if the sum $s$ can be formed using the first $k$ weights,
|
if the sum $x$ can be formed using the first $k$ weights,
|
||||||
and 0 otherwise.
|
and 0 otherwise.
|
||||||
All values of this function can be calculated
|
Since the sum of the weights is $n$,
|
||||||
|
there are at most $n$ weights and
|
||||||
|
all values of the function can be calculated
|
||||||
in $O(n^2)$ time using dynamic programming.
|
in $O(n^2)$ time using dynamic programming.
|
||||||
|
|
||||||
However, we can make the algorithm more efficient
|
However, we can make the algorithm more efficient
|
||||||
by using the fact that the sum of the weights is $n$,
|
by using the fact that there are at most $O(\sqrt n)$
|
||||||
which means that there are at most $O(\sqrt n)$
|
\emph{distinct} weights.
|
||||||
distinct weights.
|
|
||||||
Thus, we can process the weights in groups
|
Thus, we can process the weights in groups
|
||||||
such that all weights in each group are equal.
|
that consists of similar weights.
|
||||||
It turns out that we can process each group
|
We can process each group
|
||||||
in $O(n)$ time, which yields an $O(n \sqrt n)$ time algorithm.
|
in $O(n)$ time, which yields an $O(n \sqrt n)$ time algorithm.
|
||||||
|
|
||||||
The idea is to use an array that records the sums of weights
|
The idea is to use an array that records the sums of weights
|
||||||
|
@ -396,12 +397,13 @@ can be formed using this group and the previous groups.
|
||||||
|
|
||||||
\subsubsection{String construction}
|
\subsubsection{String construction}
|
||||||
|
|
||||||
Given a string \texttt{s} and a dictionary $D$ of strings,
|
Given a string \texttt{s} of length $n$
|
||||||
|
and a set of strings $D$ whose total length is $m$,
|
||||||
consider the problem of counting the number of ways
|
consider the problem of counting the number of ways
|
||||||
\texttt{s} can be formed as a concatenation of strings in $D$.
|
\texttt{s} can be formed as a concatenation of strings in $D$.
|
||||||
For example,
|
For example,
|
||||||
if \texttt{s} is \texttt{ABAB} and $D$ is
|
if $\texttt{s}=\texttt{ABAB}$ and
|
||||||
$\{\texttt{A},\texttt{B},\texttt{AB}\}$,
|
$D=\{\texttt{A},\texttt{B},\texttt{AB}\}$,
|
||||||
there are 4 ways:
|
there are 4 ways:
|
||||||
|
|
||||||
\begin{itemize}[noitemsep]
|
\begin{itemize}[noitemsep]
|
||||||
|
@ -411,12 +413,10 @@ there are 4 ways:
|
||||||
\item $\texttt{AB}+\texttt{AB}$
|
\item $\texttt{AB}+\texttt{AB}$
|
||||||
\end{itemize}
|
\end{itemize}
|
||||||
|
|
||||||
Assume that the length of \texttt{s} is $n$
|
|
||||||
and the total length of the strings in $D$ is $m$.
|
|
||||||
We can solve the problem using dynamic programming:
|
We can solve the problem using dynamic programming:
|
||||||
Let $f(k)$ denote the number of ways to construct the prefix
|
Let $\texttt{count}(k)$ denote the number of ways to construct the prefix
|
||||||
$\texttt{s}[0 \ldots k]$ using the strings in $D$.
|
$\texttt{s}[0 \ldots k]$ using the strings in $D$.
|
||||||
Now $f(n-1)$ gives the answer to the problem,
|
Now $\texttt{count}(n-1)$ gives the answer to the problem,
|
||||||
and we can solve the problem in $O(n^2)$ time
|
and we can solve the problem in $O(n^2)$ time
|
||||||
using a trie structure.
|
using a trie structure.
|
||||||
|
|
||||||
|
@ -425,12 +425,12 @@ by using string hashing and the fact that there
|
||||||
are at most $O(\sqrt m)$ distinct string lengths in $D$.
|
are at most $O(\sqrt m)$ distinct string lengths in $D$.
|
||||||
First, we construct a set $H$ that contains all
|
First, we construct a set $H$ that contains all
|
||||||
hash values of the strings in $D$.
|
hash values of the strings in $D$.
|
||||||
Then, when calculating a value of $f(k)$,
|
Then, when calculating a value of $\texttt{count}(k)$,
|
||||||
we consider each integer $p$
|
we go through all values of $p$
|
||||||
such that there is a string of length $p$ in $D$,
|
such that there is a string of length $p$ in $D$,
|
||||||
calculate the hash value of $\texttt{s}[k-p+1 \ldots k]$
|
calculate the hash value of $\texttt{s}[k-p+1 \ldots k]$
|
||||||
and check if it belongs to $H$.
|
and check if it belongs to $H$.
|
||||||
Since there are at most $O(\sqrt m)$ distinct word lengths,
|
Since there are at most $O(\sqrt m)$ distinct string lengths,
|
||||||
this results in an algorithm whose running time is $O(n \sqrt m)$.
|
this results in an algorithm whose running time is $O(n \sqrt m)$.
|
||||||
|
|
||||||
\section{Mo's algorithm}
|
\section{Mo's algorithm}
|
||||||
|
|
Loading…
Reference in New Issue