SlideShare a Scribd company logo
1 of 73
Download to read offline
Basic	
  Problems	
  &	
  
Solving	
  Algorithms
ACM-­‐ICPC	
  Thailand	
  Northern	
  Regional	
  Programming	
  Contest	
  2015
NOPADON	
  JUNEAM	
  <juneam.n@gmail.com>
CHIANG	
  MAI	
   UNIVERSITY
OUTLINE
§ Introduction
§ Problem	
  Solving	
  Paradigms
§ Basic	
  Data	
  Structures	
  &	
  Algorithms	
  
§ Tips	
  &	
  Exercises
8/29/15 ACM-­‐ICPC	
  2015:	
  BASIC	
  PROBLEMS	
  &	
  SOLVING	
  ALGORITHMS 2
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
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
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
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
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
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
OUTLINE
§ Introduction
§ Problem	
  Solving	
  Paradigms
§ Basic	
  Data	
  Structures	
  &	
  Algorithms	
  
§ Tips	
  &	
  Exercises
8/29/15 ACM-­‐ICPC	
  2015:	
  BASIC	
  PROBLEMS	
  &	
  SOLVING	
  ALGORITHMS 9
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
OUTLINE
§ Introduction
§ Problem	
  Solving	
  Paradigms
§ Basic	
  Data	
  Structures	
  &	
  Algorithms	
  
§ Tips &	
  Exercises
8/29/15 ACM-­‐ICPC	
  2015:	
  BASIC	
  PROBLEMS	
  &	
  SOLVING	
  ALGORITHMS 52
Basic	
  Data	
  Structures
Linear	
  Data	
  Structures
◦ Static	
  Array	
  (int[])	
  
◦ Resizable	
  Array	
  (C++	
  STL	
  <vector>/java.util.ArrayList)
◦ Linked	
  List	
  (C++	
  STL	
  <list>/java.util.LinkedList)
◦ LIFO – Stack	
  (C++	
  STL	
  <stack>/java.util.Stack)
◦ FIFO	
  – Queue	
  (C++	
  STL	
  <queue>/java.util.Queue)
8/29/15 ACM-­‐ICPC	
  2015:	
  BASIC	
  PROBLEMS	
  &	
  SOLVING	
  ALGORITHMS 53
Basic	
  Data	
  Structures
Non-­‐Linear	
  Data	
  Structures
◦ Balanced	
  Binary	
  Search	
  Tree	
  (BST)	
  (C++	
  STL	
  <map>/(C++	
  	
  STL	
  
<set>	
  /java.util.TreeMap/java.util.Treeset)
◦ Heap	
  (C++STL	
  <queue>/java.util.PriorityQueue)
◦ Direct	
  Addressing	
  Table	
  – Hash	
  Table	
  java.util.HashMap,
java.util.HashSet, java.util.HashTable)
8/29/15 ACM-­‐ICPC	
  2015:	
  BASIC	
  PROBLEMS	
  &	
  SOLVING	
  ALGORITHMS 54
Basic	
  Data	
  Structures
Our-­‐Own	
  Libraries	
  maintaining	
  Graph
◦ Adjacency	
  Matrix	
  – (int[][]	
  G)
◦ Adjacency	
  List	
  – (C++	
  STL	
  vector<vector<pair<int,int>>>	
  
G>/java.util.vector<java.util.vector>)
8/29/15 ACM-­‐ICPC	
  2015:	
  BASIC	
  PROBLEMS	
  &	
  SOLVING	
  ALGORITHMS 55
Basic	
  Algorithms
Basic	
  Algorithms	
  include
◦ Sorting	
  
◦ Binary	
  Search
◦ Graph
◦ Geometry
◦ Mathematics
◦ String	
  Processing
8/29/15 ACM-­‐ICPC	
  2015:	
  BASIC	
  PROBLEMS	
  &	
  SOLVING	
  ALGORITHMS 56
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
Basic	
  Algorithms – Sorting
Built-­‐in	
  sorting	
  algorithms
◦ C/C++
Ø std::sort(A.begin(),	
  A.end());	
  //	
  sort	
  in	
   𝑂 𝑛	
  log	
   𝑛 worst-­‐case	
  time	
  
complexity
Ø std::stable_sort(A.begin(),	
  A.end());	
  //	
  stable sort	
  in	
   𝑂 𝑛	
  log	
   𝑛 worst-­‐
case	
  time	
  complexity
