You are on page 1of 48

Introduction to

Competitive
Programming

What Is Competitive Programming?


Contestants given a few hours to solve a number of
problems
Goal is not to write software or build anything
Goal is to write short code for specific, narrowly-defined
problems
Often require deep, creative logical or mathematical
thinking
Well-researched and well-known in CS
But perhaps not well-known to average programmer
Contestants ranked by number of problems solved, ties

What Is Competitive Programming?

Competitive Programming

Real-World Programming
So Why Bother?

Reason #1: Good Practice


Programming is a skill.
Focused, narrow problems are ideal for deliberate
practice.

What Is Deliberate Practice?


Break down a complex skill into separate parts and
master each part quickly through repetition
Each part must be
Repetitive: short enough to be easily repeatable?
Targeted: simple enough that it can be mastered in a few
sessions?
Incremental: builds up on previously mastered skills?
Immediate feedback: can immediately know if right or
wrong and immediately adjust?

Alternative Ways to Practice


Create projects little repetition, not simple, no
immediate feedback
Contribute to open-source projects not simple, not very
accessible
Tinkering
Good for creativity, learning for the first time
Less good for solidifying learned skills
No specific goals can be a good thing or bad thing

Continue to encourage these competitive programming


is meant to complement rather than replace.

Reason #2: It Tests Essential Skills


Having strong foundations are essential before tackling
complicated challenges of real world.
Programming contests provide a controlled environment
for testing basic skills.
Programming contest: even if its
correct 99% of the time, its
wrong.
If you cannot write the correct
logic for simple program with 50
lines of code, forget about writing
real-world software.

Reason #3: Good for the Long-Term


Programming contests do go far beyond what is strictly
necessary for real-world programming, but some can and
should go there.
Competitive programmers develop these skills.
Learning new things quickly
Abstract thinking
Problem solving

They will be better able to adapt to new technologies, or even


invent new technologies on their own.
Potential source of new researchers

Reason #4: Recognized by the


World
Programming contests are a great way to showcase
Filipino talent to the world.
More technology firms will invest in the Philippines when
they see promising talent.

Reason #5: It Pays


Software companies spend a lot to train employees who
didnt learn enough in school.
A good competitive programmer will have written 100x
more code than the average CS or IT student.
All other things being equal, companies will more likely
hire or give higher salaries to the competitive
programmer.

Reason #6: Its Fun!


This is the most important reason.

The Competitive
Programming Format

What a Problem Looks Like


Problem Statement
Write a short CLI program that does something (no GUI, no
software package)

Runtime Limit
Input Format
Includes guarantees on what input range is (no need to check
if this holds!)

Output Format
Must follow exactly, no extra messages or user prompts

Sample Input

How the Program Is Checked


Judging software runs your program automatically on a secret judge
input (different from sample input).
Output of your program is automatically compared against secret
judge output (different from sample output).
You get one of the following replies

No:
No:
No:
No:
No:
Yes

Compile Error
Runtime Error
Time Limit Exceeded program took too long to run on secret input
Wrong Answer program output did not match secret output
Presentation Error matched except for some spacing or formatting

Common Beginner Errors: Testing If


Input Is In Valid Range
n = int(input())
if 1 <= n && n <= 100:
# solve problem

The input is already guaranteed to be in the given input range

Use this info to analyse if your solution is fast enough or not

Common Beginner Errors: BatchProcessing Multiple Test Cases


No Need to Do This

Just Do This

answers = [];
instances = [];
for i in range(N_CASES):
instances.append(input())
for instance in instances:
answers.append(solve(instance))
for answer in answers:
print(answer)

for i range(N_CASES):
instance = input()
answer = solve(instance)
print(answer)

Common Beginner Errors: Not Doing


Analysis
Not looking at input size and coding up the most
straightforward solution, wasting time for a TLE solution
Submitting without making sure program is correct,
because it is correct on sample input

How to Be a Competitive
Programmer

Step 1: Get Comfortable


With Programming
Practice with an Online Judge
Solve a few hundred problems

URI Online Judge


Has good selection of
problems for beginners
Has replay contests of
past ICPCs
Attractive user interface
and user statistics
Academic tools for
professors and coaches

UVa Online Judge and uHunt

Old and sometimes buggy,


but still popular and has one
of the biggest problem
archives
With problems from past
ICPCs
Best used with

Sorts UVa OJ problems by topic and difficulty (roughly), so


that its easier to find problems appropriate to ones skill level

Codeforces
Currently one of the most popular
Has new contests and problems every week
Problems have editorials
Originally in Russian, but English translation is available
(though sometimes that means the editorials are hard to read)

Can read other peoples submitted code


Can sort problems by topic (not perfect)
Can sort problems by number of solvers

HackerRank
Code challenges organized by topic, from easiest to
hardest
Even includes practical topics not found in most
programming contests
Challenges have editorials
Holds regular contests, some with prizes
Has Java, C++, and Python Tutorials

Project Euler
Math-oriented problems
Carefully designed to enable everyone to gradually
progress from beginner to advanced
Really challenging!

