100% found this document useful (2 votes)
7 views

Competitive Programming 4 Book 1 Steven Halim pdf download

The document provides information about the book 'Competitive Programming 4 Book 1' by Steven Halim, including links for downloading the PDF version and additional resources. It outlines the contents of the book, which covers various aspects of competitive programming, including tips, data structures, and problem-solving paradigms. The document also mentions other related eBooks available for download.

Uploaded by

srytays23
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (2 votes)
7 views

Competitive Programming 4 Book 1 Steven Halim pdf download

The document provides information about the book 'Competitive Programming 4 Book 1' by Steven Halim, including links for downloading the PDF version and additional resources. It outlines the contents of the book, which covers various aspects of competitive programming, including tips, data structures, and problem-solving paradigms. The document also mentions other related eBooks available for download.

Uploaded by

srytays23
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 47

Competitive Programming 4 Book 1 Steven Halim

pdf download

https://ebookmeta.com/product/competitive-
programming-4-book-1-steven-halim/

Download more ebook from https://ebookmeta.com


We believe these products will be a great fit for you. Click
the link to download now, or visit ebookmeta.com
to discover even more!

Competitive Programming 4 Book 1 4th Edition Steven


Halim

https://ebookmeta.com/product/competitive-
programming-4-book-1-4th-edition-steven-halim/

Competitive Programming 4 The Lower Bound of


Programming Contests in the 2020s 4th Edition Steven
Halim

https://ebookmeta.com/product/competitive-programming-4-the-
lower-bound-of-programming-contests-in-the-2020s-4th-edition-
steven-halim-2/

Competitive Programming 4 The Lower Bound of


Programming Contests in the 2020s 4th Edition Steven
Halim

https://ebookmeta.com/product/competitive-programming-4-the-
lower-bound-of-programming-contests-in-the-2020s-4th-edition-
steven-halim/

Good Little Girl 1st Edition Jade West

https://ebookmeta.com/product/good-little-girl-1st-edition-jade-
west/
Rich’s Vascular Trauma Todd E. Rasmussen

https://ebookmeta.com/product/richs-vascular-trauma-todd-e-
rasmussen/

Advances in Corpus Applications in Literary and


Translation Studies Routledge Advances in Translation
and Interpreting Studies 1st Edition Riccardo Moratto
(Editor)
https://ebookmeta.com/product/advances-in-corpus-applications-in-
literary-and-translation-studies-routledge-advances-in-
translation-and-interpreting-studies-1st-edition-riccardo-
moratto-editor/

Philosophical Representation Studies in in Attitudinal


Instrumentalism 1st Edition Ori Simchen

https://ebookmeta.com/product/philosophical-representation-
studies-in-in-attitudinal-instrumentalism-1st-edition-ori-
simchen/

Environmental Ethics Sustainability and Decisions


Literature Problems and Suggested Solutions 1st Edition
Fabio Zagonari

https://ebookmeta.com/product/environmental-ethics-
sustainability-and-decisions-literature-problems-and-suggested-
solutions-1st-edition-fabio-zagonari/

Spells Trouble Sisters of Salem 1 1st Edition P C Cast

https://ebookmeta.com/product/spells-trouble-sisters-of-
salem-1-1st-edition-p-c-cast/
Chosen by the Devil (Unruly Gods Book 2) 1st Edition
Milly Taiden & Kala Aster

https://ebookmeta.com/product/chosen-by-the-devil-unruly-gods-
book-2-1st-edition-milly-taiden-kala-aster/
This is the 100% identical eBook (PDF) version of CP4 Book 1
that was released on 19 July 2020
Please read https://cpbook.net/errata
for the latest known updates to this PDF
c Steven, Felix, Suhendry

ii
Contents

Forewords for CP4 vii

Testimonials of CP1/2/3 xiii

Preface for CP4 xv

Authors’ Profiles xxvii

Abbreviations xxix

1 Introduction 1
1.1 Competitive Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.2 The Competitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.2.1 International Olympiad in Informatics (IOI) . . . . . . . . . . . . . . 3
1.2.2 International Collegiate Programming Contests (ICPC) . . . . . . . . 4
1.2.3 Other Programming Contests . . . . . . . . . . . . . . . . . . . . . . 6
1.3 Tips to be Competitive . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
1.3.1 Tip 1: Type Code Faster! . . . . . . . . . . . . . . . . . . . . . . . . 6
1.3.2 Tip 2: Quickly Identify Problem Types . . . . . . . . . . . . . . . . . 8
1.3.3 Tip 3: Do Algorithm Analysis . . . . . . . . . . . . . . . . . . . . . . 10
1.3.4 Tip 4: Master Programming Languages . . . . . . . . . . . . . . . . . 15
1.3.5 Tip 5: Master the Art of Testing Code . . . . . . . . . . . . . . . . . 18
1.3.6 Tip 6: Practice and More Practice . . . . . . . . . . . . . . . . . . . 21
1.3.7 Tip 7: Team Work (for ICPC) . . . . . . . . . . . . . . . . . . . . . . 22
1.4 Getting Started: The Easy Problems . . . . . . . . . . . . . . . . . . . . . . 23
1.4.1 Anatomy of a Programming Contest Problem . . . . . . . . . . . . . 23
1.4.2 Typical Input/Output Routines . . . . . . . . . . . . . . . . . . . . . 23
1.4.3 Time to Start the Journey . . . . . . . . . . . . . . . . . . . . . . . . 26
1.4.4 Getting Our First Accepted (AC) Verdict . . . . . . . . . . . . . . . 27
1.5 Basic String Processing Skills . . . . . . . . . . . . . . . . . . . . . . . . . . 31
1.6 The Ad Hoc Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
1.7 Solutions to Non-Starred Exercises . . . . . . . . . . . . . . . . . . . . . . . 41
1.8 Chapter Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51

