algoparc - hawaii.edu
TRANSCRIPT
ICS 491: Competitve Programming – Lecture 1: Introduction
ICS 491: Competitive ProgrammingProf. Nodari Sitchinava
www.algoparc.ics.hawaii.edu
AlgoPARC
Lecture 1: Introduction
ICS 491: Competitve Programming – Lecture 1: Introduction
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
Improve problem solving skillsPrepare for ICPC programming competitionTechnical skill of a Computer Science major
Why bother?
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 SitchinavaEmail: [email protected] (Put “ICS 491” in the Subject)Office: POST 309COffice Hours: Wednesdays 3-4pm
TA: Branden OgataEmail: [email protected] (Put “ICS 491” in the Subject)Office: POST 314-6Office 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 311Fluency in Java, C or C++
ICS 491: Competitve Programming – Lecture 1: Introduction
Course Info
Prerequisites:‘B’ or better in ICS 311Fluency in Java, C or C++
Recommended Reading:Steven Halim and Felix Halim: Competitive Programming 3, 3rdEdition, Lulu Press, 2014. [CP3]Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest andClifford Stein, Introduction to Algorithms, 3rd Edition, The MITPress, 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.)
Practice problems on UVa Online Judge:
Assigned weekly, but not gradedPreparation for weekly mini-contestsNeed an account on https://uva.onlinejudge.org
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
Programming Environment
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)
2017-2018 ICPC environment without Python/Kotlin
Contests submissions:DOM Judge server (similar to UVa Online Judge)
JDK JavaDocsC++ STL docs
No internet access!
ICS 491: Competitve Programming – Lecture 1: Introduction
ICPC Regional Competition
On Saturday, November 3, 2018 in Laie, HIEligibility criteriahttps://icpc.baylor.edu/regionals/rules
If eligible and interestedTell 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 USBPress power button
ICS 491: Competitve Programming – Lecture 1: Introduction
Programming Environment
Boot from the provided USB:Insert USBPress 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 USBPress power button
username:password:
Open Firefox and visit https://judge.ics.hawaii.edu/domjudge/
Your home directory will be wiped clean at each login
Pick username wisely:Competition ranking by usernameWill 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
Accepted (AC)
Presentation Error (PE)Wrong Answer (WA)Time Limit Exceeded (TLE)Memory Limit Exceeded (MLE)Runtime Error (RTE)
Good:
Bad:
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}
java -Dfile.encoding=UTF-8 -XX:+UseSerialGC -Xss64m
-Xms1920m -Xmx1920m ${file}
Running Java:
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 elementsO(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
Use algorithm::partial_sort(A, A+K, A+N)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
Use algorithm::partial_sort(A, A+K, A+N)
O(N log K )
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
Use algorithm::partial_sort(A, A+K, A+N)
O(N log K )
O(N + K log K )
Which one is better?
ICS 491: Competitve Programming – Lecture 1: Introduction
Problem A: TopK, N ≤ 1M, K ≤ 100
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)
Modern processors: ≈ 100M = 108 ops per second
ICS 491: Competitve Programming – Lecture 1: Introduction
Problem A: TopK, N ≤ 1M, K ≤ 100
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)
Modern processors: ≈ 100M = 108 ops per second
Use the simplest algorithm within the time budget
ICS 491: Competitve Programming – Lecture 1: Introduction
Problem A: TopK, N ≤ 1M, K ≤ 100
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)
Modern processors: ≈ 100M = 108 ops per second
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 ·MFind 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 ·MFind M that minimizes cost(M)
Take a derivative and set to 0cost ′(M) = −K P/M2 + X = 0
ICS 491: Competitve Programming – Lecture 1: Introduction
Problem B: Delayed Work
Understand the problem
cost(M) = (K/M) · P + X ·MFind M that minimizes cost(M)
Take a derivative and set to 0cost ′(M) = −K P/M2 + X = 0
Solve for M⇒ K P/M2 = 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 ·MFind M that minimizes cost(M)
Take a derivative and set to 0cost ′(M) = −K P/M2 + X = 0
Solve for M⇒ K P/M2 = X⇒ M =
√K P/X
Return cost(M) = K P√K P/X
+ X√
K P/X =√
K PX +√
XK P
ICS 491: Competitve Programming – Lecture 1: Introduction
Problem B: Delayed Work
Understand the problem
cost(M) = (K/M) · P + X ·MFind M that minimizes cost(M)
Take a derivative and set to 0cost ′(M) = −K P/M2 + X = 0
Solve for M⇒ K P/M2 = X⇒ M =
√K P/X
Return cost(M) = K P√K P/X
+ X√
K P/X =√
K PX +√
XK P
WrongAnswer (WA)!
ICS 491: Competitve Programming – Lecture 1: Introduction
Problem B: Delayed Work
Understand the problem
cost(M) = (K/M) · P + X ·MFind M that minimizes cost(M)
Take a derivative and set to 0cost ′(M) = −K P/M2 + X = 0
Solve for M⇒ K P/M2 = X⇒ M =
√K P/X
Return cost(M) = K P√K P/X
+ X√
K P/X =√
K PX +√
XK P
WrongAnswer (WA)!
M must be integer
ICS 491: Competitve Programming – Lecture 1: Introduction
Problem B: Delayed Work
Understand the problem
cost(M) = (K/M) · P + X ·MFind M that minimizes cost(M)
Take a derivative and set to 0cost ′(M) = −K P/M2 + X = 0
Solve for M⇒ K P/M2 = X⇒ M =
√K P/X
Return min {cost(bMc), cost(dMe)}
= min{
K PbMc
+ XbMc, K PdMe
+ XdMe}
M must be integer
ICS 491: Competitve Programming – Lecture 1: Introduction
Problem B: Delayed Work
Understand the problem
cost(M) = (K/M) · P + X ·MFind M that minimizes cost(M)
Take a derivative and set to 0cost ′(M) = −K P/M2 + X = 0
Solve for M⇒ K P/M2 = X⇒ M =
√K P/X
Return min {cost(bMc), cost(dMe)}
= min{
K PbMc
+ XbMc, K PdMe
+ XdMe}
Don’t forget to format the output
M must be integer
ICS 491: Competitve Programming – Lecture 1: Introduction
Problem C: CardSorting
Card games are common in contestsThe problem is a simplest exerciseLearn to process strange inputsMap to integers and sort them
ICS 491: Competitve Programming – Lecture 1: Introduction
Problem C: CardSorting
Card games are common in contestsThe problem is a simplest exerciseLearn to process strange inputsMap to integers and sort them
x = 13 · suit + (value − 2)
suit intSpades 0Hearts 1Diamonds 2Clubs 3
face value2-9 2-9T (10) 10J (Jack) 11Q (Queen) 12K (King) 13A (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
O(|s|2) substringsO(|s|) time to check if a palindrom
O(|s|3) time
s3 = 1M ⇒ < 1second
Analysis
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-characterpalindrom
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-characterpalindrom
Much simpler solution:Scan the string and check if there are two identicalcharacters next to each otherOutput ”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-characterpalindrom
Much simpler solution:Scan the string and check if there are two identicalcharacters next to each otherOutput ”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-characterpalindrom
Much simpler solution:Scan the string and check if there are two identicalcharacters next to each otherOutput ”Not odd.” the moment they are found
Analysis:
A single scan: O(|s|)
Much easier (and faster) to code
ICS 491: Competitve Programming – Lecture 1: Introduction
Problem types
Class will cover many common problem typesMost rely on ICS 311 algorithms/data structures for efficientimplementations
Used as subroutinesDon’t need proofsOften library implementations existMust know asymptotic runtimes
ICS 491: Competitve Programming – Lecture 1: Introduction
Lessons learned
Understand the problem firstThe first solution might be too tedious to implementUse paperDraw examples
Do a quick runtime analysisOnly start implementing if close to passing RTE
Test programDon’t rely on sample testcasesFind difficult testcases.Try large testcases
Master Programming Language(s)Don’t waste time reading documentation
Practice, practice, practice...