◦ JAVA
Ø Java.util.Arrays.sort(int[]	
  A);	
  //	
  tuned	
  quicksort	
  
Ø Java.util.Arrays.sort(Object[]	
  O);	
  //	
  modified	
  merge	
  sort	
  (stable)	
  
8/29/15 ACM-­‐ICPC	
  2015:	
  BASIC	
  PROBLEMS	
  &	
  SOLVING	
  ALGORITHMS 58
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
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
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
Basic	
  Algorithms – Geometry
Often	
  used	
  geometric	
  algorithms
◦ Geometry	
  basic	
  (Points,	
  Lines,	
  Circles,	
  Triangles)
◦ Convex	
  Hull
◦ Interval	
  Tree
8/29/15 ACM-­‐ICPC	
  2015:	
  BASIC	
  PROBLEMS	
  &	
  SOLVING	
  ALGORITHMS 62
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
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
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
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
OUTLINE
§ Introduction
§ Problem	
  Solving	
  Paradigms
§ Basic	
  Data	
  Structures	
  &	
  Algorithms	
  
§ Tips	
  &	
  Exercises
8/29/15 ACM-­‐ICPC	
  2015:	
  BASIC	
  PROBLEMS	
  &	
  SOLVING	
  ALGORITHMS 67
Exercises
Selected	
  UVa Problems
◦ #108	
  – Maximum	
  Sum	
  (DP+ Maximum	
  Contiguous	
  Sub-­‐
array)
◦ #495	
  – Fibonacci	
  Freeze	
  (DP+java.util.BigInteger)
◦ #506	
  – System	
  Dependencies	
  (Directed	
  Graph+D&C)
◦ #523	
  – Minimum	
  Transport	
  Cost	
  (Weighted	
  Graph	
  +	
  
Modified	
  DP-­‐SSSPs/APSPs)
◦ #551	
  -­‐ Nesting	
  a	
  Bunch	
  of	
  Brackets	
  (Stack)
8/29/15 ACM-­‐ICPC	
  2015:	
  BASIC	
  PROBLEMS	
  &	
  SOLVING	
  ALGORITHMS 68
Exercises
Selected	
  Problems	
  &	
  Solutions	
  Available	
  at:
https://github.com/dmodify/UVa-­‐Collection
8/29/15 ACM-­‐ICPC	
  2015:	
  BASIC	
  PROBLEMS	
  &	
  SOLVING	
  ALGORITHMS 69
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
Tips
Resources
•Free	
  E-­‐Books
Ø Competitive	
  Programming:	
  
http://www.comp.nus.edu.sg/~stevenha/myteaching/competitive_progr
amming/cp1.pdf
Ø Art	
  of	
  Programming	
  Contest:	
  
http://acm.uva.es/problemset/Art_of_Programming_Contest_SE_for_uva
.pdf
8/29/15 ACM-­‐ICPC	
  2015:	
  BASIC	
  PROBLEMS	
  &	
  SOLVING	
  ALGORITHMS 71
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
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

More Related Content

What's hot

What Is Dynamic Programming? | Dynamic Programming Explained | Programming Fo...
What Is Dynamic Programming? | Dynamic Programming Explained | Programming Fo...What Is Dynamic Programming? | Dynamic Programming Explained | Programming Fo...
What Is Dynamic Programming? | Dynamic Programming Explained | Programming Fo...Simplilearn
 
Introduction to Computational Thinking.pptx
Introduction to Computational Thinking.pptxIntroduction to Computational Thinking.pptx
Introduction to Computational Thinking.pptxAyodeleOgegbo
 
1 introduction to problem solving and programming
1 introduction to problem solving and programming1 introduction to problem solving and programming
1 introduction to problem solving and programmingRheigh Henley Calderon
 
Bruteforce algorithm
Bruteforce algorithmBruteforce algorithm
Bruteforce algorithmRezwan Siam
 
Intro To Programming Concepts
Intro To Programming ConceptsIntro To Programming Concepts
Intro To Programming ConceptsJussi Pohjolainen
 
Introduction to computer science
Introduction to computer scienceIntroduction to computer science
Introduction to computer scienceDarshan Gohel
 
structured programming Introduction to c fundamentals
structured programming Introduction to c fundamentalsstructured programming Introduction to c fundamentals
structured programming Introduction to c fundamentalsOMWOMA JACKSON
 
Software programming and development
Software programming and developmentSoftware programming and development
Software programming and developmentAli Raza
 