2 Data Structures and Libraries 53


2.1 Overview and Motivation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
2.2 Linear DS with Built-in Libraries . . . . . . . . . . . . . . . . . . . . . . . . 55
2.2.1 Array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
2.2.2 Special Sorting Problems . . . . . . . . . . . . . . . . . . . . . . . . . 59
2.2.3 Bitmask . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
2.2.4 Big Integer (Python & Java) . . . . . . . . . . . . . . . . . . . . . . . 66

iii
CONTENTS c Steven, Felix, Suhendry

2.2.5 Linked Data Structures . . . . . . . . . . . . . . . . . . . . . . . . . . 69


2.2.6 Special Stack-based Problems . . . . . . . . . . . . . . . . . . . . . . 71
2.3 Non-Linear DS with Built-in Libraries . . . . . . . . . . . . . . . . . . . . . 78
2.3.1 Binary Heap (Priority Queue) . . . . . . . . . . . . . . . . . . . . . . 78
2.3.2 Hash Table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
2.3.3 Balanced Binary Search Tree (bBST) . . . . . . . . . . . . . . . . . . 84
2.3.4 Order Statistics Tree . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
2.4 DS with Our Own Libraries . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
2.4.1 Graph . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
2.4.2 Union-Find Disjoint Sets . . . . . . . . . . . . . . . . . . . . . . . . . 99
2.4.3 Fenwick (Binary Indexed) Tree . . . . . . . . . . . . . . . . . . . . . 104
2.4.4 Segment Tree . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114
2.5 Solution to Non-Starred Exercises . . . . . . . . . . . . . . . . . . . . . . . . 124
2.6 Chapter Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127

3 Problem Solving Paradigms 129


3.1 Overview and Motivation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129
3.2 Complete Search . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130
3.2.1 Iterative Complete Search . . . . . . . . . . . . . . . . . . . . . . . . 131
3.2.2 Recursive Complete Search . . . . . . . . . . . . . . . . . . . . . . . . 135
3.2.3 Complete Search Tips . . . . . . . . . . . . . . . . . . . . . . . . . . 139
3.2.4 Complete Search in Programming Contests . . . . . . . . . . . . . . . 143
3.3 Divide and Conquer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148
3.3.1 Interesting Usages of Binary Search . . . . . . . . . . . . . . . . . . . 148
3.3.2 Ternary Search . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152
3.3.3 Divide and Conquer in Programming Contests . . . . . . . . . . . . . 153
3.4 Greedy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155
3.4.1 Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155
3.4.2 Greedy Algorithm in Programming Contests . . . . . . . . . . . . . . 161
3.5 Dynamic Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164
3.5.1 DP Illustration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164
3.5.2 Classical Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173
3.5.3 Non-Classical Examples . . . . . . . . . . . . . . . . . . . . . . . . . 184
3.5.4 Dynamic Programming in Programming Contests . . . . . . . . . . . 187
3.6 Solution to Non-Starred Exercises . . . . . . . . . . . . . . . . . . . . . . . . 190
3.7 Chapter Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191

4 Graph 193
4.1 Overview and Motivation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193
4.2 Graph Traversal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195
4.2.1 Overview and Motivation . . . . . . . . . . . . . . . . . . . . . . . . 195
4.2.2 Depth First Search (DFS) . . . . . . . . . . . . . . . . . . . . . . . . 195
4.2.3 Breadth First Search (BFS) . . . . . . . . . . . . . . . . . . . . . . . 197
4.2.4 Finding Connected Components (Undirected Graph) . . . . . . . . . 198
4.2.5 Flood Fill (Implicit 2D Grid Graph) . . . . . . . . . . . . . . . . . . 199
4.2.6 Topological Sort (Directed Acyclic Graph) . . . . . . . . . . . . . . . 200
4.2.7 Bipartite Graph Check (Undirected Graph) . . . . . . . . . . . . . . 202
4.2.8 Cycle Check (Directed Graph) . . . . . . . . . . . . . . . . . . . . . . 203
4.2.9 Finding Articulation Points and Bridges (Undirected Graph) . . . . . 205
4.2.10 Finding Strongly Connected Components (Directed Graph) . . . . . . 208

iv
CONTENTS c Steven, Felix, Suhendry

4.2.11 Graph Traversal in Programming Contests . . . . . . . . . . . . . . . 211


4.3 Minimum Spanning Tree (MST) . . . . . . . . . . . . . . . . . . . . . . . . . 215
4.3.1 Overview and Motivation . . . . . . . . . . . . . . . . . . . . . . . . 215
4.3.2 Kruskal’s Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . 215
4.3.3 Prim’s Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217
4.3.4 Other Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . 218
4.3.5 MST in Programming Contests . . . . . . . . . . . . . . . . . . . . . 221
4.4 Single-Source Shortest Paths (SSSP) . . . . . . . . . . . . . . . . . . . . . . 223
4.4.1 Overview and Motivation . . . . . . . . . . . . . . . . . . . . . . . . 223
4.4.2 On Unweighted Graph: BFS . . . . . . . . . . . . . . . . . . . . . . . 223
4.4.3 On Weighted Graph: Dijkstra’s . . . . . . . . . . . . . . . . . . . . . 227
4.4.4 On Small Graph (with Negative Cycle): Bellman-Ford . . . . . . . . 234
4.4.5 SSSP in Programming Contests . . . . . . . . . . . . . . . . . . . . . 237
4.5 All-Pairs Shortest Paths (APSP) . . . . . . . . . . . . . . . . . . . . . . . . 241
4.5.1 Overview and Motivation . . . . . . . . . . . . . . . . . . . . . . . . 241
4.5.2 Floyd-Warshall Algorithm . . . . . . . . . . . . . . . . . . . . . . . . 242
4.5.3 Other Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . 244
4.5.4 APSP in Programming Contests . . . . . . . . . . . . . . . . . . . . . 246
4.6 Special Graphs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249
4.6.1 Directed Acyclic Graph . . . . . . . . . . . . . . . . . . . . . . . . . . 249
4.6.2 Tree . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255
4.6.3 Bipartite Graph . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 257
4.6.4 Eulerian Graph . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 260
4.6.5 Special Graphs in Programming Contests . . . . . . . . . . . . . . . . 263
4.7 Solution to Non-Starred Exercises . . . . . . . . . . . . . . . . . . . . . . . . 267
4.8 Chapter Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 270

