AlgoPARC
ICS 491: Competitive Programming
Prof. Nodari Sitchinava
Lecture 1: Introduction
ICS 491: Competitve Programming – Lecture 1: Introduction www.algoparc.ics.hawaii.edu
Competitive Programming?
Goal:
Learn to solve computational problems quickly and efficiently
ICS 491: Competitve Programming – Lecture 1: Introduction
Competitive Programming?
Goal:
Learn to solve computational problems quickly and efficiently
Why bother?
Improve problem solving skills
Prepare for ICPC programming competition
Technical skill of a Computer Science major
ICPC website: https://icpc.baylor.edu
ICS 491: Competitve Programming – Lecture 1: Introduction
UH Manoa ICPC Performance
2015
2017
ICS 491: Competitve Programming – Lecture 1: Introduction
UH Manoa 2017 ICPC Performance
ICS 491: Competitve Programming – Lecture 1: Introduction
UH Manoa 2017 ICPC Performance
ICS 491: Competitve Programming – Lecture 1: Introduction
Course Info
Course Website:
http://www2.hawaii.edu/~nodari/teaching/f18/
Instructor: Nodari Sitchinava
Email:
[email protected] (Put “ICS 491” in the Subject)
Office: POST 309C
Office Hours: Wednesdays 3-4pm
TA: Branden Ogata
Email:
[email protected] (Put “ICS 491” in the Subject)
Office: POST 314-6
Office Hours: Tuesday 2-3pm and Thursday 3-4pm
ICS 491: Competitve Programming – Lecture 1: Introduction
Course Info
ICS 491: Competitve Programming – Lecture 1: Introduction
Course Info
Prerequisites:
‘B’ or better in ICS 311
Fluency in Java, C or C++
ICS 491: Competitve Programming – Lecture 1: Introduction
Course Info
Prerequisites:
‘B’ or better in ICS 311
Fluency in Java, C or C++
Recommended Reading:
Steven Halim and Felix Halim: Competitive Programming 3, 3rd
Edition, Lulu Press, 2014. [CP3]
Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest and
Clifford Stein, Introduction to Algorithms, 3rd Edition, The MIT
Press, 2009 [CLRS]
ICS 491: Competitve Programming – Lecture 1: Introduction
Course Info (cont.)
Grading:
70%: Weekly mini-contests (75 min)
15%: Midterm contest (150 min)
15%: Final contest (120 min)
ICS 491: Competitve Programming – Lecture 1: Introduction
Course Info (cont.)
Grading:
70%: Weekly mini-contests (75 min)
15%: Midterm contest (150 min)
15%: Final contest (120 min)
Cheating will result in an immediate F
ICS 491: Competitve Programming – Lecture 1: Introduction
Course Info (cont.)
Grading:
70%: Weekly mini-contests (75 min)
15%: Midterm contest (150 min)
15%: Final contest (120 min)
Cheating will result in an immediate F
Practice problems on UVa Online Judge:
Assigned weekly, but not graded
Preparation for weekly mini-contests
Need an account on https://uva.onlinejudge.org
ICS 491: Competitve Programming – Lecture 1: Introduction
Programming Environment
2017-2018 ICPC environment without Python/Kotlin
OS: Ubuntu 16.04 with GNOME (on USB)
Languages:
Java 8 (OpenJDK 1.8)
C (gcc v5.4 with gnu11 extensions)
C++ (g++ v5.4 with gnu++14 extensions)
JDK JavaDocs
C++ STL docs
Contests submissions:
DOM Judge server (similar to UVa Online Judge)
No internet access!
ICS 491: Competitve Programming – Lecture 1: Introduction
ICPC Regional Competition
On Saturday, November 3, 2018 in Laie, HI
Eligibility criteria
https://icpc.baylor.edu/regionals/rules
If eligible and interested
Tell me (via email) by the end of Sept 30, 2018
Potential team selection competition on
Saturday, Oct 20, 2018
ICS 491: Competitve Programming – Lecture 1: Introduction
Programming Environment
Boot from the provided USB:
Insert USB
Press power button
ICS 491: Competitve Programming – Lecture 1: Introduction
Programming Environment
Boot from the provided USB:
Insert USB
Press power button
username:
password:
Your home directory will be wiped clean at each login
ICS 491: Competitve Programming – Lecture 1: Introduction
Programming Environment
Boot from the provided USB:
Insert USB
Press power button
username:
password:
Your home directory will be wiped clean at each login
Open Firefox and visit https://judge.ics.hawaii.edu/domjudge/
Pick username wisely:
Competition ranking by username
Will be public information
ICS 491: Competitve Programming – Lecture 1: Introduction
Familiarize yourself with the environment
E.g.: write a program that prints ”Hello World!” and exits
Compile using one of the following:
C:
gcc -g -O2 -std=gnu11 -static ${files} -lm
C++:
g++ -g -O2 -std=gnu++14 -static ${files}
Java:
javac -encoding UTF-8 -sourcepath . -d . ${files}
And run/test your program
For Java, run it with:
java -Dfile.encoding=UTF-8 -XX:+UseSerialGC -Xss64m
-Xms1920m -Xmx1920m ${file}
ICS 491: Competitve Programming – Lecture 1: Introduction
DomJudge Responses
Good:
Accepted (AC)
Bad:
Presentation Error (PE)
Wrong Answer (WA)
Time Limit Exceeded (TLE)
Memory Limit Exceeded (MLE)
Runtime Error (RTE)
ICS 491: Competitve Programming – Lecture 1: Introduction
Sample Contest – 45 min
Compiling:
C:
gcc -g -O2 -std=gnu11 -static ${files} -lm
C++:
g++ -g -O2 -std=gnu++14 -static ${files}
Java:
javac -encoding UTF-8 -sourcepath . -d . ${files}
Running Java:
java -Dfile.encoding=UTF-8 -XX:+UseSerialGC -Xss64m
-Xms1920m -Xmx1920m ${file}
ICS 491: Competitve Programming – Lecture 1: Introduction
Contest
ICS 491: Competitve Programming – Lecture 1: Introduction
Problem A: TopK
Solutions:
ICS 491: Competitve Programming – Lecture 1: Introduction
Problem A: TopK
Solutions:
Scan K times, looking for smallest and removing it
ICS 491: Competitve Programming – Lecture 1: Introduction
Problem A: TopK
Solutions:
Scan K times, looking for smallest and removing it
O(K · N)
ICS 491: Competitve Programming – Lecture 1: Introduction
Problem A: TopK
Solutions:
Scan K times, looking for smallest and removing it
O(K · N)
Sort, report first K
ICS 491: Competitve Programming – Lecture 1: Introduction
Problem A: TopK
Solutions:
Scan K times, looking for smallest and removing it
O(K · N)
Sort, report first K
O(N log N + K )
ICS 491: Competitve Programming – Lecture 1: Introduction
Problem A: TopK
Solutions:
Scan K times, looking for smallest and removing it
O(K · N)
Sort, report first K
O(N log N + K )
Build a heap, extract min K times
ICS 491: Competitve Programming – Lecture 1: Introduction
Problem A: TopK
Solutions:
Scan K times, looking for smallest and removing it
O(K · N)
Sort, report first K
O(N log N + K )
Build a heap, extract min K times
O(N + K log N)
ICS 491: Competitve Programming – Lecture 1: Introduction
Problem A: TopK
Solutions:
Scan K times, looking for smallest and removing it
O(K · N)
Sort, report first K
O(N log N + K )
Build a heap, extract min K times
O(N + K log N)
Partition around K -th smallest element, sort first K elements
ICS 491: Competitve Programming – Lecture 1: Introduction
Problem A: TopK
Solutions:
Scan K times, looking for smallest and removing it
O(K · N)
Sort, report first K
O(N log N + K )
Build a heap, extract min K times
O(N + K log N)
Partition around K -th smallest element, sort first K elements
O(N + K log K )
ICS 491: Competitve Programming – Lecture 1: Introduction
Problem A: TopK
Solutions:
Scan K times, looking for smallest and removing it
O(K · N)
Sort, report first K
O(N log N + K )
Build a heap, extract min K times
O(N + K log N)
Partition around K -th smallest element, sort first K elements
O(N + K log K )
Use algorithm::partial_sort(A, A+K, A+N)
ICS 491: Competitve Programming – Lecture 1: Introduction
Problem A: TopK
Solutions:
Scan K times, looking for smallest and removing it
O(K · N)
Sort, report first K
O(N log N + K )
Build a heap, extract min K times
O(N + K log N)
Partition around K -th smallest element, sort first K elements
O(N + K log K )
Use algorithm::partial_sort(A, A+K, A+N)
O(N log K )
ICS 491: Competitve Programming – Lecture 1: Introduction
Problem A: TopK
Solutions:
Scan K times, looking for smallest and removing it
O(K · N)
Sort, report first K
O(N log N + K )
Build a heap, extract min K times
O(N + K log N)
Partition around K -th smallest element, sort first K elements
O(N + K log K )
Use algorithm::partial_sort(A, A+K, A+N)
O(N log K )
Which one is better?
ICS 491: Competitve Programming – Lecture 1: Introduction
Problem A: TopK, N ≤ 1M, K ≤ 100
Modern processors: ≈ 100M = 108 ops per second
n Worst-case AC Algorithm
≤ [10..11] O(n!), O(n6 )
≤ [15..18] O(2n · n2 )
≤ [18..22] O(2n · n)
≤ 100 O(n4 )
≤ 400 O(n3 )
≤ 2K O(n2 log2 n)
≤ 10K O(n2 )
≤ 1M O(n log2 n)
≤ 100M O(n)
ICS 491: Competitve Programming – Lecture 1: Introduction
Problem A: TopK, N ≤ 1M, K ≤ 100
Modern processors: ≈ 100M = 108 ops per second
n Worst-case AC Algorithm
≤ [10..11] O(n!), O(n6 )
≤ [15..18] O(2n · n2 )
≤ [18..22] O(2n · n)
≤ 100 O(n4 )
≤ 400 O(n3 )
≤ 2K O(n2 log2 n)
≤ 10K O(n2 )
≤ 1M O(n log2 n)
≤ 100M O(n)
Use the simplest algorithm within the time budget
ICS 491: Competitve Programming – Lecture 1: Introduction
Problem A: TopK, N ≤ 1M, K ≤ 100
Modern processors: ≈ 100M = 108 ops per second
n Worst-case AC Algorithm
≤ [10..11] O(n!), O(n6 )
≤ [15..18] O(2n · n2 )
≤ [18..22] O(2n · n)
≤ 100 O(n4 )
≤ 400 O(n3 )
≤ 2K O(n2 log2 n)
≤ 10K O(n2 )
≤ 1M O(n log2 n)
≤ 100M O(n)
Use the simplest algorithm within the time budget
O(N log K ) ≈ 7M finishes within 1 second
ICS 491: Competitve Programming – Lecture 1: Introduction
Problem B: Delayed Work
Understand the problem
ICS 491: Competitve Programming – Lecture 1: Introduction
Problem B: Delayed Work
Understand the problem
cost(M) = (K /M) · P + X · M
Find M that minimizes cost(M)
ICS 491: Competitve Programming – Lecture 1: Introduction
Problem B: Delayed Work
Understand the problem
cost(M) = (K /M) · P + X · M
Find M that minimizes cost(M)
Take a derivative and set to 0
cost 0 (M) = −K P /M 2 + X = 0
ICS 491: Competitve Programming – Lecture 1: Introduction
Problem B: Delayed Work
Understand the problem
cost(M) = (K /M) · P + X · M
Find M that minimizes cost(M)
Take a derivative and set to 0
cost 0 (M) = −K P /M 2 + X = 0
Solve for M
2
⇒ K P /Mp =X
⇒ M = K P /X
ICS 491: Competitve Programming – Lecture 1: Introduction
Problem B: Delayed Work
Understand the problem
cost(M) = (K /M) · P + X · M
Find M that minimizes cost(M)
Take a derivative and set to 0
cost 0 (M) = −K P /M 2 + X = 0
Solve for M
2
⇒ K P /Mp =X
⇒ M = K P /X
p q √
Return cost(M) = √K P +X K P /X = KP
X
+ XKP
K P /X
ICS 491: Competitve Programming – Lecture 1: Introduction
Problem B: Delayed Work
Understand the problem
cost(M) = (K /M) · P + X · M
Find M that minimizes cost(M)
Take a derivative and set to 0
cost 0 (M) = −K P /M 2 + X = 0
Solve for M
2
⇒ K P /Mp =X
⇒ M = K P /X
p q √
Return cost(M) = √K P +X K P /X = KP
X
+ XKP
K P /X
W r ongAnswer (WA)!
ICS 491: Competitve Programming – Lecture 1: Introduction
Problem B: Delayed Work
Understand the problem
cost(M) = (K /M) · P + X · M
Find M that minimizes cost(M)
Take a derivative and set to 0
cost 0 (M) = −K P /M 2 + X = 0
Solve for M
2
⇒ K P /Mp =X M must be integer
⇒ M = K P /X
p q √
Return cost(M) = √K P +X K P /X = KP
X
+ XKP
K P /X
W r ongAnswer (WA)!
ICS 491: Competitve Programming – Lecture 1: Introduction
Problem B: Delayed Work
Understand the problem
cost(M) = (K /M) · P + X · M
Find M that minimizes cost(M)
Take a derivative and set to 0
cost 0 (M) = −K P /M 2 + X = 0
Solve for M
2
⇒ K P /Mp =X M must be integer
⇒ M = K P /X
Return min {cost(bM c), cost(dM e)}
KP KP
= min + X bM c, + X dM e
bM c dM e
ICS 491: Competitve Programming – Lecture 1: Introduction
Problem B: Delayed Work
Understand the problem
cost(M) = (K /M) · P + X · M
Find M that minimizes cost(M)
Take a derivative and set to 0
cost 0 (M) = −K P /M 2 + X = 0
Solve for M
2
⇒ K P /Mp =X M must be integer
⇒ M = K P /X
Return min {cost(bM c), cost(dM e)}
KP KP
= min + X bM c, + X dM e
bM c dM e
Don’t forget to format the output
ICS 491: Competitve Programming – Lecture 1: Introduction
Problem C: CardSorting
Card games are common in contests
The problem is a simplest exercise
Learn to process strange inputs
Map to integers and sort them
ICS 491: Competitve Programming – Lecture 1: Introduction
Problem C: CardSorting
Card games are common in contests
The problem is a simplest exercise
Learn to process strange inputs
Map to integers and sort them
x = 13 · suit + (value − 2)
face value
suit int 2-9 2-9
Spades 0 T (10) 10
Hearts 1 J (Jack) 11
Diamonds 2 Q (Queen) 12
Clubs 3 K (King) 13
A (Ace) 14
ICS 491: Competitve Programming – Lecture 1: Introduction
Problem D: OddPalindrom, |s| ≤ 100
Solution 1:
ICS 491: Competitve Programming – Lecture 1: Introduction
Problem D: OddPalindrom, |s| ≤ 100
Solution 1:
Check every substring if it’s an even palindrom
Analysis
ICS 491: Competitve Programming – Lecture 1: Introduction
Problem D: OddPalindrom, |s| ≤ 100
Solution 1:
Check every substring if it’s an even palindrom
Analysis
O(|s|2 ) substrings
O(|s|) time to check if a palindrom
O(|s|3 ) time
s3 = 1M ⇒ < 1second
ICS 491: Competitve Programming – Lecture 1: Introduction
Problem D: OddPalindrom, |s| ≤ 100
Solution 2:
ICS 491: Competitve Programming – Lecture 1: Introduction
Problem D: OddPalindrom, |s| ≤ 100
Solution 2:
Claim: If the string is not odd, then it contains a 2-character
palindrom
ICS 491: Competitve Programming – Lecture 1: Introduction
Problem D: OddPalindrom, |s| ≤ 100
Solution 2:
Claim: If the string is not odd, then it contains a 2-character
palindrom
Much simpler solution:
Scan the string and check if there are two identical
characters next to each other
Output ”Not odd.” the moment they are found
ICS 491: Competitve Programming – Lecture 1: Introduction
Problem D: OddPalindrom, |s| ≤ 100
Solution 2:
Claim: If the string is not odd, then it contains a 2-character
palindrom
Much simpler solution:
Scan the string and check if there are two identical
characters next to each other
Output ”Not odd.” the moment they are found
Much easier (and faster) to code
ICS 491: Competitve Programming – Lecture 1: Introduction
Problem D: OddPalindrom, |s| ≤ 100
Solution 2:
Claim: If the string is not odd, then it contains a 2-character
palindrom
Much simpler solution:
Scan the string and check if there are two identical
characters next to each other
Output ”Not odd.” the moment they are found
Much easier (and faster) to code
Analysis:
A single scan: O(|s|)
ICS 491: Competitve Programming – Lecture 1: Introduction
Problem types
Class will cover many common problem types
Most rely on ICS 311 algorithms/data structures for efficient
implementations
Used as subroutines
Don’t need proofs
Often library implementations exist
Must know asymptotic runtimes
ICS 491: Competitve Programming – Lecture 1: Introduction
Lessons learned
Understand the problem first
The first solution might be too tedious to implement
Use paper
Draw examples
Do a quick runtime analysis
Only start implementing if close to passing RTE
Test program
Don’t rely on sample testcases
Find difficult testcases.
Try large testcases
Master Programming Language(s)
Don’t waste time reading documentation
Practice, practice, practice...
ICS 491: Competitve Programming – Lecture 1: Introduction