Natural Language Processing with Python
Natural Language Processing with PythonNatural Language Processing with Python
Natural Language Processing with PythonBenjamin Bengfort
 
Lecture 01 introduction to compiler
Lecture 01 introduction to compilerLecture 01 introduction to compiler
Lecture 01 introduction to compilerIffat Anjum
 
Design and analysis of algorithms
Design and analysis of algorithmsDesign and analysis of algorithms
Design and analysis of algorithmsDr Geetha Mohan
 
.NET and C# Introduction
.NET and C# Introduction.NET and C# Introduction
.NET and C# IntroductionSiraj Memon
 
Top 10 Applications of Machine Learning | Machine Learning Application Exampl...
Top 10 Applications of Machine Learning | Machine Learning Application Exampl...Top 10 Applications of Machine Learning | Machine Learning Application Exampl...
Top 10 Applications of Machine Learning | Machine Learning Application Exampl...Edureka!
 
4 evolution-of-programming-languages
4 evolution-of-programming-languages4 evolution-of-programming-languages
4 evolution-of-programming-languagesRohit Shrivastava
 
Compiler Design
Compiler DesignCompiler Design
Compiler DesignMir Majid
 

What's hot (20)

What Is Dynamic Programming? | Dynamic Programming Explained | Programming Fo...
What Is Dynamic Programming? | Dynamic Programming Explained | Programming Fo...What Is Dynamic Programming? | Dynamic Programming Explained | Programming Fo...
What Is Dynamic Programming? | Dynamic Programming Explained | Programming Fo...
 
Introduction to Computational Thinking.pptx
Introduction to Computational Thinking.pptxIntroduction to Computational Thinking.pptx
Introduction to Computational Thinking.pptx
 
1 introduction to problem solving and programming
1 introduction to problem solving and programming1 introduction to problem solving and programming
1 introduction to problem solving and programming
 
Bruteforce algorithm
Bruteforce algorithmBruteforce algorithm
Bruteforce algorithm
 
Intro To Programming Concepts
Intro To Programming ConceptsIntro To Programming Concepts
Intro To Programming Concepts
 
Time complexity.ppt
Time complexity.pptTime complexity.ppt
Time complexity.ppt
 
Introduction to computer science
Introduction to computer scienceIntroduction to computer science
Introduction to computer science
 
structured programming Introduction to c fundamentals
structured programming Introduction to c fundamentalsstructured programming Introduction to c fundamentals
structured programming Introduction to c fundamentals
 
Software programming and development
Software programming and developmentSoftware programming and development
Software programming and development
 
Introduction to algorithms
Introduction to algorithmsIntroduction to algorithms
Introduction to algorithms
 
Algorithm and flowchart
Algorithm and flowchartAlgorithm and flowchart
Algorithm and flowchart
 
Code Optimization
Code OptimizationCode Optimization
Code Optimization
 
Natural Language Processing with Python
Natural Language Processing with PythonNatural Language Processing with Python
Natural Language Processing with Python
 
Introduction To C#
Introduction To C#Introduction To C#
Introduction To C#
 
Lecture 01 introduction to compiler
Lecture 01 introduction to compilerLecture 01 introduction to compiler
Lecture 01 introduction to compiler
 
Design and analysis of algorithms
Design and analysis of algorithmsDesign and analysis of algorithms
Design and analysis of algorithms
 
.NET and C# Introduction
.NET and C# Introduction.NET and C# Introduction
.NET and C# Introduction
 
Top 10 Applications of Machine Learning | Machine Learning Application Exampl...
Top 10 Applications of Machine Learning | Machine Learning Application Exampl...Top 10 Applications of Machine Learning | Machine Learning Application Exampl...
Top 10 Applications of Machine Learning | Machine Learning Application Exampl...
 
4 evolution-of-programming-languages
4 evolution-of-programming-languages4 evolution-of-programming-languages
4 evolution-of-programming-languages
 
Compiler Design
Compiler DesignCompiler Design
Compiler Design
 

Similar to Basic Problems and Solving Algorithms

CS8461 - Design and Analysis of Algorithms
CS8461 - Design and Analysis of AlgorithmsCS8461 - Design and Analysis of Algorithms
CS8461 - Design and Analysis of AlgorithmsKrishnan MuthuManickam
 