Bibliography 276

v
CONTENTS c Steven, Felix, Suhendry

vi
Forewords for CP4

Bill Poucher
Introduction
In 1970, the Texas A&M UPE Honor Society hosted the first university competitive pro-
gramming competition in the history of the ICPC. The first Finals was held in 1977 in
Atlanta in conjunction with the Winter Meeting of the ACM Computer Science Conference.
The ICPC International Collegiate Programming Contest hosted regional competitions at
643 sites in 104 countries for 59 000 team members and their 5043 coaches from over 3400
universities that span the globe. The top 135 teams of three will advance to the ICPC World
Finals in Moscow hosted by MIPT scheduled for June 2021.
ICPC alumni number over 400,000 worldwide, many playing key roles in building the
global digital community for many decades. The ICPC is the root of competitive program-
ming that reaches out through the global digital community to persons from all cultures and
in increasingly-younger generations.
The UVa Online Judge opened the doors for online competition and access to ICPC prob-
lems under the direction of Professor Miguel Ángel Revilla. Three of the star-studded team
are Steven Halim, Felix Halim, and Suhendry E↵endy, authors of Competitive Programming
4, Book 1 and Book 2. Their work will be honored at the ICPC World Finals in Moscow
hosted by MIPT with a special award from the ICPC Foundation.

Competitive Programming
What is competitive programming and why should you get involved? First and foremost, it’s
a mind sport. It more fully develops your algorithmic reasoning skills and bridges the gap
between theory and application in bite-sized chunks. Full participation develops problem-
solving intuition and competence. Get ready for the Digital Renaissance that will shape
your world in the coming decades. To understand the landscape, it is important to shape
your mind beyond a swarm of buzzwords. Do it as a team sport.

How do we get started?


Start with Competitive Programming 4, Book 1 and Book 2. Start with Book 1 first :). The
authors are seasoned competitive programming experts who have dedicated decades of work
to help at all levels of the sport.
In parallel, engage in a culture that develops habits excellence. You are the first genera-
tion that has never been disconnected. Being connected is best when we bind our strengths
together in common cause. Do that and prepare to meet the challenges that will define your
generation.
Life needs you. We are born to compete. We compete best when we compete together, in
good faith, in goodwill, and with good deeds. When you come to college, consider the ICPC

vii
FOREWORDS Bill Poucher

and the new program ICPC University Commons that will provide a spectrum of activities
that happen outside of the classroom. You can visit https://icpc.global for details.

Why get started?


Is developing your problem-solving skills important? Yes. Is preparing for a future engaged
in the global digital community important? Yes. Is following T.S. Elliot’s advice that to
fully develop you must go too far? Yes. Do that in competitive programming. Be careful of
pursuits that are not reversible.
Is competitive programming practical? Aristotle asserted that there is nothing more
practical than engaging in mental activities and reflections which have their goal in them-
selves and take pace for their own sake. Let me recommend that you engage your spirit in
building a more beautiful world. In the immense scope of life, abundant small kindnesses
make a di↵erence. Find friends with common interest and embrace this cycle:

Repeat for a lifetime: Study; Practice; Rehearse; Dress Rehearse; Perform.

It works for athletes.


It works for musicians.
It works for all performance arts.
It will work for you.

Best, Bill
Dr. William B. “Bill” Poucher, Ph.D., ACM Fellow
Professor of Computer Science, Baylor University
Executive Director, ICPC International Collegiate Programming Contest
President, ICPC Foundation
July 13th, 2020.

viii
FOREWORDS Miguel Revilla Rodrı́guez

Miguel Revilla Rodrı́guez


Almost 20 years ago (on November 11th , 2003, to be precise), my father (Miguel Ángel
Revilla) received an e-mail with the following message:

“I should say in a simple word that with the UVa Site, you have given birth to
a new CIVILIZATION and with the books you write (he meant “Programming
Challenges: The Programming Contest Training Manual” [53], coauthored with
Steven Skiena), you inspire the soldiers to carry on marching. May you live long
to serve the humanity by producing super-human programmers.”

What, in my father’s words, was “clearly an exaggeration”, caused some thinking. And it’s
not a secret that thoughts can easily lead to dreams. His dream was to create a community
around the project he had started, as part of is teaching job at the University of Valladolid,
Spain, that gathered people from all around the world working together towards the same
ideal, the same quest. With a little searching, on the primitive Internet of the first years of
our century, a whole online community of excellent users and tools, built around the UVa
site, came to light.
The website Methods to Solve 1 , created by a very young student from Indonesia, was one
of the most impressive among them. There was the result of the hard work of a real genius
of algorithms and computer science. The seed was planted to believe that the dream could
come true. Moreover, it was not only that the leaves of that growing tree were a perfect
match, but the root of both projects were exactly the same: to serve the humanity. That
young student, the author of the e-mail and the website that put my father to dream, was
Steven Halim. Later he would discover that Steven was not alone in his quest, as his younger
brother, Felix, shared his view, his interests, and his extraordinary capabilities.
After 15 years of fruitful collaboration and, more important, friendship with Steven and
Felix, my father sadly passed away in 2018. His work, and his dreams, now belong to us,
the next generation. This book is the living proof that the dream has become true.
“I can’t imagine a better complement for the UVa Online Judge”, are my father’s words.
Now, with this fourth version of Competitive Programming in my hands, I can add that I
can’t imagine the very existence of the Online Judge without this book. Both projects have
grown in parallel and are, no doubt, perfect complements and companions to each other.
By practicing and mastering most programming exercises in this book, the reader can learn
how to solve hundreds of tasks and find a place in the top 500 best Online Judge coders.
You have in your hands over 2000 (yes, two thousand!) selected, classified, and carefully
commented problems from the Online Judge.
The authors, in the past two decades, have grown from contestants, to coaches and,
finally, masters in the art of competitive programming. They perfectly know every curve
and crossroad in that long path, and they can put themselves in the skins of the young
IOI contestant, the ICPC newcomer or the seasoned coach, speaking to each in their own
language. This book is, for that very reason, the perfect reading for all of them. No matter
if you are starting as a competitive programmer in your local IOI, or are coaching in the
next ICPC World Finals, no doubt this IS the book for you.

