ICPC 2016 - L. Swap Space
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/2016/L-swap-space. Edit
competitive_programming/icpc/2016/L-swap-space/solution.tex to update the written solution and
competitive_programming/icpc/2016/L-swap-space/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 L
Swap Space
Time limit: 6 seconds
You administer a large cluster of computers with hard drives that use various file system types to store
data. You recently decided to unify the file systems to the same type. That is quite a challenge since all
the drives are currently in use, all of them are filled with important data to the limits of their capacities,
and you cannot afford to lose any of the data. Moreover, reformatting a drive to use a new file system
may significantly change the drive’s capacity. To make the reformat possible, you will have to buy an
extra hard drive. Obviously, you want to save money by minimizing the size of such extra storage.
You can reformat the drives in any order. Prior to reformatting a drive, you must move all data from that
drive to one or more other drives, splitting the data if necessary. After a drive is reformatted, you can
immediately start using it to store data from other drives. It is not necessary to put all the data on the
same drives they originally started on – in fact, this might even be impossible if some of the drives have
smaller capacity with the new file system. It is also allowed for some data to end up on the extra drive.
As an example, suppose you have four drives A, B, C, and D with drive capacities 6, 1, 3, and 3 GB.
Under the new file system, the capacities become 6, 7, 5, and 5 GB, respectively. If you buy only 1 GB
of extra space, you can move the data from drive B there and then reformat drive B. Now you have 7
GB free on drive B, so you can move the 6 GB from drive A there and reformat drive A. Finally, you
move the six total gigabytes from drives C and D to drive A, and reformat C and D.
Input
The input begins with a line containing one integer n (1 ≤ n ≤ 106 ), which is the number of drives in
your cluster. Following this are n lines, each describing a drive as two integers a and b, where a is the
capacity with the old file system and b is the capacity with the new file system.
All capacities are given in gigabytes and satisfy 1 ≤ a, b ≤ 109 . (One thousand petabytes should be
enough for everyone, right?)
Output
Display the total extra capacity in gigabytes you must buy to reformat the drives.
Sample Input 1 Sample Output 1
4 1
6 6
1 7
3 5
3 5
Sample Input 2 Sample Output 2
4 5
2 2
3 3
5 1
5 10
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 2016\\L. Swap Space}
\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;
}