CH-1.1 Introduction (1).pptx
CH-1.1 Introduction (1).pptxCH-1.1 Introduction (1).pptx
CH-1.1 Introduction (1).pptxsatvikkushwaha1
 
Algorithms Design Patterns
Algorithms Design PatternsAlgorithms Design Patterns
Algorithms Design PatternsAshwin Shiv
 
Production model lifecycle management 2016 09
Production model lifecycle management 2016 09Production model lifecycle management 2016 09
Production model lifecycle management 2016 09Greg Makowski
 
Introduction to dynamic programming
Introduction to dynamic programmingIntroduction to dynamic programming
Introduction to dynamic programmingAmisha Narsingani
 
Design and Analysis Algorithms.pdf
Design and Analysis Algorithms.pdfDesign and Analysis Algorithms.pdf
Design and Analysis Algorithms.pdfHarshNagda5
 
CS8451 DAA Unit-I.pptx
CS8451 DAA Unit-I.pptxCS8451 DAA Unit-I.pptx
CS8451 DAA Unit-I.pptxBolliniNivas
 
Chapter1.1 Introduction.ppt
Chapter1.1 Introduction.pptChapter1.1 Introduction.ppt
Chapter1.1 Introduction.pptTekle12
 
Chapter1.1 Introduction to design and analysis of algorithm.ppt
Chapter1.1 Introduction to design and analysis of algorithm.pptChapter1.1 Introduction to design and analysis of algorithm.ppt
Chapter1.1 Introduction to design and analysis of algorithm.pptTekle12
 
Solving Industrial Scheduling Problems with Constraint Programming
Solving Industrial Scheduling Problems with Constraint ProgrammingSolving Industrial Scheduling Problems with Constraint Programming
Solving Industrial Scheduling Problems with Constraint ProgrammingPhilippe Laborie
 
PROBLEM SOLVING TECHNIQUES
PROBLEM SOLVING TECHNIQUESPROBLEM SOLVING TECHNIQUES
PROBLEM SOLVING TECHNIQUESsudhanagarajan5
 
Algo_Lecture01.pptx
Algo_Lecture01.pptxAlgo_Lecture01.pptx
Algo_Lecture01.pptxShaistaRiaz4
 
Lecture01 algorithm analysis
Lecture01 algorithm analysisLecture01 algorithm analysis
Lecture01 algorithm analysisZara Nawaz
 
Programs_Problem_Solving_Algorithms.ppt
Programs_Problem_Solving_Algorithms.pptPrograms_Problem_Solving_Algorithms.ppt
Programs_Problem_Solving_Algorithms.pptmalik681299
 
Ds03 part i algorithms by jyoti lakhani
Ds03 part i algorithms   by jyoti lakhaniDs03 part i algorithms   by jyoti lakhani
Ds03 part i algorithms by jyoti lakhanijyoti_lakhani
 
LF Energy Webinar - Unveiling OpenEEMeter 4.0
LF Energy Webinar - Unveiling OpenEEMeter 4.0LF Energy Webinar - Unveiling OpenEEMeter 4.0
LF Energy Webinar - Unveiling OpenEEMeter 4.0DanBrown980551
 

Similar to Basic Problems and Solving Algorithms (20)

CS8461 - Design and Analysis of Algorithms
CS8461 - Design and Analysis of AlgorithmsCS8461 - Design and Analysis of Algorithms
CS8461 - Design and Analysis of Algorithms
 
Algorithm.pptx
Algorithm.pptxAlgorithm.pptx
Algorithm.pptx
 
CH-1.1 Introduction (1).pptx
CH-1.1 Introduction (1).pptxCH-1.1 Introduction (1).pptx
CH-1.1 Introduction (1).pptx
 
Algorithms Design Patterns
Algorithms Design PatternsAlgorithms Design Patterns
Algorithms Design Patterns
 
Production model lifecycle management 2016 09
Production model lifecycle management 2016 09Production model lifecycle management 2016 09
Production model lifecycle management 2016 09
 
CC-112-Lec.1.ppsx
CC-112-Lec.1.ppsxCC-112-Lec.1.ppsx
CC-112-Lec.1.ppsx
 
Introduction to dynamic programming
Introduction to dynamic programmingIntroduction to dynamic programming
Introduction to dynamic programming
 
Design and Analysis Algorithms.pdf
Design and Analysis Algorithms.pdfDesign and Analysis Algorithms.pdf
Design and Analysis Algorithms.pdf
 