1
Please visit https://cpbook.net/methodstosolve

ix
FOREWORDS Miguel Revilla Rodrı́guez

I love movies, I adore classic movies, and I know that what I’m watching is a masterpiece,
when, after the film ends, I can’t wait to start all over again. In Steven and Felix own words
“the book is not meant to be read once, but several times”. And you will find that same
feeling, not only because the authors recommend it, but because you will be anxious to read
and re-read it as, like in the greatest movies, you will find something new and amazing each
time. This book is, by that logic, a masterpiece.
I also have the great honor of being the Spanish language translator of this book. Trans-
lating requires a very meticulous process of converting the words while keeping the spirit.
You have to think as the author would think, and have to perfectly understand not only
what the author is saying, but also what the author is meaning. It is a handcrafting exer-
cise. Having gone forth and back through this text hundreds of times, I have enjoyed every
concept, every new idea, and every tip, not only by what is written in it, but also by what
it wants to achieve. The quest of making better programmers and, behind that, the quest
of serving humanity. This book is, indeed, a truly masterpiece.
Once you’ve read this book several times, you will realize how much a better programmer
you are but, believe it or not, you will realize that you are also a happier person.
Miguel Revilla Rodrı́guez (Miguel Jr)
Online Judge Manager
https://onlinejudge.org
July 1st, 2020, Valladolid.

x
FOREWORDS Fredrik Niemelä

Fredrik Niemelä
I got my first physical copy of this book from Steven at IOI 2012 in Italy. Like so many other
computer scientists, he has a great sense of humor, and named it “Competitive Programming:
Increasing the Lower Bound of Programming Contests.” It was the second edition of the
book and already twice the size of the first edition. Packed with practical advice, it was well-
suited to get beginners started and had useful material for the more seasoned algorithmist.
Steven and Felix’s vision for their book was to teach everybody how to program (As
Gusteau from Ratatouille would put it: “Tout le monde peut programmer”). I had a similar
vision, but instead of writing a book, we created Kattis. “Competitive Programming” and
Kattis share this motivating principle: to make learning computer science and programming
accessible for everyone. In that sense, they are like two of many pieces in the same puzzle.
Kattis is an online tool for teaching computer science and programming, which relies on
a curated library of programming tasks. I managed to convince Steven that he should try
using Kattis for some of his teaching activities. Over the years he has moved from using
Kattis, to pushing us to improve Kattis, to adding high-quality content to Kattis.
From years of teaching algorithms and using similar systems that preceded Kattis, we
learned that the quality of the problems, and their absolute correctness, are paramount for
learning outcomes. So, this is where we put extra e↵ort into Kattis. (If you ever felt that
it’s too much work to add problems to Kattis, this is why). What we did back then is now
standard practice—both the ICPC and IOI use the same kinds of methods for their finals.
In this fourth edition (more than twice as large as the second edition!), Steven and Felix,
now joined by co-author Suhendry, are using problems from Kattis. We are honored to be
included. Finally, our puzzle pieces are directly connected, and I am very excited about
that.
I hope you will find this book informative and helpful and that you will spend the time
it asks of you. You will not be disappointed.
Fredrik Niemelä
Founder of Kattis
ICPC Contest System Director
IOI Technical Committee Founding Member
https://www.kattis.com
July 11th, 2020.

xi
FOREWORDS Brian Christopher Dean

Brian Christopher Dean


I’ve had the privilege to be part of the competitive programming world for more than three
decades, during which time I’ve seen the field grow substantially in terms of its impact
on modern computing. As director of the USA Computing Olympiad and coach of my
University’s ICPC teams, I have seen firsthand how competitive programming has become a
key part of the global computing talent pipeline - both academia and industry are now filled
with present-day superstars who were formerly superstars in competitive programming.
Just as the world of competitive programming has shown tremendous growth in scope,
depth, and relevance, so too has this text, now in its fourth edition. Earlier editions of
this book provided what I consider to be the gold standard for both an introduction and a
thorough reference to the algorithmic concepts most prevalent in competitive programming.
The same remains true for this edition.
Competitive programming can be a daunting undertaking for the novice student - learn-
ing to code is plenty challenging by itself, and on top of this we add a layer of ”standard”
algorithms and data structures and then another layer of problem-solving insight and tricks.
This text helps the introductory student navigate these challenges in several ways, by its
thoughtful organization, extensive practice exercises, and by articulating ideas both in clear
prose and code. Competitive programming can also be a daunting prospect for the advanced
student due to its rapid pace of evolution - techniques can go from cutting-edge to common-
place in a matter of just a few years, and one must demonstrate not only proficiency but
true mastery of a formidable and ever-expanding body of algorithmic knowledge. With its
comprehensive algorithmic coverage and its extensive listing of ⇡ 3458 categorized problems,
this text provides the advanced student with years of structured practice that will lead to a
high baseline skill level.
I think this is a book that belongs in the library of anyone serious about computing, not
just those training for their first or their hundredth programming competition. Ideas from
competitive programming can help one develop valuable skills and insight - both in theory
and implementation - that can be brought to bear on a wide range of modern computing
problems of great importance in practice. Algorithmic problem solving is, after all, truly
the heart and soul of computer science! These types of problems are often used in job
interviews for a good reason, since they indicate the type of prospective employee who has
a skill set that is broadly applicable and that can adapt gracefully to changes in underlying
technologies and standards. Studying the concepts in this text is an excellent way to sharpen
your skills at problem solving and coding, irrespective of whether you intend to use them in
competition or in your other computational pursuits.
I’ve thoroughly enjoyed reading successive drafts
of this updated work shared with me by the au-
thors at recent IOIs, and I commend the authors on
the impressive degree to which they have been able
extend the scope, clarity, and depth of an already-
remarkable text.
Brian Christopher Dean
Professor and Chair
Division of Computer Science, School of Computing
Clemson University, Clemson, SC, USA
Director, USA Computing Olympiad
July 5th, 2020
http://www.usaco.org/

