This document outlines an introduction to competitive programming and problem solving algorithms. It discusses that competitive programming involves writing programs to solve well-known computer science problems quickly. To be successful requires coding quickly, identifying problem types, analyzing time complexity, and extensive practice. The document then covers four basic problem solving paradigms - complete search, divide and conquer, greedy algorithms, and dynamic programming. It provides details on complete search, including that it involves searching the entire solution space and is useful when no clever algorithm exists or the input size is small.
3. Introduction
Fact:
“ACM-‐ICPC
is
a
competitive
programming
contest.”
“Given
a
set
of
well-‐known
problems
in
Computer
Science
your
job
is
to
write
programs
to
solve them
as
quickly as
possible.”
8/29/15 ACM-‐ICPC
2015:
BASIC
PROBLEMS
&
SOLVING
ALGORITHMS 3
4. Introduction
Fact:
“Given
problems
are
already
solved.”
“At
least
the
problem
setter
have
solved
these
problems
before.”
8/29/15 ACM-‐ICPC
2015:
BASIC
PROBLEMS
&
SOLVING
ALGORITHMS 4
5. Introduction
Fact:
“Your
program
is
accepted
if
it
produces
the
same
output
as
the
problem
setter
using
their
secret input
data.”
“The
sample
input
and
output
will
be
given
for
every
single
problem
.”
8/29/15 ACM-‐ICPC
2015:
BASIC
PROBLEMS
&
SOLVING
ALGORITHMS 5
6. Introduction
Fact:
“In
competitive
programming,
your
program
must
also
quickly solve
a
problem
within
the
given
time
limit.”
“The
time
limit
will
always
be
provided
for
every
single
problem.”
8/29/15 ACM-‐ICPC
2015:
BASIC
PROBLEMS
&
SOLVING
ALGORITHMS 6
7. Introduction
To
be
a
competitive
programmer
◦ Code
faster
◦ Quickly
identify
problem
types
◦ Do
algorithm
analysis
(time
complexity)
◦ Master
programming
languages
(C/C++
or
Java)
◦ Master
at
testing
and
debugging
code
◦ Practice
and
more
practice
8/29/15 ACM-‐ICPC
2015:
BASIC
PROBLEMS
&
SOLVING
ALGORITHMS 7
8. Introduction
Keys
to
success
in
identifying
problem
types
◦ Know
problem
solving
paradigms.
◦ Be
familiar
with
basic
algorithms,
their
time
complexities,
and
their
implementations.
◦ Understand
and
know
how
to
use
basic
data
structures.
8/29/15 ACM-‐ICPC
2015:
BASIC
PROBLEMS
&
SOLVING
ALGORITHMS 8
10. Problem
Solving
Paradigms
Four
Basic
Problem
Solving
Paradigms
◦ Complete
Search
◦ Divide
and
Conquer
◦ Greedy
◦ Dynamic
Programming
8/29/15 ACM-‐ICPC
2015:
BASIC
PROBLEMS
&
SOLVING
ALGORITHMS 10
11. Problem
Solving
Paradigms
– Complete
Search
Complete
Search
◦A
method
for
solving
a
problem
by
searching
(up
to)
the
entire
search
space to
obtain
the
desired
solution.
◦Also
known
as
brute
force/tree
pruning
or recursive
backtracking
techniques.
8/29/15 ACM-‐ICPC
2015:
BASIC
PROBLEMS
&
SOLVING
ALGORITHMS 11
12. Problem
Solving
Paradigms
– Complete
Search
Keys
to
develop
a
Complete
Search
solution
1. Identify
the
search
space
which
contains
feasible
solutions.
2. Identify
the
requirement
of
the
desired
solution.
3. Construct
a
feasible
solution.
4. Check
if
a
feasible
solution
matches
the
requirement.
8/29/15 ACM-‐ICPC
2015:
BASIC
PROBLEMS
&
SOLVING
ALGORITHMS 12
13. Problem
Solving
Paradigms
– Complete
Search
Example
of
Complete
Search
(Permutation
Sort)
◦In
the
problem
of
sorting
n integers
ØSearch
space
is
all
possible
permutations
of
n integers.
ØThe
desired
solution
is
a
permutation
a1a2…an such
that
a1 ≤
a2 ≤
… ≤
an.
8/29/15 ACM-‐ICPC
2015:
BASIC
PROBLEMS
&
SOLVING
ALGORITHMS 13
14. Problem
Solving
Paradigms
– Complete
Search
Example
of
Complete
Search
(Permutation
Sort)
◦In
the
problem
of
sorting
n integers
ØA
complete
search
solution
can
be
constructed
by
enumerating
all
permutations
of
n integers
and
then
outputting
the
permutation
in
sorted
order.
ØClearly,
this
solution
has
𝑂 𝑛
! time
complexity.
8/29/15 ACM-‐ICPC
2015:
BASIC
PROBLEMS
&
SOLVING
ALGORITHMS 14
15. Problem
Solving
Paradigms
– Complete
Search
Pros
•Usually
easy
to
come
up
with.
•A
(bug-‐free)
Complete
Search
solution
will
never
cause
Wrong
Answer
(WA)
response
(since
it
explores
the
entire
search
space).
Cons
•A
(naïve)
Complete
Search
solution
may
cause
Time
Limit
Exceeded
(TLE)
response (extremely
slow,
at
least
exponential
running
time).
8/29/15 ACM-‐ICPC
2015:
BASIC
PROBLEMS
&
SOLVING
ALGORITHMS 15
16. Problem
Solving
Paradigms
– Complete
Search
When
to
use
Complete
Search?
◦Use
when
there
is
no
clever
algorithm
available.
ØEX: The
problem
of
generating
all
possible
permutations.
◦Use
when
the
input
size
trends
to
be
very
small
(so
that
it
can
pass
the
time
limit).
8/29/15 ACM-‐ICPC
2015:
BASIC
PROBLEMS
&
SOLVING
ALGORITHMS 16
17. Practice
Problem
UVa #725:
Division
Problem
statement
Find
and
display
all
pairs
of
5-‐digit
numbers
that
between
them
use
the
digits
0
through
9
once
each,
such
that
the
first
number
divided
by
the
second
is
equal
to
an
integer
N,
where
2
≤
N
≤
79.
That
is,
abcde /
fghij =
N,
where
each
letter
represents
a
different
digit.
The
first
digit
of
one
of
the
numerals
is
allowed
to
be
zero,
e.g.
79546
/
01283
=
62;
94736
/
01528
=
62.
Time
limit
:
3s
Iterative
Complete
Search
8/29/15 ACM-‐ICPC
2015:
BASIC
PROBLEMS
&
SOLVING
ALGORITHMS 17
18. Practice
Problem
UVa #725:
Division
Quick
Analysis
§fghij can
only
be
from
01234
to
98765
(≈100k
possibilities).
§For
each
tried
fghij,
abcde is
computed
from
fghij*N
and
then
check
if
all
digits
are
different.
§Since
100k
operations
are
small,
iterative
complete
search
may
pass
the
time
limit.
Time
limit
:
3s
Iterative
Complete
Search
8/29/15 ACM-‐ICPC
2015:
BASIC
PROBLEMS
&
SOLVING
ALGORITHMS 18
19. Problem
Solving
Paradigms
Four
Basic
Problem
Solving
Paradigms
◦ Complete
Search
◦ Divide
and
Conquer
◦ Greedy
◦ Dynamic
Programming
8/29/15 ACM-‐ICPC
2015:
BASIC
PROBLEMS
&
SOLVING
ALGORITHMS 19
20. Problem
Solving
Paradigms
– Divide
and
Conquer
Divide
and
Conquer
(D&C)
◦A
method
for
solving
a
problem
where
we
try
to
make
the
problem
simpler
by
dividing
it
into
smaller
parts
(smaller
sizes)
and
conquering
them.
◦Frequently
used
D&C
algorithms
include
Quick
Sort,
Merge
Sort,
Heap
Sort,
and
Binary
Search.
8/29/15 ACM-‐ICPC
2015:
BASIC
PROBLEMS
&
SOLVING
ALGORITHMS 20
21. Problem
Solving
Paradigms
– Divide
and
Conquer
Keys
to
develop
a
D&C
solution
1. Divide
the
original
problem
into
sub-‐problems
(usually
by
half).
2. Find
sub-‐solutions
for
each
of
these
sub-‐problems.
3. If
required,
combine
sub-‐solutions
to
produce
a
complete
solution
to
the
original
problem.
8/29/15 ACM-‐ICPC
2015:
BASIC
PROBLEMS
&
SOLVING
ALGORITHMS 21
22. Problem
Solving
Paradigms
– Divide
and
Conquer
Example
of
D&C
(Merge
Sort)
◦In
the
problem
of
sorting
n integers
ØDivide
the
problem
into
2
sub-‐problems
each
of
which
sorting
n/2 integers.
ØRecursively
solve
each
of
these
two
sub-‐problems
until
the
sub-‐problem
of
sorting
n/2
=
1
integers
is
encountered.
ØCombine
a
sorted
sequence
from
the
sub-‐solutions.
Ø𝑂 𝑛
log
𝑛 time
complexity.
8/29/15 ACM-‐ICPC
2015:
BASIC
PROBLEMS
&
SOLVING
ALGORITHMS 22
23. Problem
Solving
Paradigms
– Divide
and
Conquer
Pros
•Quite
easy
to
come
up
with
•For
some
problems,
if
applicable,
a
D&C
solution
can
be
pretty
fast
as
it
is
(ideally)
expected
to
reduce
the
size
of
the
problem
by
half
each
time.
Cons
•Time
complexity
is
not
always
good.
•Availability
for
efficient
D&C
solutions
is
hard
to
tell.
8/29/15 ACM-‐ICPC
2015:
BASIC
PROBLEMS
&
SOLVING
ALGORITHMS 23
24. Practice
Problem
UVa #495:
Fibonacci
Freeze
Problem
statement
The
Fibonacci
numbers
(0,
1,
1,
2,
3,
5,
8,
13,
21,
34,
55,
…)
are
defined
by
the
recurrence:
F0 =
0
F1 =
1
Fi =
Fi-‐1 +
Fi-‐2 ,
for
all
𝑖 ≥ 2
Compute
Fi,
where
𝑖 ≤ 5000.
Time
limit
:
3s
First
trial
:
Divide
and
Conquer
8/29/15 ACM-‐ICPC
2015:
BASIC
PROBLEMS
&
SOLVING
ALGORITHMS 24
25. Practice
Problem
UVa #495:
Fibonacci
Freeze
Quick
Analysis
(D&C
Solution)
§Solve
Fi by
recursively
solving
Fi-‐1 and
Fi-‐2.
§If
i =
0,
then
return
Fi =
0.
§If
i =
1,
then
return
Fi =
1.
§Combine
Fi
from
Fi-‐1
+ Fi-‐2.
The
D&C
solution
is
extremely
slow.
The
time
complexity
is
about
𝑂 𝑛
! . It
cannot
pass
the
time
limit
definitely
!!
Time
limit
:
3s
First
trial
:
Divide
and
Conquer
8/29/15 ACM-‐ICPC
2015:
BASIC
PROBLEMS
&
SOLVING
ALGORITHMS 25
26. Problem
Solving
Paradigms
Four
Basic
Problem
Solving
Paradigms
◦ Complete
Search
◦ Divide
and
Conquer
◦ Greedy
◦ Dynamic
Programming
8/29/15 ACM-‐ICPC
2015:
BASIC
PROBLEMS
&
SOLVING
ALGORITHMS 26
27. Problem
Solving
Paradigms
– Greedy
Greedy
◦A
method
for
solving
a
problem
by
picking
locally
optimal
choice
at
each
step
with
the
hope
of
producing
the
optimal
solution.
◦Frequently
used
greedy
algorithms
include
Kruskal’s MST,
Prim’s
MST,
Dijkstra’s SSSPs.
8/29/15 ACM-‐ICPC
2015:
BASIC
PROBLEMS
&
SOLVING
ALGORITHMS 27
28. Problem
Solving
Paradigms
– Greedy
A
problem
that
admits
a
Greedy
solution
must
exhibit
two
properties:
1. Optimal
sub-‐structure:
Ø Optimal
solution
to
the
problem
contains
within
optimal
solutions
to
the
sub-‐problems.
2. Greedy-‐choice
property:
Ø Pick
a
choice
that
seems
best
at
the
moment
and
solve
the
remaining
sub-‐problems.
No
need
to
reconsider
previous
choices.
8/29/15 ACM-‐ICPC
2015:
BASIC
PROBLEMS
&
SOLVING
ALGORITHMS 28
29. Problem
Solving
Paradigms
– Greedy
Keys
to
develop
a
Greedy
solution
1. Determine
the
optimal
sub-‐structure
of
the
problem.
2. Prove
the
greedy-‐choice
property.
8/29/15 ACM-‐ICPC
2015:
BASIC
PROBLEMS
&
SOLVING
ALGORITHMS 29
30. Problem
Solving
Paradigms
– Greedy
Example
of
Greedy
(Selection
Sort)
◦In
the
problem
of
sorting
n integers
ØOptimal
sub-‐structure:
o A’
=
a1a2…an
such
that
a1 ≤
a2 ≤
… ≤
an.
o A’’
=
remove(A’,
ai)
is
still
in
sorted
order.
ØGreedy-‐choice
property:
o Consider
a*
=
min(A’)
as
a
greedy
choice.
o add(remove(A’,
a*),
a*)
is
not
in
sorted
order.
8/29/15 ACM-‐ICPC
2015:
BASIC
PROBLEMS
&
SOLVING
ALGORITHMS 30
31. Problem
Solving
Paradigms
– Greedy
Example
of
Greedy
(Selection
Sort)
◦In
the
problem
of
sorting
n integers
ØIterative
Greedy
Sort(A):
1. A’
=
[]
2. for
i =
1
to
n:
3. a*
=
min(A)
4. add(A’,
a*)
5. remove(A,
a*)
Ø𝑂 𝑛 + 𝑂 𝑛 − 1 + ⋯ 𝑂 1 =
𝑂(𝑛3
) time
complexity.
8/29/15 ACM-‐ICPC
2015:
BASIC
PROBLEMS
&
SOLVING
ALGORITHMS 31
32. Problem
Solving
Paradigms
– Greedy
Pros
•For
some
problems,
if
applicable,
a
Greedy
solution
is
usually
compact
and
fast.
Cons
•Time-‐consuming
to
prove
the
greedy-‐choice
property.
•(Very)
hard
to
come
up
with.
•A
non-‐proven
Greedy
solution
may
easily
cause
Wrong
Answer
(WA)
response.
8/29/15 ACM-‐ICPC
2015:
BASIC
PROBLEMS
&
SOLVING
ALGORITHMS 32
33. Problem
Solving
Paradigms
– Greedy
When
to
use
Greedy?
◦Use
when
the
problem
exhibits
the
two
properties.
◦Use
when
we
know
for
sure
that
the
input
size
is
too
large
for
our
best
Complete
Search
(we
greed!!).
8/29/15 ACM-‐ICPC
2015:
BASIC
PROBLEMS
&
SOLVING
ALGORITHMS 33
34. Practice
Problem
UVa #410:
Station
Balance
Problem
statement
Given
1
≤
C
≤
5
chambers
which
can
store
0,
1,
or
2
specimens,
1
≤
S
≤
2C
specimens,
and
M:
a
list
of
mass
of
the
S
specimens,
determine
in
which
chamber
we
should
store
each
specimen
in
order
to
minimize
IMBALANCE.
𝐴 = ∑ 𝑀8
9
8:; /𝐶;
A
is
the
average
of
all
mass
over
C
chambers.
𝐼𝑀𝐵𝐴𝐿𝐴𝑁𝐶𝐸 =
(∑ |𝑋E − 𝐴|F
8:; )
;
Time
limit
:
3s
Greedy
8/29/15 ACM-‐ICPC
2015:
BASIC
PROBLEMS
&
SOLVING
ALGORITHMS 34
35. Practice
Problem
UVa #410:
Station
Balance
Illustration
Source:
Competitive
Programming
Book
by
Steven
Halim
Time
limit
:
3s
Greedy
IMBALANCE = C
i=1 |Xi − A|, i.e. sum of differences between the mass i
where Xi is the total mass of specimens in chamber i.
Figure 3.3: Visualization of UVa 410 - Station Balan
This problem can be solved using a greedy algorithm. But first, we have
IMBALANCE = C
i=1 |Xi − A|, i.e. sum of differences between the mass i
where Xi is the total mass of specimens in chamber i.
Figure 3.3: Visualization of UVa 410 - Station Balan
This problem can be solved using a greedy algorithm. But first, we have8/29/15 ACM-‐ICPC
2015:
BASIC
PROBLEMS
&
SOLVING
ALGORITHMS 35
36. Practice
Problem
UVa #410:
Station
Balance
Quick
Analysis
Source:
Competitive
Programming
Book
by
Steven
Halim
Observation
1
:
If
there
exists
an
empty
chamber,
at
least
one
chamber
with
2
specimens
must
be
moved
to
this
empty
chamber!
Otherwise
the
empty
chambers
contribute
too
much
to
IMBALANCE.
Time
limit
:
3s
Greedy
tions. If there exists an empty chamber, at least one chamber with 2 specim
this empty chamber! Otherwise the empty chambers contribute too much
Figure 3.4.
Figure 3.4: UVa 410 - Observation 1
Next observation: If S > C, then S −C specimens must be paired with one
in some chambers. The Pigeonhole principle! See Figure 3.5.
8/29/15 ACM-‐ICPC
2015:
BASIC
PROBLEMS
&
SOLVING
ALGORITHMS 36
37. Practice
Problem
UVa #410:
Station
Balance
Quick
Analysis
Source:
Competitive
Programming
Book
by
Steven
Halim
Observation
2 :
If
S
>
C,
then
S−C
specimens
must
be
paired
with
one
other
specimen
already
in
some
chambers
(the
Pigeonhole
principle).
Time
limit
:
3s
Greedy
Next observation: If S > C, then S −C specimens must be paired with one other sp
in some chambers. The Pigeonhole principle! See Figure 3.5.
Figure 3.5: UVa 410 - Observation 2
Now, the key insight that can simplify the problem is this: If S < 2C, add dummy 2
with mass 0. For example, C = 3, S = 4, M = {5, 1, 2, 7} → C = 3, S = 6, M =
Then, sort these specimens based on their mass such that M1 ≤ M2 ≤ . . . ≤ M2
this example, M = {5, 1, 2, 7, 0, 0} → {0, 0, 1, 2, 5, 7}.
368/29/15 ACM-‐ICPC
2015:
BASIC
PROBLEMS
&
SOLVING
ALGORITHMS 37
38. Practice
Problem
UVa #410:
Station
Balance
Quick
Analysis
Source:
Competitive
Programming
Book
by
Steven
Halim
Tricks:
§ If
S
<
2C,
add
dummy
2C−S
specimens
with
mass
0.
EX: C
=
3,
S
=
4,
M
=
{5,1,2,7}
→
C
=
3,S
=
6,M
=
{5,1,2,7,0,0}.
§ Then,
sort
these
specimens
based
on
their
mass
such
that
M1 ≤
M2 ≤
...
≤
M2C−1
≤
M2C.
EX: M
=
{5,1,2,7,0,0}
→
{0,0,1,2,5,7}.
Time
limit
:
3s
Greedy
Pair the specimens with masses M1&M2C and put them in chamber 1, th
Pair the specimens with masses M2&M2C−1 and put them in chamber 2,
This greedy algorithm – known as ‘Load Balancing’ – works! See Figure 3
Figure 3.6: UVa 410 - Greedy Solution
To come up with this way of thinking is hard to teach but can be gained f
from this example: If no obvious greedy strategy seen, try to sort the dat
tweaks and see if a greedy strategy emerges.8/29/15 ACM-‐ICPC
2015:
BASIC
PROBLEMS
&
SOLVING
ALGORITHMS 38
39. Practice
Problem
UVa #410:
Station
Balance
Quick
Analysis
Source:
Competitive
Programming
Book
by
Steven
Halim
Greedy
solution
(Load
Balancing):
§ Pair
the
specimens
with
masses
M1 and
M2C and
put
them
in
chamber
1.
§ Then,
pair
the
specimens
with
masses
M2 and
M2C−1 and
put
them
in
chamber
2,
and
so
on.
Time
limit
:
3s
Greedy
Figure 3.6: UVa 410 - Greedy Solution
To come up with this way of thinking is hard to teach but can be gained from exp
from this example: If no obvious greedy strategy seen, try to sort the data first o
tweaks and see if a greedy strategy emerges.
3.3.3 Remarks About Greedy Algorithm in Programming Conte
Using Greedy solutions in programming contests is usually risky. A greedy solut8/29/15 ACM-‐ICPC
2015:
BASIC
PROBLEMS
&
SOLVING
ALGORITHMS 39
40. Problem
Solving
Paradigms
Four
Basic
Problem
Solving
Paradigms
◦ Complete
Search
◦ Divide
and
Conquer
◦ Greedy
◦ Dynamic
Programming
8/29/15 ACM-‐ICPC
2015:
BASIC
PROBLEMS
&
SOLVING
ALGORITHMS 40
41. Problem
Solving
Paradigms
– Dynamic
Programming
Dynamic
Programming
(DP)
◦ A
method
for
solving
a
problem
by
combining
the
solutions
to
sub-‐problems
(like
D&C).
◦ Advanced
problem
solving
paradigm
(improvised,
subtleties).
◦ Most
frequently
appearing
in
programming
contest.
◦ Frequently
used
DP
algorithms
include
LCS,
Matrix
Chain
Multiplication,
Knapsack,
Floyd
Warshall’s APSPs,
Kadene’s
Maximum
Contiguous
Sub-‐array.
8/29/15 ACM-‐ICPC
2015:
BASIC
PROBLEMS
&
SOLVING
ALGORITHMS 41
42. Problem
Solving
Paradigms
– Dynamic
Programming
Elements
of
DP
1. Optimal
sub-‐structure:
Ø Optimal
solution
to
the
problem
contains
within
optimal
solutions
to
the
sub-‐problems.
2. Overlapping
sub-‐problems:
Ø The
same
problem
is
revisited
over
and
over
(contrast
to
D&C).
3. Memoization :
Ø Caching
lookup
table
(contrast
to
Complete
Search).
8/29/15 ACM-‐ICPC
2015:
BASIC
PROBLEMS
&
SOLVING
ALGORITHMS 42
43. Problem
Solving
Paradigms
– Dynamic
Programming
Keys
to
develop
a
DP
solution
1. Determine
the
optimal
sub-‐structure
of
the
problem.
2. Identify
if
the
problem
has
overlapping
sub-‐
problems.
3. Choose
Top-‐Down
or
Bottom-‐Up.
8/29/15 ACM-‐ICPC
2015:
BASIC
PROBLEMS
&
SOLVING
ALGORITHMS 43
44. Practice
Problem
UVa #495:
Fibonacci
Freeze
Problem
statement
The
Fibonacci
numbers
(0,
1,
1,
2,
3,
5,
8,
13,
21,
34,
55,
…)
are
defined
by
the
recurrence:
F0 =
0
F1 =
1
Fi =
Fi-‐1 +
Fi-‐2 ,
for
all
𝑖 ≥ 2
Compute
Fi,
where
𝑖 ≤ 5000.
Time
limit
:
3s
First
trial
:
Divide
and
Conquer
8/29/15 ACM-‐ICPC
2015:
BASIC
PROBLEMS
&
SOLVING
ALGORITHMS 44
45. Practice
Problem
UVa #495:
Fibonacci
Freeze
Quick
Analysis
(D&C
Solution)
§Solve
Fi by
recursively
solving
Fi-‐1 and
Fi-‐2.
§If
i =
0,
then
return
Fi =
0.
§If
i =
1,
then
return
Fi =
1.
§Combine
Fi
from
Fi-‐1
+ Fi-‐2.
The
D&C
solution
is
extremely
slow.
The
time
complexity
is
about
𝑂 𝑛
! . It
cannot
pass
the
time
limit
definitely
!!
Time
limit
:
3s
First
trial
:
Divide
and
Conquer
8/29/15 ACM-‐ICPC
2015:
BASIC
PROBLEMS
&
SOLVING
ALGORITHMS 45
46. Problem
Solving
Paradigms
– Dynamic
Programming
Example
of
DP
(Fibonacci
Sequence)
◦In
the
Fibonacci
Freeze
problem
ØOptimal
sub-‐structure:
o Fi =
Fi-‐1 +
Fi-‐2
ØOverlapping
sub-‐problems:
o EX: F6 =
F5 +
F4,
F5 =
F4 +
F3,
F4 =
F3 +
F2 ,
F3 =
F2 +
F1
8/29/15 ACM-‐ICPC
2015:
BASIC
PROBLEMS
&
SOLVING
ALGORITHMS 46
47. Practice
Problem
UVa #495:
Fibonacci
Freeze
Quick
Analysis
(DP
Top-‐Down
Solution)
§Initialize
array
A[0..i],
A[0]
=
0, A[1]
=
1.
Intuitively,
we
will
have
A[i]
=
Fi
at
the
end.
§Solve
Fi =
Fi-‐1
+
Fi-‐2
by
oIf
A[i-‐1]
is
null,
then
recursively
solve Fi-‐1;
Otherwise,
return
A[i-‐1].
oif
A[i-‐2]
is
null,
then
recursively
solve Fi-‐2;
Otherwise,
return
A[i-‐2].
The
time
complexity
is
𝑂 𝑛 ,
as
no
duplicate
sub-‐
problems
recomputed.
Note:
recursive
calls
overhead
as
many
sub-‐problems
are
still
revisited.
The
solution
might
not
be
good
enough
to
pass
the
time
limit.
Time
limit
:
3s
First
trial
:
Divide
and
Conquer
Second
trial
:
Dynamic
Programming
(Top-‐Down)
8/29/15 ACM-‐ICPC
2015:
BASIC
PROBLEMS
&
SOLVING
ALGORITHMS 47
48. Practice
Problem
UVa #495:
Fibonacci
Freeze
Quick
Analysis
(DP
Bottom-‐Up
Solution)
§Initialize
array
A[0..i],
A[0]
=
0, A[1]
=
1.
Intuitively,
we
will
have
A[i]
=
Fi
at
the
end.
§For
each
2
≤
j
≤
i,
compute
A[j]
=
A[j-‐1]+ A[j-‐
2].
§Return
A[i].
The
time
complexity
is
also
𝑂 𝑛 ,
as
no
duplicate
sub-‐problems
recomputed.
Obviously,
the
Bottom-‐up
version
is
faster.
Yes,
it
can
likely
pass
the
time
limit.
But…
wait
for
it!!!
We
will
come
back
later.
Time
limit
:
3s
First
trial
:
Divide
and
Conquer
Second
trial
:
Dynamic
Programming
(Top-‐Down)
Second
trial
:
Dynamic
Programming
(Bottom-‐Up)
8/29/15 ACM-‐ICPC
2015:
BASIC
PROBLEMS
&
SOLVING
ALGORITHMS 48
49. Problem
Solving
Paradigms
– Dynamic
Programming
Pros
of
DP
Top-‐Down
•Natural
transformation
from
recursive
Complete
Search.
•Sub-‐problems
are
computed
when
necessary.
Cons
of
DP
Top-‐Down
•Slower
if
many
sub-‐
problems
are
revisited.
•Caching
table
of
very
large
size
may
cause
Memory
Limit
Exceeded
(MLE).
8/29/15 ACM-‐ICPC
2015:
BASIC
PROBLEMS
&
SOLVING
ALGORITHMS 49
50. Problem
Solving
Paradigms
– Dynamic
Programming
Pros
of
DP
Bottom-‐Up
•Faster
if
many
sub-‐
problems
are
revisited.
•Save
memory
space.
Cons
of
DP
Bottom-‐Up
•May
not
be
intuitive.
•Every
states
of
caching
table
needed
to
be
filled.
8/29/15 ACM-‐ICPC
2015:
BASIC
PROBLEMS
&
SOLVING
ALGORITHMS 50
51. Problem
Solving
Paradigms
– Dynamic
Programming
When
to
use
DP?
◦Use
when
Complete
Search
receives
TLE.
◦Use
when
Greedy
receives
WA.
8/29/15 ACM-‐ICPC
2015:
BASIC
PROBLEMS
&
SOLVING
ALGORITHMS 51
57. Basic
Algorithms – Sorting
Often
used
sorting
algorithms
◦ Insertion
sort
-‐ 𝑂(𝑛3
) time
in
the
worst
case
◦ Merge
sort
-‐ 𝑂 𝑛
log
𝑛 time
in
the
worst
case
◦ Heap
sort
-‐ 𝑂 𝑛
log
𝑛 time
in
the
worst
case
◦ Quicksort
-‐ 𝑂 𝑛
log
𝑛 time
in
the
average
case
◦ Counting
sort
-‐ 𝑂 𝑛 + 𝑘 time
in
the
worst
case
◦ Stable
sorting
property
8/29/15 ACM-‐ICPC
2015:
BASIC
PROBLEMS
&
SOLVING
ALGORITHMS 57
59. Basic
Algorithms
– Binary
Search
Binary
Search
algorithm
◦ Input
Ø Sorted
array
:
A[1
..
n]
Ø Target
value
:
a
◦ Output
Ø Report
the
position
of
a in
A, if
a is
found
8/29/15 ACM-‐ICPC
2015:
BASIC
PROBLEMS
&
SOLVING
ALGORITHMS 59
60. Basic
Algorithms
– Binary
Search
Built-‐in
Binary
Search
algorithm
with
𝑂 log
𝑛 worst-‐
case
time
complexity
◦ C/C++
Ø std::binary_search(A.begin(),
A.end(),
a);
◦ JAVA
Ø Java.util.Arrays.binarySearch(int[]
A,
int a);
8/29/15 ACM-‐ICPC
2015:
BASIC
PROBLEMS
&
SOLVING
ALGORITHMS 60
61. Basic
Algorithms – Graph
Often
used
graph
algorithms
◦ Graph
Representations
– Adjacency
list
&
matrix
◦ Graph
Traversal
– DFS,
BFS,
CC,
SCC
◦ Maximum
Spanning
Tree
◦ Shortest
Paths
– SSSPs,
APSPs
◦ Maximum
Flow
◦ Maximum
Matching
8/29/15 ACM-‐ICPC
2015:
BASIC
PROBLEMS
&
SOLVING
ALGORITHMS 61
63. Basic
Algorithms – Mathematics
Topics
often
related
as
Ad-‐Hoc
problems
◦ Number
Theory
– GCD
&
LCS,
Prime,
Fibonacci,
Modulo
Arithmetic,
Factorial
◦ java.util.BigInteger Class
8/29/15 ACM-‐ICPC
2015:
BASIC
PROBLEMS
&
SOLVING
ALGORITHMS 63
64. Practice
Problem
UVa #495:
Fibonacci
Freeze
Problem
statement
The
Fibonacci
numbers
(0,
1,
1,
2,
3,
5,
8,
13,
21,
34,
55,
…)
are
defined
by
the
recurrence:
F0 =
0
F1 =
1
Fi =
Fi-‐1 +
Fi-‐2 ,
for
all
𝑖 ≥ 2
Compute
Fi,
where
𝑖 ≤ 5000.
Time
limit
:
3s
First
trial
:
Divide
and
Conquer
Second
trial
:
Dynamic
Programming
(Top-‐Down)
Second
trial
:
Dynamic
Programming
(Bottom-‐Up)
8/29/15 ACM-‐ICPC
2015:
BASIC
PROBLEMS
&
SOLVING
ALGORITHMS 64
65. Practice
Problem
UVa #495:
Fibonacci
Freeze
Quick
Analysis
(DP
Bottom-‐Up
Solution)
§Initialize
array
A[0..i],
A[0]
=
0, A[1]
=
1.
Intuitively,
we
will
have
A[i]
=
Fi
at
the
end.
§For
each
2
≤
j
≤
i,
compute
A[j]
=
A[j-‐1]+
A[j-‐2].
§Return
A[i].
The
real
problem
is
no
primitive
data
types
supporting
the
1045
digits
number.
Exit
way:
java.math.BigInteger class
Time
limit
:
3s
First
trial
:
Divide
and
Conquer
Second
trial
:
Dynamic
Programming
(Top-‐Down)
Second
trial
:
Dynamic
Programming
(Bottom-‐Up)
8/29/15 ACM-‐ICPC
2015:
BASIC
PROBLEMS
&
SOLVING
ALGORITHMS 65
66. Basic
Algorithms – String
Processing
Often
used
algorithms
in
string
processing
◦ LCS
◦ Suffix
Tree
&
Array
◦ Palindrome
8/29/15 ACM-‐ICPC
2015:
BASIC
PROBLEMS
&
SOLVING
ALGORITHMS 66
70. Tips
To
be
a
competitive
programmer
◦ Code
faster
◦ Quickly
identify
problem
types
◦ Do
algorithm
analysis
(time
complexity)
◦ Master
programming
languages
(C/C++
or
Java)
◦ Master
at
testing
and
debugging
code
◦ Practice
and
more
practice
8/29/15 ACM-‐ICPC
2015:
BASIC
PROBLEMS
&
SOLVING
ALGORITHMS 70
72. Tips
Resources
•Online
Judges
Ø University
of
Valladolid
:
https://uva.onlinejudge.org/
Ø Peking
University:
http://poj.org/
Ø USA
Computing
Olympiad:
http://train.usaco.org/usacogate/
8/29/15 ACM-‐ICPC
2015:
BASIC
PROBLEMS
&
SOLVING
ALGORITHMS 72
73. References
Figure 1.5: Some Reference Books that Inspired the Authors to Write This Book
This and subsequent chapters are supported by many text books (see Figure 1.5) and Internet
resources. Tip 1 is an adaptation from introduction text in USACO training gateway [18]. More
details about Tip 2 can be found in many CS books, e.g. Chapter 1-5, 17 of [4]. Reference for
Tip 3 are http://www.cppreference.com, http://www.sgi.com/tech/stl/ for C++ STL and
http://java.sun.com/javase/6/docs/api for Java API. For more insights to do better testing
(Tip 4), a little detour to software engineering books may be worth trying. There are many other
Online Judges than those mentioned in Tip 5, e.g.
SPOJ http://www.spoj.pl,
POJ http://acm.pku.edu.cn/JudgeOnline,
TOJ http://acm.tju.edu.cn/toj,
ZOJ http://acm.zju.edu.cn/onlinejudge/,
Ural/Timus OJ http://acm.timus.ru, etc.
There are approximately 34 programming exercises discussed in this chapter.
c⃝ Steven & Felix, NUS
(c) Create tricky test cases and find the bug.
3. Follow up question (see question 2 above): What if maximum N is 100.000?
1.2.5 Tip 5: Practice and More Practice
Competitive programmers, like real athletes, must train themselves regularly and keep themselves
‘programming-fit’. Thus in our last tip, we give a list of websites that can help you improve your
problem solving skill. Success is a continuous journey!
University of Valladolid (from Spain) Online Judge [17] contains past years ACM contest prob-
lems (usually local or regional) plus problems from another sources, including their own contest
problems. You can solve these problems and submit your solutions to this Online Judge. The
correctness of your program will be reported as soon as possible. Try solving the problems men-
tioned in this book and see your name on the top-500 authors rank list someday :-). At the point
of writing (9 August 2010), Steven is ranked 121 (for solving 857 problems) while Felix is ranked
70 (for solving 1089 problems) from ≈ 100386 UVa users and 2718 problems.
Figure 1.1: University of Valladolid (UVa) Online Judge, a.k.a Spanish OJ [17]
UVa ‘sister’ online judge is the ACM ICPC Live Archive that contains recent ACM ICPC Regionals
and World Finals problem sets since year 2000. Train here if you want to do well in future ICPCs.
Figure 1.2: ACM ICPC Live Archive [11]
INTRODUCTION
TO
ALGORITHMS
2nd ED
CLRS
Art
of
Programming
Contest
2ED
Arefin
Competitive
Programming
1ST ED
Steven
Halim
and
Felix
Halim
University
of
Valladolid
(Spain)
Online
Judge
8/29/15 ACM-‐ICPC
2015:
BASIC
PROBLEMS
&
SOLVING
ALGORITHMS 73