A2 Online Judge
Useful for organizing practice contests quickly, using
existing problems from other online judges
Contests of user verngutz are public, so you can join
our training if you want

Step 2: Learn Standard


Tools and Techniques
Algorithm analysis, data structures, algorithms, problem solving
paradigms

Where and How to Learn?


Get a book
Or, online
Just need a decent
computer and internet
connection

Algorithm Design by Kleinberg and


Tardos

Kevin Wayne created slides here

MIT Open Courseware


6.042J Mathematics for C
omputer Science
6.006 Introduction to Alg
orithms
6.046J Design and Analysi
s of Algorithms

Coursera

Algorithms, Part I from Princeton


Algorithms, Part II from Princeton
Algorithms: Design and Analysis, Part 1 from Stanford
Algorithms: Design and Analysis, Part 2 from Stanford

Topcoder Tutorials
Really focused on
competitive programming

(Partial) List of Topics to Study


Basics
1. Recursion
2. Using Standard Library Data
Structures
(Lists, Stacks, Queues, Priority
Queues, Sets, Maps, Counters)

Problem Solving Paradigms


1. Complete Search, Recursive
Backtracking
2. Greedy
3. Dynamic Programming
4. Divide-and-Conquer
5. Binary Search the Answer
6. Ternary Search

Graphs
1. DFS, Connected Components, Flood
Fill
2. BFS, Shortest Paths, Minimum
Spanning Trees
3. Topological Sort, Cut Vertices and
Edges
4. Euler Paths and Cycles
5. Network Flows, Bipartite Matching

Mathematics and Geometry


1. Number Theory
2. Combinatorics and Probability
3. Vectors, Dot and Cross Product
4. Polygon Containment, Area
5. Convex Hull
6. Sweeping Line Method

Data Structures
1. Bit Vector, Bitwise Operations
2. Union-Find Disjoint-Set
3. Square-root Decomposition,
Segment Trees
4. Binary-indexed Trees
5. KMP Substring Search
6. Suffix Trees and Arrays, AhoCorasick

Miscellaneous, Advanced Topics


1. Combinatorial Games, SpragueGrundy Theory
2. Fast Fourier Transform
3. Heavy-light Decomposition
4. 2SAT
5. Meet-in-the-middle

Finished Learning All of Those?

Good!

Now the real fun starts

Step 3: Practice, Practice,


and Practice!

How to Practice
1. Try to solve problem
2. Stuck? Read editorial (solution explanation)
3. (Re)-study any unfamiliar data structures or
algorithms mentioned in editorial
4. Re-solve
5. Still stuck? Read (but do not copy) other peoples code
and re-solve
6. Repeat same (type of) problem until speed improves
7. Repeat until mastery of wide types of problems

Step 4: Join Online


Contests
Join the ACM-ICPC

Preparing for the ICPC


Practice using old contest problems

ACM-ICPC Live Archive


Google search for ICPC <region>
Easy: ICPC Greater New York
Medium: ICPC Rocky Mountain
Hard: ICPC Northwest Europe
Hardest: ICPC Northeast Europe
List of all regions found on the Baylor University website
American and European contests typically share judge data
and solutions

Some Tips for Training


Your Own Programming
Teams

Tip #1: Integrate Competitive


Programming Into Your
Programming Classes

Competitive programming format is great for practice


Benefits not only those who will compete and represent
your school, but all your students
Dont just test knowledge of terms and facts, give
programming exams
Warning: dont use problems from Online Judges in
graded work (too easy to cheat)

Tip #2: Find Student / Alumni


Assistants
First few years will be very difficult, because you will be
doing all the teaching and guiding
Try to encourage your more successful students to stay
and help teach for even just one year or two
Make sure you do not start from scratch for every batch,
but keep improving based on experience from previous
batches
Improvement will come gradually but surely

Tip #3: Train Regularly


Once a week
Dont just train right before a contest
If you cannot train weekly, try once every two weeks,
then increase frequency as the contest draws nearer
Cannot teach or prepare training sessions every week?
No problem: just give your students some food, a
computer, internet connection, and a space to study or
practice on their own

Tip #4: Use Existing Materials


Hundreds of lecture videos
Hundreds of online tutorials
Thousands of problems from online judges
URI, uHunt, and HackerRank provide structured courses

Tip #5: Encourage Self-Study


Too many topics may not have time to cover them all in
formal training sessions
Self-learning is now very easy with the internet
Truly motivated students will want to self-learn
Just to give you an idea
ACM-ICPC world finalists from ADMU: Kyle, Rico, and David
have more than 1000 solved problems each

Tip #6: Let Students Specialize


(Later On)
Too many topics to be learned and mastered by one
person
College programming contests usually for teams of three
Divide and conquer

Tip #7: Once in A While, Host Your


Own Programming Contests
Can be intra or inter school
Suggestion if you cannot afford to host a contest every
year: organize a small league and take turns hosting
inter-school contests
Dont forget to invite us!

Tip #8: Most Importantly


Give your full support!

You might also like