xii
Testimonials of CP1/2/3

“Competitive Programming 3 has contributed immensely to my understanding of data


structures & algorithms. Steven & Felix have created an incredible book that thoroughly
covers every aspect of competitive programming, and have included plenty of practice
problems to make sure each topic sinks in. Practicing with CP3 has helped me nail job
interviews at Google, and I can’t thank Steven & Felix enough!”
— Troy Purvis, Software Engineer @ Google.
“Steven and Felix are passionate about competitive programming. Just as importantly,
they are passionate about helping students become better programmers. CP3 is the result:
a dauntless dive into the data structures, algorithms, tips, and secrets used by competitive
programmers around the world. Yet, when the dust settles on the book, the strongest
sillage is likely to be one of confidence—that, yes, this stu↵ is challenging, but that you can
do it.” — Dr. Daniel Zingaro, Associate Professor Teaching Stream,
University of Toronto Mississauga.
“CP-Book helped us to train many generations of ICPC and IOI participants for Bolivia.
It’s the best source to start and reach a good level to be a competitive programmer.” —
Jhonatan Castro, ICPC coach and Bolivia IOI Team coach,
Universidad Mayor de San Andrés, La Paz, Bolivia.
“Reading CP3 has been a major contributor to my growth, not just as a competitive
programmer, but also as a computer scientist. My entire approach to problem solving has
been improved by doing the exercises in the book; my passion for the art of problem
solving, especially in contest environments, has been intensified. I now mentor several
students using this book as a guide. It is an invaluable resource to anyone who wants to be
a better problem solver.” — Ryan Austin Fernandez, Assistant Professor,
De La Salle University, Manila, Philippines.
“I rediscovered CP3 book on 2017-2019 when I come back to Peru after my master in
Brazil, I enjoyed, learned and solved many problems, more than during my undergraduate,
coaching and learning together in small group of new students that are interesting in
competitive programming. It kept me in a constantly competition with them, at the end
they have solved more problems than me.” — Luciano Arnaldo Romero Calla,
PhD Student, University of Zurich.
“CP1 helped my preparation during national team training and selection for participating
the IOI. When I took the competitive programming course in NUS, CP2 book is
extensively used for practice and homework. The good balance between the programming
and theoretic exercises for deeper understanding in the book makes CP book a great book
to be used for course references, as well as for individual learning. Even at the top
competitive programming level, experts can still learn topics they have not learnt before
thanks to the rare miscellaneous topics at the end of the book.”
— Jonathan Irvin Gunawan, Software Engineer, Google.

xiii
TESTIMONIALS Various Book Readers

“Dr. Steven Halim is one of the best professors I have had in NUS. His intuitive
visualizations and clear explanations of highly complex algorithms make it significantly
easier for us to grasp difficult concepts. Even though I was never fully into Competitive
Programming, his book and his teaching were vital in helping me in job interviews and
making me a better coder. Highly recommend CP4 to anyone looking to impress in
software engineering job interviews.” — Patrick Cho, Machine Learning Scientist, Tesla.

“Flunked really hard at IOI 2017, missing medal cuto↵ by 1 place. Then at the beginning
of 2018 Steven Halim gave me a draft copy of CP3.1 / CP4 and I ended up getting a gold
medal!” — Joey Yu, Student, University of Waterloo, SWE Intern at Rippling,
IOI 2018 Gold Medalist.

“As a novice self-learner, CP-book helped me to learn the topics in both fun and
challenging ways. As an avid and experienced CP-er, CP-book helped me to find a
plentiful and diverse problems. As a trainer, CP-book helped me to plan ahead the
materials and tactical strategies or tricks in competition for the students. As the person
ever in those three di↵erent levels, I must e↵ortlessly say CP-book is a must-have to being
a CP master!” — Ammar Fathin Sabili, PhD Student, National University of Singapore.

“I’ve been in CP for three years. A rookie number for all the competitive programmers out
there. I have a friend (still chatting with him today) who introduced me to this book. He’s
my roommate on our National Training Camp for IOI 2018’s selection. I finally get a grab
of this book in early 2019. To be honest I’m not the ‘Adhoc’ and good at ‘Math’ type of
CP-er. I love data structures, graph (especially trees) And this CP3 book. Is a leap of
knowledge. No joke. I met Dr Felix when I was training in BINUS, I also met Dr Steven
when I competed in Singapore’s NOI and one of my unforgettable moment is, this legend
book got signed by its two authors. Even tho the book is full of marks and stains, truly
one of my favorite. Kudos for taking me to this point of my life.”
— Hocky Yudhiono, Student, University of Indonesia.

