You are on page 1of 29

Alexandria University

Competitive programming
junior training

Why are we here ?

We are here to prepare ourselves for ACM-ACPC,
ACM-ICPC, TopCoder, Codeforces, Codejam,
Hackercup etc..

We are here to develop better thinking and problem
solving skills.

It’s just like any other sport.

What’s in it for you ?

Better thinking and problem skills

Better coding skills, write neater code

More Knowledge in cs and mathematics


More fun

You get to travel and see new places!

Competition structure


5 hours

A team of 3

A problem set of 10 to 15 problems

one machine


The problems are usually hard

It’s hard to choose which problem to solve

A lot of pressure

Egyptian/Arab/African teams:•

Bad ranks

Last super region

What is your goals ?

The goals

Do better than the best_so_far

Earn a medal at icpc

Teach other people so that they do better than you

Training plan

Two sessions per week.


Usually easy, related to the session’s topic

3-6 problems are discussed in each session

2 hours.

You have to solve those as well.

3-6 problems after each session + Readings

Not much harder than session’s problems.

Training outline

Problem solving techniques

Recursion / Recurrences

Binary search / Ternary search

Graphs basic

Dynamic programming

Various topics in mathematics

Graphs Advanced


String processing

Why we solve ?

Solving problems is the most efficient way to get better at problem
solving, reading solutions or analysis doesn’t give you much boost.

You should solve problems that are slightly harder than your current
level in order to improve, we’ll focus on this during the training.

You should give each problem enough time before you start asking
for hints or looking at solutions, it’s often better not to look at
solutions at all but we don’t have infinite time.

When you solve enough problems you start developing strategies
which are tools that help you solve problems faster

Check some of which I saw during solving problems at :D :D

How do you solve ?


Abstraction is reducing the information about an object to keep only the
information that’s really needed.

Always think using abstractions instead of implementation

While solving a problem your tools while thinking should

Set, sequence, interval, range minimum query, graph...

Not TreeSet, Array, LinkedList, Segment tree, Adjacency matrix...

It’s often better to solve the problem using abstractions only then we can choose
whatever implementation we want, this helps us to see alternate solutions.

One exception is the problem that uses implementation tricks.

We’ll see more of this in the problems.

Strategy: Simplify

Ants [UVa 10714]

Problem: n ants on a horizontal pole of length L cm, each
ant walks at a constant speed of 1 cm, when two ants
collide they both change direction, given the location of
each of the ants on the pole, assign directions to the ants
such that time taken by the all ants to fall from the pole is

as small as possible

as large as possible

output the minimum, maximum time taken by all ants to
fall from the pole

Solution ?

Simplify the problem!

Take a look at smaller inputs

one ant

answers are:

min(p, L-p)

max(p, L-p)

Two ants

Two ants walking in same direction: No conflicts

Two Ants walking in different directions
Collision occurs
Change direction
When ants change direction

First and does what the second was going to do

Second ant does what the first was going to do
It’s as if they just shake hands and keep going

So it does not affect the time taken for all ants to fall


Final answer for n ants

max(min(p0, L-p0),min(p1, L-p1),…min(pn-1, L-pn-1))

max(max(p0, L-p0),max(p1, L-p1),…max(pn-1, Lpn-1))

Here we simplified the problem by looking at a
smaller input size, this is the easiest way of
simplifying a problem, the solution for smaller inputs
helped us to solve larger inputs.

Meet point

You are given n points in 2D (x, y) find a point p (u,
v) such that the sum of the manhattan distances
from p to other points is minimum.

distance((x0, y0), (x1, y1))=|x0 - x1| + |y0-y1|

Simplify ?

Here we don’t simplify the problem by reducing the
input size, we simplify by reducing the
dimensionality, i.e solve on 1D first then solve on

Points at (1, 3, 5, 6, 8)

If we select 2

Sum of distances = 1 + 1 + 3 + 4 + 6
Move one point right 

Sum of distances = 2 + 0 + 2 + 3 + 5
Distance will keep decreasing as we
move towards the median


If number of points is odd, select the median

If number of points is even select one of the two
middle points or any point between them.

Calculating u is independent of calculating v
Calculate u, then calculate v
Running time ? Sort in O(nlgn) and find medians in O(1) 

Or calculate medians in O(n)


Observing independence can help you solve a lot
of problems also can help you reduce the running
time of your program

for example in the previous problem we found that
we can solve the problem for X’s and Y’s


Given an nxn you are required to place n non
attacking rooks such that the ith rook lies in the
rectangle specified by Ri(x0, y0, x1, y1)

Output for each rook it’s location, or output
impossible if it’s impossible to place rooks







More Independence

Choosing a row for a rook is independent from
choosing a column!

Solve for rows (1D)

Solve for columns (1D)

Combine both solutions to recover rook locations

1D version

Set of intervals, select exactly one point point for each

Greedy algorithm: Give each cell to the open interval (i.e
start ≤ cell, and was not given a cell already) with the
smallest end point

If one cell cannot be given to any interval then impossible



Ants [UVa 10714]

Meet point (Own problem)

Rooks (




B. Chip construction (