All ICPC entries
Competitive Programming

ICPC 2015 - H. Qanat

State the problem in your own words. Focus on the mathematical or algorithmic core rather than repeating the full statement.

Source sync Apr 19, 2026
Track ICPC
Year 2015
Files TeX, C++, statement assets
Folder competitive_programming/icpc/2015/H-qanat
ICPC2015TeXC++statement textstatement pdf

Source-first archive entry

This page is built from the copied files in competitive_programming/icpc/2015/H-qanat. Edit competitive_programming/icpc/2015/H-qanat/solution.tex to update the written solution and competitive_programming/icpc/2015/H-qanat/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 H
                                                 Qanat
                                       Time limit: 2 seconds
A qanat is an irrigation system widely used to deliver water in hot, arid climates. The technology was
originally developed by Persians over 2000 years ago. In Morocco, qanats are known as khettara and
are still used today in the southern part of the country.
The basic feature of a qanat is an essentially horizontal channel that brings water from an underground
water source to an outlet near a civilization. There is also a shaft known as a mother well that rises
vertically from the underground water source to the surface of a mountain or hill. Creating such a system
is extremely expensive, and was especially so in ancient times, since all of the materials excavated from
the channel and mother well must be carried above ground, either through the channel outlet or the top of
the mother well. To aid in the construction, there are often one or more additional vertical shafts placed
at strategic locations above the underground channel. Although these shafts must also be excavated, they
provide a means for lifting additional dirt from the horizontal channel as illustrated in Figure H.1.

                                    Figure H.1: An illustration of a qanat.

For this problem, model the cross-section of a qanat as shown in Figure H.2, with the channel outlet at
(0, 0), the water source at (w, 0), and the top of the mother well at (w, h) with w > h. The surface of
the mountain extends along a straight line from (w, h) to (0, 0).

                                                                              (w, h)

                           (0, 0)                                             (w, 0)

                        Figure H.2: A simplified model of a qanat cross-section.

Every qanat must have a vertical mother well from the water source to the mountain surface above, along
with n additional vertical shafts. The channel and all shafts are modeled as line segments. Your goal
is to determine the placement for those additional shafts so as to minimize the overall excavation cost.
This cost is equal to the sum of the distances that each piece of excavated dirt must be transported to
reach the surface (using any combination of horizontal and vertical movement). For example, the cost
of excavating a continuous section
                             R`     of dirt starting from the surface and going along a path of length `
(possibly including turns) is 0 x dx = 21 `2 .

Input

The input consists of a single line containing three integers w (1 ≤ w ≤ 10 000), h (1 ≤ h < w), and
n (1 ≤ n ≤ 1 000). The value w is the horizontal distance from the water source to the qanat outlet.
The value h is the vertical distance from the water source to the mountain surface. The value n is the
number of vertical shafts that must be used in addition to the mother well.

Output

First, display the minimum overall excavation cost. Next, display the x-coordinates, in increasing order,
for n optimally placed vertical shafts. If n > 10, display only the first 10 x-coordinates. Answers within
an absolute or relative error of 10−4 will be accepted. You may assume that there is a unique solution.
No test case will result in a shaft within 0.001 units from the outlet of the qanat channel or from another
shaft.

 Sample Input 1                                       Sample Output 1
 8 4 1                                                31.500000
                                                      3.000000

 Sample Input 2                                       Sample Output 2
 195 65 2                                             12220.000000
                                                      48.000000
                                                      108.000000

 Sample Input 3                                       Sample Output 3
 10000 1 1000                                         30141.885677
                                                      9.956721
                                                      19.913443
                                                      29.870164
                                                      39.826887
                                                      49.783610
                                                      59.740334
                                                      69.697060
                                                      79.653786
                                                      89.610515
                                                      99.567245

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

  1. Describe the data structures and the state maintained by the algorithm.

  2. Explain the processing order and why it is sufficient.

  3. 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.

C++ competitive_programming/icpc/2015/H-qanat/solution.cpp

Exact copied implementation source.

Raw file
#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.

TeX write-up competitive_programming/icpc/2015/H-qanat/solution.tex

Exact copied write-up source.

Raw file
\documentclass[11pt]{article}
\usepackage[margin=1in]{geometry}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{amsmath,amssymb,amsthm}
\usepackage{enumitem}

\title{ICPC World Finals 2015\\H. Qanat}
\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}