Upload
nopadon-juneam
View
1.214
Download
3
Embed Size (px)
Citation preview
Basic Problems & Solving AlgorithmsACM-‐ICPC Tha i l and Northern Regiona l Programming Contest 2015
NOPADON JUNEAM <juneam.n@gmai l .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 competitiveprogramming contest.”
“Given a set of well-‐known problems in Computer Scienceyour 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 acceptedif 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
IntroductionTo 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
IntroductionKeys 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 ParadigmsFour 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 SearchComplete 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 SearchKeys 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 SearchExample 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 SearchExample 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 SearchPros•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 SearchWhen 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 ProblemUVa #725:Division
Problem statementFind 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 ProblemUVa #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 ParadigmsFour 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 ConquerDivide 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 ConquerKeys 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 ConquerExample 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 ConquerPros•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 ProblemUVa #495:Fibonacci Freeze
Problem statementThe Fibonacci numbers (0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, …) are defined by the recurrence:F0 = 0F1 = 1Fi = Fi-‐1 + Fi-‐2 , for all 𝑖 ≥ 2Compute Fi, where 𝑖 ≤ 5000.
Time limit : 3s
First trial : Divide and Conquer
8/29/15 ACM-‐ICPC 2015: BASIC PROBLEMS & SOLVING ALGORITHMS 24
Practice ProblemUVa #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 ParadigmsFour 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 – GreedyGreedy◦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 – GreedyA 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 – GreedyKeys 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 – GreedyExample 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 – GreedyExample 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 – GreedyPros•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 – GreedyWhen 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 ProblemUVa #410:Station Balance
Problem statementGiven 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.
𝐴 = ∑ 𝑀898:; /𝐶;
A is the average of all mass over C chambers.
𝐼𝑀𝐵𝐴𝐿𝐴𝑁𝐶𝐸 = (∑ |𝑋E − 𝐴|F8:; ) ;
Time limit : 3s
Greedy
8/29/15 ACM-‐ICPC 2015: BASIC PROBLEMS & SOLVING ALGORITHMS 34
Practice ProblemUVa #410:Station Balance
Illustration
Source: Competitive Programming Book by Steven Halim
Time limit : 3s
Greedy
3.3. GREEDY ⊖ c⃝ Steven & Felix, NUS
UVa 410 - Station Balance
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. See Figure 3.3 for visual explanation.
A = (!S
j=1Mj)/C, i.e. A is the average of all mass over C chambers.
IMBALANCE =!C
i=1 |Xi −A|, i.e. sum of differences between the mass in each chamber w.r.t A.
where Xi is the total mass of specimens in chamber i.
Figure 3.3: Visualization of UVa 410 - Station Balance
This problem can be solved using a greedy algorithm. But first, we have to make several observa-
tions. 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! See
Figure 3.4.
Figure 3.4: UVa 410 - Observation 1
Next observation: If S > C, then S−C specimens must be paired with one other specimen already
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 2C−S specimens
with mass 0. For example, 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 . In
this example, M = {5, 1, 2, 7, 0, 0} → {0, 0, 1, 2, 5, 7}.
36
3.3. GREEDY ⊖ c⃝ Steven & Felix, NUS
UVa 410 - Station Balance
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. See Figure 3.3 for visual explanation.
A = (!S
j=1Mj)/C, i.e. A is the average of all mass over C chambers.
IMBALANCE =!C
i=1 |Xi −A|, i.e. sum of differences between the mass in each chamber w.r.t A.
where Xi is the total mass of specimens in chamber i.
Figure 3.3: Visualization of UVa 410 - Station Balance
This problem can be solved using a greedy algorithm. But first, we have to make several observa-
tions. 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! See
Figure 3.4.
Figure 3.4: UVa 410 - Observation 1
Next observation: If S > C, then S−C specimens must be paired with one other specimen already
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 2C−S specimens
with mass 0. For example, 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 . In
this example, M = {5, 1, 2, 7, 0, 0} → {0, 0, 1, 2, 5, 7}.
36
8/29/15 ACM-‐ICPC 2015: BASIC PROBLEMS & SOLVING ALGORITHMS 35
Practice ProblemUVa #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
3.3. GREEDY ⊖ c⃝ Steven & Felix, NUS
UVa 410 - Station Balance
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. See Figure 3.3 for visual explanation.
A = (!S
j=1Mj)/C, i.e. A is the average of all mass over C chambers.
IMBALANCE =!C
i=1 |Xi −A|, i.e. sum of differences between the mass in each chamber w.r.t A.
where Xi is the total mass of specimens in chamber i.
Figure 3.3: Visualization of UVa 410 - Station Balance
This problem can be solved using a greedy algorithm. But first, we have to make several observa-
tions. 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! See
Figure 3.4.
Figure 3.4: UVa 410 - Observation 1
Next observation: If S > C, then S−C specimens must be paired with one other specimen already
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 2C−S specimens
with mass 0. For example, 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 . In
this example, M = {5, 1, 2, 7, 0, 0} → {0, 0, 1, 2, 5, 7}.
36
8/29/15 ACM-‐ICPC 2015: BASIC PROBLEMS & SOLVING ALGORITHMS 36
Practice ProblemUVa #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
3.3. GREEDY ⊖ c⃝ Steven & Felix, NUS
UVa 410 - Station Balance
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. See Figure 3.3 for visual explanation.
A = (!S
j=1Mj)/C, i.e. A is the average of all mass over C chambers.
IMBALANCE =!C
i=1 |Xi −A|, i.e. sum of differences between the mass in each chamber w.r.t A.
where Xi is the total mass of specimens in chamber i.
Figure 3.3: Visualization of UVa 410 - Station Balance
This problem can be solved using a greedy algorithm. But first, we have to make several observa-
tions. 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! See
Figure 3.4.
Figure 3.4: UVa 410 - Observation 1
Next observation: If S > C, then S−C specimens must be paired with one other specimen already
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 2C−S specimens
with mass 0. For example, 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 . In
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 ProblemUVa #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
3.3. GREEDY ⊖ c⃝ Steven & Felix, NUS
By adding dummy specimens and then sorting them, a greedy strategy ‘appears’. We can now:
Pair the specimens with masses M1&M2C and put them in chamber 1, then
Pair the specimens with masses M2&M2C−1 and put them in chamber 2, and so on . . .
This greedy algorithm – known as ‘Load Balancing’ – works! See Figure 3.6.
Figure 3.6: UVa 410 - Greedy Solution
To come up with this way of thinking is hard to teach but can be gained from experience! One tip
from this example: If no obvious greedy strategy seen, try to sort the data first or introduce some
tweaks and see if a greedy strategy emerges.
3.3.3 Remarks About Greedy Algorithm in Programming Contests
Using Greedy solutions in programming contests is usually risky. A greedy solution normally will
not encounter TLE response, as it is lightweight, but tends to get WA response. Proving that
a certain problem has optimal sub-structure and greedy property in contest time may be time
consuming, so a competitive programmer usually do this:
He will look at the input size. If it is ‘small enough’ for the time complexity of either Complete
Search or Dynamic Programming (see Section 3.4), he will use one of these approaches as both will
ensure correct answer. He will only use Greedy solution if he knows for sure that the input size
given in the problem is too large for his best Complete Search or DP solution.
Having said that, it is quite true that many problem setters nowadays set the input size of such
can-use-greedy-algorithm-or-not-problems to be in some reasonable range so contestants cannot
use the input size to quickly determine the required algorithm!
Programming Exercises solvable using Greedy (hints omitted):
1. UVa 410 - Station Balance (elaborated in this section)
2. UVa 10020 - Minimal Coverage
3. UVa 10340 - All in All
4. UVa 10440 - Ferry Loading II
5. UVa 10670 - Work Reduction
6. UVa 10763 - Foreign Exchange
7. UVa 11054 - Wine Trading in Gergovia
8. UVa 11292 - Dragon of Loowater
9. UVa 11369 - Shopaholic
37
8/29/15 ACM-‐ICPC 2015: BASIC PROBLEMS & SOLVING ALGORITHMS 38
Practice ProblemUVa #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
3.3. GREEDY ⊖ c⃝ Steven & Felix, NUS
By adding dummy specimens and then sorting them, a greedy strategy ‘appears’. We can now:
Pair the specimens with masses M1&M2C and put them in chamber 1, then
Pair the specimens with masses M2&M2C−1 and put them in chamber 2, and so on . . .
This greedy algorithm – known as ‘Load Balancing’ – works! See Figure 3.6.
Figure 3.6: UVa 410 - Greedy Solution
To come up with this way of thinking is hard to teach but can be gained from experience! One tip
from this example: If no obvious greedy strategy seen, try to sort the data first or introduce some
tweaks and see if a greedy strategy emerges.
3.3.3 Remarks About Greedy Algorithm in Programming Contests
Using Greedy solutions in programming contests is usually risky. A greedy solution normally will
not encounter TLE response, as it is lightweight, but tends to get WA response. Proving that
a certain problem has optimal sub-structure and greedy property in contest time may be time
consuming, so a competitive programmer usually do this:
He will look at the input size. If it is ‘small enough’ for the time complexity of either Complete
Search or Dynamic Programming (see Section 3.4), he will use one of these approaches as both will
ensure correct answer. He will only use Greedy solution if he knows for sure that the input size
given in the problem is too large for his best Complete Search or DP solution.
Having said that, it is quite true that many problem setters nowadays set the input size of such
can-use-greedy-algorithm-or-not-problems to be in some reasonable range so contestants cannot
use the input size to quickly determine the required algorithm!
Programming Exercises solvable using Greedy (hints omitted):
1. UVa 410 - Station Balance (elaborated in this section)
2. UVa 10020 - Minimal Coverage
3. UVa 10340 - All in All
4. UVa 10440 - Ferry Loading II
5. UVa 10670 - Work Reduction
6. UVa 10763 - Foreign Exchange
7. UVa 11054 - Wine Trading in Gergovia
8. UVa 11292 - Dragon of Loowater
9. UVa 11369 - Shopaholic
37
8/29/15 ACM-‐ICPC 2015: BASIC PROBLEMS & SOLVING ALGORITHMS 39
Problem Solving ParadigmsFour 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 ProgrammingDynamic 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’sMaximum Contiguous Sub-‐array.
8/29/15 ACM-‐ICPC 2015: BASIC PROBLEMS & SOLVING ALGORITHMS 41
Problem Solving Paradigms – Dynamic ProgrammingElements 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 ProgrammingKeys 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 ProblemUVa #495:Fibonacci Freeze
Problem statementThe Fibonacci numbers (0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, …) are defined by the recurrence:F0 = 0F1 = 1Fi = Fi-‐1 + Fi-‐2 , for all 𝑖 ≥ 2Compute Fi, where 𝑖 ≤ 5000.
Time limit : 3s
First trial : Divide and Conquer
8/29/15 ACM-‐ICPC 2015: BASIC PROBLEMS & SOLVING ALGORITHMS 44
Practice ProblemUVa #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 ProgrammingExample 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 ProblemUVa #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 byoIf 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 ProblemUVa #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 ProgrammingPros 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 ProgrammingPros 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 ProgrammingWhen 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 StructuresLinear 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 StructuresNon-‐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 StructuresOur-‐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 AlgorithmsBasic Algorithms include◦ Sorting ◦ Binary Search◦ Graph◦ Geometry◦ Mathematics◦ String Processing
8/29/15 ACM-‐ICPC 2015: BASIC PROBLEMS & SOLVING ALGORITHMS 56
Basic Algorithms – SortingOften 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 – SortingBuilt-‐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 SearchBinary 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 SearchBuilt-‐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 – GraphOften 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 – GeometryOften 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 – MathematicsTopics 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 ProblemUVa #495:Fibonacci Freeze
Problem statementThe Fibonacci numbers (0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, …) are defined by the recurrence:F0 = 0F1 = 1Fi = Fi-‐1 + Fi-‐2 , for all 𝑖 ≥ 2Compute 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 ProblemUVa #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 ProcessingOften 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
ExercisesSelected 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
TipsTo 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
TipsResources•Free E-‐BooksØ Competitive Programming: http://www.comp.nus.edu.sg/~stevenha/myteaching/competitive_programming/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
TipsResources•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
1.4. CHAPTER NOTES c⃝ Steven & Felix, NUS
1.4 Chapter Notes
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.
13
1.4. CHAPTER NOTES c⃝ Steven & Felix, NUS
1.4 Chapter Notes
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.
13
1.2. TIPS TO BE COMPETITIVE c⃝ Steven & Felix, NUS
1. You receive a WA response for a very easy problem. What should you do?
(a) Abandon this problem and do another.
(b) Improve the performance of the algorithm.
(c) Create tricky test cases and find the bug.
(d) (In team contest): Ask another coder in your team to re-do this problem.
2. You receive a TLE response for an your O(N3) solution. However, maximum N is just 100.
What should you do?
(a) Abandon this problem and do another.
(b) Improve the performance of the algorithm.
(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]
10
INTRODUCTION TO ALGORITHMS 2nd EDCLRS
Art of Programming Contest2EDArefin
Competitive Programming1ST EDSteven Halim and Felix Halim
University of Valladolid (Spain) Online Judge
8/29/15 ACM-‐ICPC 2015: BASIC PROBLEMS & SOLVING ALGORITHMS 73