Guide To Competitive Programming (001-150) (001-015)
Guide To Competitive Programming (001-150) (001-015)
Antti Laaksonen
Guide to
Competitive
Programming
Learning and Improving Algorithms
Through Contests
Second Edition
Undergraduate Topics in Computer
Science
Series Editor
Ian Mackie, University of Sussex, Brighton, UK
Advisory Editors
Samson Abramsky , Department of Computer Science, University of Oxford,
Oxford, UK
Chris Hankin , Department of Computing, Imperial College London, London, UK
Mike Hinchey , Lero – The Irish Software Research Centre, University of
Limerick, Limerick, Ireland
Dexter C. Kozen, Department of Computer Science, Cornell University, Ithaca,
NY, USA
Andrew Pitts , Department of Computer Science and Technology, University of
Cambridge, Cambridge, UK
Hanne Riis Nielson , Department of Applied Mathematics and Computer Science,
Technical University of Denmark, Kongens Lyngby, Denmark
Steven S. Skiena, Department of Computer Science, Stony Brook University, Stony
Brook, NY, USA
Iain Stewart , Department of Computer Science, Durham University, Durham, UK
‘Undergraduate Topics in Computer Science’ (UTiCS) delivers high-quality
instructional content for undergraduates studying in all areas of computing and
information science. From core foundational and theoretical material to final-year
topics and applications, UTiCS books take a fresh, concise, and modern approach
and are ideal for self-study or for a one- or two-semester course. The texts are all
authored by established experts in their fields, reviewed by an international advisory
board, and contain numerous examples and problems, many of which include fully
worked solutions.
The UTiCS concept relies on high-quality, concise books in softback format, and
generally a maximum of 275–300 pages. For undergraduate textbooks that are likely
to be longer, more expository, Springer continues to offer the highly regarded Texts
in Computer Science series, to which we refer potential authors.
Guide to Competitive
Programming
Learning and Improving Algorithms
Through Contests
Second Edition
123
Antti Laaksonen
Department of Computer Science
University of Helsinki
Helsinki, Finland
This Springer imprint is published by the registered company Springer Nature Switzerland AG
The registered company address is: Gewerbestrasse 11, 6330 Cham, Switzerland
Preface to the Second Edition
This second edition of the book contains several new sections that discuss advanced
topics, such as calculating the Fourier transform, finding minimum cost flows in
graphs, and using automata in string problems.
I am grateful to Olli Matilainen for reading through most of the new material and
giving many useful comments and suggestions.
v
Preface to the First Edition
vii
viii Preface to the First Edition
1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.1 What Is Competitive Programming? . . . . . . . . . . . . . . . . . . . . . 1
1.1.1 Programming Contests . . . . . . . . . . . . . . . . . . . . . . . . 2
1.1.2 Tips for Practicing . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.2 About This Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.3 CSES Problem Set . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.4 Other Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
2 Programming Techniques . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
2.1 Language Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
2.1.1 Input and Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
2.1.2 Working with Numbers . . . . . . . . . . . . . . . . . . . . . . . 12
2.1.3 Shortening Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
2.2 Recursive Algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
2.2.1 Generating Subsets . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
2.2.2 Generating Permutations . . . . . . . . . . . . . . . . . . . . . . . 17
2.2.3 Backtracking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
2.3 Bit Manipulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
2.3.1 Bit Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
2.3.2 Representing Sets . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
3 Efficiency . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
3.1 Time Complexity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
3.1.1 Calculation Rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
3.1.2 Common Time Complexities . . . . . . . . . . . . . . . . . . . . 30
3.1.3 Estimating Efficiency . . . . . . . . . . . . . . . . . . . . . . . . . 31
3.1.4 Formal Definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
3.2 Algorithm Design Examples . . . . . . . . . . . . . . . . . . . . . . . . . . 32
3.2.1 Maximum Subarray Sum . . . . . . . . . . . . . . . . . . . . . . 33
3.2.2 Two Queens Problem . . . . . . . . . . . . . . . . . . . . . . . . . 35
3.3 Code Optimization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
3.3.1 Compiler Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
3.3.2 Processor Features . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
ix
x Contents
7 Graph Algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
7.1 Basics of Graphs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84
7.1.1 Graph Terminology . . . . . . . . . . . . . . . . . . . . . . . . . . 84
7.1.2 Graph Representation . . . . . . . . . . . . . . . . . . . . . . . . . 86
7.2 Graph Traversal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
7.2.1 Depth-First Search . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
7.2.2 Breadth-First Search . . . . . . . . . . . . . . . . . . . . . . . . . . 91
7.2.3 Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92
7.3 Shortest Paths . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
7.3.1 Bellman–Ford Algorithm . . . . . . . . . . . . . . . . . . . . . . 94
7.3.2 Dijkstra’s Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . 96
7.3.3 Floyd–Warshall Algorithm . . . . . . . . . . . . . . . . . . . . . 98
7.4 Directed Acyclic Graphs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100
7.4.1 Topological Sorting . . . . . . . . . . . . . . . . . . . . . . . . . . 100
7.4.2 Dynamic Programming . . . . . . . . . . . . . . . . . . . . . . . . 102
7.5 Successor Graphs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104
7.5.1 Finding Successors . . . . . . . . . . . . . . . . . . . . . . . . . . . 104
7.5.2 Cycle Detection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105
7.6 Minimum Spanning Trees . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106
7.6.1 Kruskal’s Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . 107
7.6.2 Union-Find Structure . . . . . . . . . . . . . . . . . . . . . . . . . 110
7.6.3 Prim’s Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112
8 Algorithm Design Topics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115
8.1 Bit-Parallel Algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115
8.1.1 Hamming Distances . . . . . . . . . . . . . . . . . . . . . . . . . . 115
8.1.2 Counting Subgrids . . . . . . . . . . . . . . . . . . . . . . . . . . . 116
8.1.3 Reachability in Graphs . . . . . . . . . . . . . . . . . . . . . . . . 118
8.2 Amortized Analysis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118
8.2.1 Two Pointers Method . . . . . . . . . . . . . . . . . . . . . . . . . 119
8.2.2 Nearest Smaller Elements . . . . . . . . . . . . . . . . . . . . . . 121
8.2.3 Sliding Window Minimum . . . . . . . . . . . . . . . . . . . . . 122
8.3 Finding Minimum Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122
8.3.1 Ternary Search . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123
8.3.2 Convex Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . 124
8.3.3 Minimizing Sums . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125
9 Range Queries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127
9.1 Queries on Static Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127
9.1.1 Sum Queries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128
9.1.2 Minimum Queries . . . . . . . . . . . . . . . . . . . . . . . . . . . 129
xii Contents