“I bought CP3 on 7th April 2014 on my birthday as a gift for myself and it has been the
most worth-it 30USD spent by me on any educational material. In the later years, I was
able to compete in IOI and ICPC WF. I think CP3 played a very big factor in igniting the
interest and providing a strong technical foundation about all the essential topics required
in CP.” — Sidhant Bansal, Student, National University of Singapore.

“I have always wanted to get involved in competitive programming, but I didn’t know how
and where to get started. I was introduced to this book while taking Steven’s companion
course (CS3233) in NUS as an exchange student, and I found the book to be really helpful
in helping me to learn competitive programming. It comes with a set of Kattis exercises as
well. This book provides a structured content for competitive programming, and can be
really useful to anyone ranging from beginners to experts. Just like CLRS for algorithms,
CP is THE book for competitive programming.” — Jay Ching Lim,
Student, University of Waterloo.

“My memories about CP3 is me reading it in many places, the bus, my room, the library,
the contest floor...not much time had passed since I start in competitive programming
reading CP3 until I got qualified to an ICPC World Final.”
— Javier Eduardo Ojeda Jorge, ICPC World Finalist, Universidad Mayor de San Simón,
Software Engineer at dParadig, Chile

xiv
Preface for CP4

This Competitive Programming book, 4th edition (CP4) is a must have for every compet-
itive programmer. Mastering the contents of this book is a necessary (but admittedly not
sufficient) condition if one wishes to take a leap forward from being just another ordinary
coder to being among one of the world’s finest competitive programmers.
Typical readers of Book 1 (only) of CP4 would include:

1. Secondary or High School Students who are competing in the annual International
Olympiad in Informatics (IOI) [31] (including the National or Provincial Olympiads)
as Book 1 covers most of the current IOI Syllabus [16],
2. Casual University students who are using this book as supplementary material for
typical Data Structures and Algorithms courses,
3. Anyone who wants to prepare for typical fundamental data structure/algorithm part
of a job interview at top IT companies.

Typical readers of both Book 1 + Book 2 of CP4 would include:

1. University students who are competing in the annual International Collegiate Program-
ming Contest (ICPC) [57] Regional Contests (including the World Finals) as Book 2
covers much more Computer Science topics that have appeared in the ICPCs,
2. Teachers or Coaches who are looking for comprehensive training materials [21],
3. Anyone who loves solving problems through computer programs. There are numerous
programming contests for those who are no longer eligible for ICPC, including Google
CodeJam, Facebook Hacker Cup, TopCoder Open, CodeForces contest, Internet Prob-
lem Solving Contest (IPSC), etc.

Prerequisites
This book is not written for novice programmers so that we can write much more about
Competitive Programming instead of repeating the basic programming methodology con-
cepts that are widely available in other Computer Science textbooks. This book is aimed
at readers who have at least basic knowledge in programming methodology, are familiar
with at least one of these programming languages (C/C++, Java, Python, or OCaml) but
preferably more than one programming language, have passed (or currently taking) a basic
data structures and algorithms course and a discrete mathematics course (both are typically
taught in year one of Computer Science university curricula or in the NOI/IOI training
camps), and understand simple algorithmic analysis (at least the big-O notation). In the
next subsections, we will address the di↵erent potential readers of this book.

xv
Another Random Scribd Document
with Unrelated Content
Another treatment of cheesecloth, showing a poppy design that is
taken from the cut out band pasted at the top of a gray striped wall
paper
Views of an old-fashioned bedroom that is finished in dull grayish blue. Such heavy furniture would
appear uncomfortably bulky in any smaller room
The carpet helps to make the room homelike, with bare floors the height and size of the room would be
more apparent
A short length of cretonne hanging between two sill length curtains
may be used instead of a valance
The cut out paper border goes well with a shaped valance and side
curtains. The valance is hung on a projecting frame
The Japanese design is repeated on the gathered valance of the curtains, chairs
and the table-cover. Several original stencils of butterflies are framed as
decorations
Ivory white is always a satisfactory color for the woodwork of most bedrooms. Here it takes the place of
wall paper
One article of bedroom furniture that should not be neglected is a bedside cabinet on which an electric
light may be placed. These twin beds are of gray ash with a natural finish
An example of several of the uses for stenciled borders is shown here in
this desk corner. The room makes good use of wicker furniture and
bungalow rugs
The Problem of the Bathroom
Only a few years ago, sanitary conveniences, which were very crude
when compared with those of to-day, were considered luxuries; to-
day they are necessities, demanded for our physical comfort and
welfare. The old-fashioned Saturday tubbing was a much dreaded
and messy event; but with sanitary house plumbing, bathing became
a pleasure and a valuable adjunct to good health. It is, therefore,
interesting to note the treatments of the present bathroom.
The average house to-day contains at least two bathrooms, the
simplest equipment being a water-closet, lavatory, and tub, the two
latter fixtures supplied with hot and cold water supply pipes. From
these three fixtures of the simplest kind, installed in a room not
smaller than 5 ft. 6 in. × 6 ft. 6 in., we may enlarge the scheme to
contain a shower-bath, with floor receptor to catch the water, a sitz
or foot-bath, double lavatories, if for the owner's bath, with marble
or porcelain pier slabs for toilet articles. These fixtures may be
simple in pattern, of enameled iron or of porcelain or marble, in a
room having tile or marble floors and wainscot. There are, happily,
inexpensive fixtures of good quality that are just as efficient as the
most expensive ones, and the plainer the lines of the fixtures the
more beautiful they will appear in the finished bath; heavy
ornamentation in color or molded design should be avoided—it is not
so easily kept clean, nor is it so beautiful.
In many houses having but one servant, a separate bath is provided
for her use, and in a house costing $8,000 it is customary to provide
a private bath connecting with the owner's chamber, as well as a
general bath for the family and guests, and a servants' bath in the
attic. The importance the bath and sanitary plumbing have attained
is shown by the fact that seven or eight per cent of the cost of a
house is taken for plumbing, and in houses costing from $8,000 to
$15,000, three bathrooms are installed.
Two oval lavatories are generally representative of the latest convenience for the modern bathroom