CS8451 DAA Unit-I.pptx
CS8451 DAA Unit-I.pptxCS8451 DAA Unit-I.pptx
CS8451 DAA Unit-I.pptx
 
Chapter1.1 Introduction.ppt
Chapter1.1 Introduction.pptChapter1.1 Introduction.ppt
Chapter1.1 Introduction.ppt
 
Chapter1.1 Introduction to design and analysis of algorithm.ppt
Chapter1.1 Introduction to design and analysis of algorithm.pptChapter1.1 Introduction to design and analysis of algorithm.ppt
Chapter1.1 Introduction to design and analysis of algorithm.ppt
 
Unit no_1.pptx
Unit no_1.pptxUnit no_1.pptx
Unit no_1.pptx
 
Solving Industrial Scheduling Problems with Constraint Programming
Solving Industrial Scheduling Problems with Constraint ProgrammingSolving Industrial Scheduling Problems with Constraint Programming
Solving Industrial Scheduling Problems with Constraint Programming
 
PROBLEM SOLVING TECHNIQUES
PROBLEM SOLVING TECHNIQUESPROBLEM SOLVING TECHNIQUES
PROBLEM SOLVING TECHNIQUES
 
Algo_Lecture01.pptx
Algo_Lecture01.pptxAlgo_Lecture01.pptx
Algo_Lecture01.pptx
 
Lecture01 algorithm analysis
Lecture01 algorithm analysisLecture01 algorithm analysis
Lecture01 algorithm analysis
 
Unit i
Unit iUnit i
Unit i
 
Programs_Problem_Solving_Algorithms.ppt
Programs_Problem_Solving_Algorithms.pptPrograms_Problem_Solving_Algorithms.ppt
Programs_Problem_Solving_Algorithms.ppt
 
Ds03 part i algorithms by jyoti lakhani
Ds03 part i algorithms   by jyoti lakhaniDs03 part i algorithms   by jyoti lakhani
Ds03 part i algorithms by jyoti lakhani
 
LF Energy Webinar - Unveiling OpenEEMeter 4.0
LF Energy Webinar - Unveiling OpenEEMeter 4.0LF Energy Webinar - Unveiling OpenEEMeter 4.0
LF Energy Webinar - Unveiling OpenEEMeter 4.0
 

Recently uploaded

Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibitjbellavia9
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentationcamerronhm
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfSherif Taha
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...Nguyen Thanh Tu Collection
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseAnaAcapella
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and ModificationsMJDuyan
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxAmanpreet Kaur
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jisc
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the ClassroomPooky Knightsmith
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfDr Vijay Vishwakarma
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Pooja Bhuva
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxJisc
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxEsquimalt MFRC
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxJisc
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...Nguyen Thanh Tu Collection
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxmarlenawright1
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfPoh-Sun Goh
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - Englishneillewis46
 

Recently uploaded (20)

Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 

