ICPC 2018 - F. Go with the Flow
State the problem in your own words. Focus on the mathematical or algorithmic core rather than repeating the full statement.
Source-first archive entry
This page is built from the copied files in competitive_programming/icpc/2018/F-go-with-the-flow. Edit
competitive_programming/icpc/2018/F-go-with-the-flow/solution.tex to update the written solution and
competitive_programming/icpc/2018/F-go-with-the-flow/solution.cpp to update the implementation.
The website does not replace those files with hand-maintained HTML. It reads the copied source tree during the build and exposes the exact files below.
Problem Statement
Copied statement text kept beside the solution archive for direct reference.
Problem F
Go with the Flow
Time limit: 12 seconds
In typesetting, a “river” is a string of spaces formed by gaps between words that extends down sev-
eral lines of text. For instance, Figure F.1 shows several examples of rivers highlighted in red (text is
intentionally blurred to make the rivers more visible).
Figure F.1: Examples of rivers in typeset text.
Celebrated river authority Flo Ng wants her new book on rivers of the world to include the longest
typographic rivers possible. She plans to set the text in a mono-spaced font (all letters and spaces have
equal width) in a left-aligned column of some fixed width, with exactly one space separating words on
each line (the text is not aligned on the right). For Flo, a “river” is defined as a sequence of spaces lying
in consecutive lines in which the position of each space in the sequence (except the first) differs by at
most 1 from the position of the space in the line above it. Trailing white space cannot appear in a river.
Words must be packed as tightly as possible on lines; no words may be split across lines. The line width
used must be at least as long as the longest word in the text. For instance, Figure F.2 shows the same
text set with two different line widths.
Line width 14: River of length 4 Line width 15: River of length 5
The Yangtze is| The Yangtze is |
the third | the third |
longest river | longest*river |
in*Asia and | in Asia*and the|
the*longest in| longest*in the |
the*world to | world to*flow |
flow*entirely | entirely*in one|
in one country| country |
Figure F.2: Longest rivers (*) for two different line widths.
Given a text, you have been tasked with determining the line width that produces the longest river of
spaces for that text.
Input
The first line of input contains an integer n (2 ≤ n ≤ 2 500) specifying the number of words in the
text. The following lines of input contain the words of text. Each word consists only of lowercase and
uppercase letters, and words on the same line are separated by a single space. No word exceeds 80
characters.
Output
Display the line width for which the input text contains the longest possible river, followed by the length
of the longest river. If more than one line width yields this maximum, display the shortest such line
width.
Sample Input 1 Sample Output 1
21 15 5
The Yangtze is the third longest
river in Asia and the longest in
the world to flow
entirely in one country
Sample Input 2 Sample Output 2
25 21 6
When two or more rivers meet at
a confluence other than the sea
the resulting merged river takes
the name of one of those rivers
Editorial
Rendered from the copied solution.tex file. The original TeX source remains
available below.
Key Observations
Write the structural observations that make the problem tractable.
State any useful invariant, monotonicity property, graph interpretation, or combinatorial reformulation.
If the constraints matter, explain exactly which part of the solution they enable.
Algorithm
Describe the data structures and the state maintained by the algorithm.
Explain the processing order and why it is sufficient.
Mention corner cases explicitly if they affect the implementation.
Correctness Proof
We prove that the algorithm returns the correct answer.
Lemma 1.
State the first key claim.
Proof.
Provide a concise proof.
Lemma 2.
State the next claim if needed.
Proof.
Provide a concise proof.
Theorem.
The algorithm outputs the correct answer for every valid input.
Proof.
Combine the lemmas and finish the argument.
Complexity Analysis
State the running time and memory usage in terms of the input size.
Implementation Notes
Mention any non-obvious implementation detail that is easy to get wrong.
Mention numeric limits, indexing conventions, or tie-breaking rules if relevant.
Code
Exact copied C++ implementation from solution.cpp.
#include <bits/stdc++.h>
using namespace std;
namespace {
void solve() {
// Fill in the full solution logic for the problem here.
}
} // namespace
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
solve();
return 0;
}
Source Files
Exact copied source-of-truth files. Edit solution.tex for the write-up and solution.cpp for the implementation.
\documentclass[11pt]{article}
\usepackage[margin=1in]{geometry}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{amsmath,amssymb,amsthm}
\usepackage{enumitem}
\title{ICPC World Finals 2018\\F. Go with the Flow}
\author{}
\date{}
\begin{document}
\maketitle
\section*{Problem Summary}
State the problem in your own words. Focus on the mathematical or algorithmic core rather than repeating the full statement.
\section*{Key Observations}
\begin{itemize}[leftmargin=*]
\item Write the structural observations that make the problem tractable.
\item State any useful invariant, monotonicity property, graph interpretation, or combinatorial reformulation.
\item If the constraints matter, explain exactly which part of the solution they enable.
\end{itemize}
\section*{Algorithm}
\begin{enumerate}[leftmargin=*]
\item Describe the data structures and the state maintained by the algorithm.
\item Explain the processing order and why it is sufficient.
\item Mention corner cases explicitly if they affect the implementation.
\end{enumerate}
\section*{Correctness Proof}
We prove that the algorithm returns the correct answer.
\paragraph{Lemma 1.}
State the first key claim.
\paragraph{Proof.}
Provide a concise proof.
\paragraph{Lemma 2.}
State the next claim if needed.
\paragraph{Proof.}
Provide a concise proof.
\paragraph{Theorem.}
The algorithm outputs the correct answer for every valid input.
\paragraph{Proof.}
Combine the lemmas and finish the argument.
\section*{Complexity Analysis}
State the running time and memory usage in terms of the input size.
\section*{Implementation Notes}
\begin{itemize}[leftmargin=*]
\item Mention any non-obvious implementation detail that is easy to get wrong.
\item Mention numeric limits, indexing conventions, or tie-breaking rules if relevant.
\end{itemize}
\end{document}
#include <bits/stdc++.h>
using namespace std;
namespace {
void solve() {
// Fill in the full solution logic for the problem here.
}
} // namespace
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
solve();
return 0;
}