The model servants' bath should have a floor of small hexagonal white, unglazed
tile with hard plastered walls, above a sanitary base, painted with four coats of
moisture-resisting paint and equipped with a five-foot enameled iron tub, quiet
syphon-jet closet, with oak seat and tank, and a plain pattern enameled iron
lavatory. A medicine closet should be built in the wall over it, having a mirror set
in the door. The fixtures cannot be properly set in an area less than 5 ft. 6 in. ×
6 ft. 6 in., and 5 ft. 6 in. × 7 ft. 6 in. would be much better.
The owner's bath is largely a matter of personal taste and cost. Usually this has
a floor of 2 in. white, unglazed hexagonal tile, with a 4 × 6 in. white glazed tile
for walls, with cap and sanitary base, marble thresholds and plinth blocks. The
height of the wainscot is optional; but 4 ft. 6 in. is usual, with the walls and
ceilings above it oil painted. The room should not be smaller than 8 ft. × 10 ft.
and may open from the owner's chamber or dressing-room. Its equipment
usually comprises two lavatories of vitreous china, placed at least six inches
apart, unless a double lavatory is used in one slab, over which may be a
medicine closet built into the wall with mirror door set in; the bathroom door
should have a full-length mirror. In the illustration that appears on page 95 is
shown a silent syphon-jet closet with low-down tank finished in mahogany. The
"low-down combinations," as they are called, are made in oak, cherry,
mahogany, and white enamel. The tub should be at least 5 ft. long, of enameled
iron or porcelain, finished on both sides if enameled, and supported on porcelain
block feet, with standing waste and mixing cocks. The tub must be set far
enough from the wall to permit cleaning.
The great problem in planning a bathroom is not to sacrifice all warmth and color to sanitation. At
present there are washable and sanitary papers made by the decorators that are very proper for
bathroom furnishings. The seagull design in blues and greens and the kingfisher in a brilliant, cool green
are both suitable and attractive
The upper left hand plan shows excellent spacing and good arrangement combining all the desirable
features and conveniences of a bathroom. The upper right hand plan shows a desirable arrangement for
minimum space. The lower left hand plan shows a modern idea of dividing the bathroom into two
sections. In the lower right hand plan there is a complete equipment, well arranged, but with few more
than the necessary conveniences

Every fitting or exposed pipe in the bathroom should be nickel-plated. The


shower may be installed over the tub, as in one of the illustrations, or made a
separate fixture with a floor receptor to drain off the water. It may be inclosed
with a cotton duck curtain, which is more agreeable to the body than rubber or
marble slabs. The merits of each fixture and its equipment I shall leave to the
reader, because these things he can readily determine for himself; but the
arrangements and number of fixtures required must be considered—the quality
is a matter of price. The general bathroom of a house should be similar to the
owner's—in some cases it is divided into two compartments, as shown in one
plan, with the water-closet by itself—permitting independent use.
One especial convenience that should not be neglected is the shower with the duck curtain arranged over
the top. For bathroom floors the hexagonal tile is very suitable and enduring

In homes costing from $15,000 up, the number of bathrooms is in proportion to


the number of occupants. Every room may have a connecting bath with tile floor
and wainscot, completely equipped—in such a case the visit of a guest is not
fraught with hasty skirmishes to the nearest bath, perhaps only to retreat, and
wait and listen for an opportunity to use it.
Plumbing fixtures are made in many materials; the most popular of these, on
account of durability and cost, is cast iron with an enamel glaze fused on the
iron. This ware will stand hard usage, is not easily fractured, does not craze, and
therefore holds its color. The vitreous china ware is, I think, more appropriate for
bathrooms finished in tile, because the materials, being similar, are in harmony,
while the enameled iron is not quite as heavy or substantial looking when used
with tile. Vitreous china is potter's clay, properly fired, with a vitreous glaze
baked on; porcelain is similar and their cost is about the same, except that this
increases rapidly with the larger pieces; because fewer perfect fixtures are
obtained. Fixtures cut from solid marble block are the most expensive and their
relative merit with their relation to cost is a question for the owner to determine.
A sunken bathtub is a form of luxury that is desirable but requires low ceiling in the room below, or that
the bathroom be built on the ground floor. This is oftentimes an impossible arrangement

There is little difference between the enameled iron, vitreous china and porcelain
or marble as far as the retention of heat is concerned, or the feeling from bodily
contact. There are in every kiln some fixtures that are not quite perfect; they are
called "seconds," and catalogued as "Class B" goods, with a lower price.
The weight of massive plumbing in a frame dwelling is considerable and will
cause a settlement of the floors unless carefully supported.
The fashions in tubs are many. The usual shape is square at the foot and round
at the head-at the foot are the waste and supply pipes which are made in
several combinations. The double bath cock, which gives hot or cold water or a
mixture of both, is advisable for tubs-the small cup between the faucets is a ring
tray and can be replaced with a soap dish if desired. Most shower-baths have a
shampoo attachment or body spray that can be used instead of the overhead
shower, so that the head and hair are kept dry if desired, and if a shower is not
to be installed this can be provided in the tub.
The bathroom to-day finds great need of such built-in conveniences as wall chests and cupboards. These
should be arranged for at the time the house is built