Basic Problems and Solving Algorithms

  • 1. Basic  Problems  &   Solving  Algorithms ACM-­‐ICPC  Thailand  Northern  Regional  Programming  Contest  2015 NOPADON  JUNEAM  <juneam.n@gmail.com> CHIANG  MAI   UNIVERSITY
  • 2. OUTLINE § Introduction § Problem  Solving  Paradigms § Basic  Data  Structures  &  Algorithms   § Tips  &  Exercises 8/29/15 ACM-­‐ICPC  2015:  BASIC  PROBLEMS  &  SOLVING  ALGORITHMS 2
  • 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
  • 9. OUTLINE § Introduction § Problem  Solving  Paradigms § Basic  Data  Structures  &  Algorithms   § Tips  &  Exercises 8/29/15 ACM-­‐ICPC  2015:  BASIC  PROBLEMS  &  SOLVING  ALGORITHMS 9
  • 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
  • 52. OUTLINE § Introduction § Problem  Solving  Paradigms § Basic  Data  Structures  &  Algorithms   § Tips &  Exercises 8/29/15 ACM-­‐ICPC  2015:  BASIC  PROBLEMS  &  SOLVING  ALGORITHMS 52
  • 53. Basic  Data  Structures Linear  Data  Structures ◦ Static  Array  (int[])   ◦ Resizable  Array  (C++  STL  <vector>/java.util.ArrayList) ◦ Linked  List  (C++  STL  <list>/java.util.LinkedList) ◦ LIFO – Stack  (C++  STL  <stack>/java.util.Stack) ◦ FIFO  – Queue  (C++  STL  <queue>/java.util.Queue) 8/29/15 ACM-­‐ICPC  2015:  BASIC  PROBLEMS  &  SOLVING  ALGORITHMS 53
  • 54. Basic  Data  Structures Non-­‐Linear  Data  Structures ◦ Balanced  Binary  Search  Tree  (BST)  (C++  STL  <map>/(C++    STL   <set>  /java.util.TreeMap/java.util.Treeset) ◦ Heap  (C++STL  <queue>/java.util.PriorityQueue) ◦ Direct  Addressing  Table  – Hash  Table  java.util.HashMap, java.util.HashSet, java.util.HashTable) 8/29/15 ACM-­‐ICPC  2015:  BASIC  PROBLEMS  &  SOLVING  ALGORITHMS 54
  • 55. Basic  Data  Structures Our-­‐Own  Libraries  maintaining  Graph ◦ Adjacency  Matrix  – (int[][]  G) ◦ Adjacency  List  – (C++  STL  vector<vector<pair<int,int>>>   G>/java.util.vector<java.util.vector>) 8/29/15 ACM-­‐ICPC  2015:  BASIC  PROBLEMS  &  SOLVING  ALGORITHMS 55
  • 56. Basic  Algorithms Basic  Algorithms  include ◦ Sorting   ◦ Binary  Search ◦ Graph ◦ Geometry ◦ Mathematics ◦ String  Processing 8/29/15 ACM-­‐ICPC  2015:  BASIC  PROBLEMS  &  SOLVING  ALGORITHMS 56
  • 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
  • 58. Basic  Algorithms – Sorting Built-­‐in  sorting  algorithms ◦ C/C++ Ø std::sort(A.begin(),  A.end());  //  sort  in   𝑂 𝑛  log   𝑛 worst-­‐case  time   complexity Ø std::stable_sort(A.begin(),  A.end());  //  stable sort  in   𝑂 𝑛  log   𝑛 worst-­‐ case  time  complexity ◦ JAVA Ø Java.util.Arrays.sort(int[]  A);  //  tuned  quicksort   Ø Java.util.Arrays.sort(Object[]  O);  //  modified  merge  sort  (stable)   8/29/15 ACM-­‐ICPC  2015:  BASIC  PROBLEMS  &  SOLVING  ALGORITHMS 58
  • 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
  • 62. Basic  Algorithms – Geometry Often  used  geometric  algorithms ◦ Geometry  basic  (Points,  Lines,  Circles,  Triangles) ◦ Convex  Hull ◦ Interval  Tree 8/29/15 ACM-­‐ICPC  2015:  BASIC  PROBLEMS  &  SOLVING  ALGORITHMS 62
  • 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
  • 67. OUTLINE § Introduction § Problem  Solving  Paradigms § Basic  Data  Structures  &  Algorithms   § Tips  &  Exercises 8/29/15 ACM-­‐ICPC  2015:  BASIC  PROBLEMS  &  SOLVING  ALGORITHMS 67
  • 68. Exercises Selected  UVa Problems ◦ #108  – Maximum  Sum  (DP+ Maximum  Contiguous  Sub-­‐ array) ◦ #495  – Fibonacci  Freeze  (DP+java.util.BigInteger) ◦ #506  – System  Dependencies  (Directed  Graph+D&C) ◦ #523  – Minimum  Transport  Cost  (Weighted  Graph  +   Modified  DP-­‐SSSPs/APSPs) ◦ #551  -­‐ Nesting  a  Bunch  of  Brackets  (Stack) 8/29/15 ACM-­‐ICPC  2015:  BASIC  PROBLEMS  &  SOLVING  ALGORITHMS 68
  • 69. Exercises Selected  Problems  &  Solutions  Available  at: https://github.com/dmodify/UVa-­‐Collection 8/29/15 ACM-­‐ICPC  2015:  BASIC  PROBLEMS  &  SOLVING  ALGORITHMS 69
  • 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
  • 71. Tips Resources •Free  E-­‐Books Ø Competitive  Programming:   http://www.comp.nus.edu.sg/~stevenha/myteaching/competitive_progr amming/cp1.pdf Ø Art  of  Programming  Contest:   http://acm.uva.es/problemset/Art_of_Programming_Contest_SE_for_uva .pdf 8/29/15 ACM-­‐ICPC  2015:  BASIC  PROBLEMS  &  SOLVING  ALGORITHMS 71
  • 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