A tub incased in tile is a perfectly sanitary treatment, and in some cases the tub
has been sunk into the floor a foot and then incased to avoid the high step
necessary to get into the tub. The plunge—sunk in the floor—is an unusual
treatment that permits more freedom of movement than the tub; but the tile,
when wet, is slippery, and I should expect one might carelessly slip in with fatal
results. Roman tubs are alike at each end—with fixtures in the middle of one
side of the rim. Solid porcelain tubs rest on the floor, set into the tile. The ideal
position for the tub, if there is available room, is with the foot against a wall and
ample room on either side to get in or out of it. Tubs are made in lengths
ranging from 4 ft. to 6 feet, and about 30 inches in width over rims.
This room contains the desirable articles and fixtures for the modern bathroom in a
very satisfactory arrangement. There is the latest thing in nickel plumbing and
modern equipment, but at the same time there is an attractiveness that is so
lacking in the cold, hospital type of room
The small hexagonal floor tiles permit sections to be taken up with little difficulty and
replaced at small expense. Wall tile, however, should be made of the larger
rectangular units with a cornice at the top and a rounded surbase that obviates a
sharp angle at the junction with the floor
The lavatory is an important fixture that is made in a great many
varieties. The old-fashioned bowl is obsolete—the oval has taken its
place, though probably the best is the kidney-shaped bowl, as it
permits a free and natural movement of the arms in raising water to
lave the face. The bowl should be at least 14 × 17 inches, in a slab
22 × 32, with a space surrounding the bowl countersunk a little to
form a border that tends to confine the splashed water. All the
fixtures manufactured by responsible concerns are equipped with
nickel-plated faucets, wastes, traps, and supplies that are very
satisfactory; but quite often the plumber who installs the work buys
the fixtures without the selected trimmings and substitutes a
cheaper pattern. Some tubs and lavatories are sold in "A" and "B"
qualities, and it will be to your advantage to select the fixtures with
your architect, who knows the grades and fittings.
All the wood that is in these bathrooms is heavily enameled in white. Both rooms show a good use of
colored tile worked in attractive designs. The room on the left has a mosaic pattern in several shades as
a decoration, while in the right hand room there is a bright border and vertical strips making panels. The
tiles between them are laid in herring-bone fashion. Both tubs are without supports and rest flat on the
floor
This European treatment is particularly effective for the owner's bath, which opens into his bedroom. The
glass doors provide all the lighting necessary and are very decorative with their curved segments

A particularly pleasing treatment is the bath opening from the owner's chamber,
and separated from it by glass partition. This arrangement is good where outside
light cannot be afforded or obtained, and a curtain effectively screens it.
A simple bathroom where waterproof enameled paint was used in several coats instead of a tiled
wainscot, and above this an attractive waterproof paper that suggests tiling. Such an arrangement is
a possibility where tiles may not be purchased

Bath-room accessories should be arranged with care and consist of the


following devices: Plate glass shelves supported on nickel-plated brackets are
the best; towel-racks; toothbrush holders; clothes-brush hangers; clothes
hooks; soap dishes; and soiled towel baskets. Hardware is usually of nickel-
plated tubing screwed into the tile. The accompanying photographs and plans
will illustrate the subject further and are self-explanatory.
A. Raymond Ellis
The Proper Treatment for the
Nursery
Furnishings for the modern child's room, like everything else that
belongs to that important personage, are as complete in the smallest
detail as skill and ingenuity can make them, and every feature of a
well-appointed bedroom may be duplicated in miniature for the
youngsters.
The wall-papers and draperies especially designed for nurseries and
children's rooms are in a way more distinctively juvenile than the
actual pieces of furniture, and are a most important consideration in
fitting out such apartments. If one does not care to go to the
expense of furnishing a nursery completely, paper and curtains that
will leave no doubt as to the identity of the room may be had at
small cost, and from this simple touch the scheme of decorations
and the furniture, to say nothing of the cost, may be indefinitely
extended.
Strictly hygienic parents who scout the idea of wall-paper as being
unhealthy and will have nothing but painted walls in a bedroom are
confronted by a bare expanse that may be sanitary, but is neither
attractive nor interesting for the child. With walls treated in this way
a decorative frieze may be used with good effect. The friezes, which
come in panels varying in depth from fourteen to nineteen and one-
half inches, are printed in gay colors on backgrounds of blue-gray,
ivory-white, drab, and other neutral tones that can be matched
exactly in the color of the walls. The designs include processions of
Noah's ark inhabitants, farmyard animals, chickens and ducks,
Normandy peasants going to market, toy villages with stiff little
soldiers and prim-looking trees, hunting scenes, and a row of Dutch
kiddies indulging in a mad race across the paper.
If wall-paper is used it also matches the background of the frieze,
the paper being either in a solid color or with a figure so
inconspicuous that it gives the impression of a single tone.
One of the new papers for children's rooms is a reproduction of the
quaint Kate Greenaway figures that are quite as fascinating to little
people in these days as they were years ago. The background is a
pale yellow and the figures are printed in rather delicate colors, each
group representing one of the calendar months. The effect is
particularly dainty and the designs are diverting for the children
without becoming tiresome from too great contrast in color. Another
paper that shows groups quite as charming is printed from designs
by Boutet de Monvel, the famous French illustrator of child life.
A new idea, and one that is proving popular, is a decided departure
from the conventional wall-paper, with its figures at regularly
repeated intervals. This consists in first putting on the walls a paper
of solid color to be used as a background for single figures or groups
that are cut from friezes and pasted on to suit one's individual taste.
The figures, of course, must be quite large, in order to be effective,
and in some favorite groups cut from a frieze showing little Dutch
girls and yellow chicks the latter are even larger than life. For
nurseries, when the children are very small, the figures are often
arranged in a frieze just above the foot-board, so that they come on
a line with the child's eye, and are therefore vastly more entertaining
than when placed at the infinite distance of the top of the wall.
Blue and white seems to be the favorite combination of colors for
nursery draperies, and among the all-over patterns are a lot of roly-
poly children picking gigantic daisies on a pale blue ground, and also
a Delft design on a white ground covered with black cross lines that
are far enough apart to give a tiled effect. A number of other colors
and patterns may be had as well as the gay printed borders that
come two strips to a width of the material. When figured wallpaper

You might also like