algoparc - hawaii.edu

58
ICS 491: Competitve Programming – Lecture 1: Introduction ICS 491: Competitive Programming Prof. Nodari Sitchinava www.algoparc.ics.hawaii.edu AlgoPARC Lecture 1: Introduction

Upload: others

Post on 25-Dec-2021

8 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: AlgoPARC - hawaii.edu

ICS 491: Competitve Programming – Lecture 1: Introduction

ICS 491: Competitive ProgrammingProf. Nodari Sitchinava

www.algoparc.ics.hawaii.edu

AlgoPARC

Lecture 1: Introduction

Page 2: AlgoPARC - hawaii.edu

ICS 491: Competitve Programming – Lecture 1: Introduction

Competitive Programming?

Goal:

Learn to solve computational problems quickly and efficiently

Page 3: AlgoPARC - hawaii.edu

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

Page 4: AlgoPARC - hawaii.edu

ICS 491: Competitve Programming – Lecture 1: Introduction

UH Manoa ICPC Performance

2015

2017

Page 5: AlgoPARC - hawaii.edu

ICS 491: Competitve Programming – Lecture 1: Introduction

UH Manoa 2017 ICPC Performance

Page 6: AlgoPARC - hawaii.edu

ICS 491: Competitve Programming – Lecture 1: Introduction

UH Manoa 2017 ICPC Performance

Page 7: AlgoPARC - hawaii.edu

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

Page 8: AlgoPARC - hawaii.edu

ICS 491: Competitve Programming – Lecture 1: Introduction

Course Info

Page 9: AlgoPARC - hawaii.edu

ICS 491: Competitve Programming – Lecture 1: Introduction

Course Info

Prerequisites:‘B’ or better in ICS 311Fluency in Java, C or C++

Page 10: AlgoPARC - hawaii.edu

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]

Page 11: AlgoPARC - hawaii.edu

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)

Page 12: AlgoPARC - hawaii.edu

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

Page 13: AlgoPARC - hawaii.edu

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

Page 14: AlgoPARC - hawaii.edu

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!

Page 15: AlgoPARC - hawaii.edu

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

Page 16: AlgoPARC - hawaii.edu

ICS 491: Competitve Programming – Lecture 1: Introduction

Programming Environment

Boot from the provided USB:Insert USBPress power button

Page 17: AlgoPARC - hawaii.edu

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

Page 18: AlgoPARC - hawaii.edu

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

Page 19: AlgoPARC - hawaii.edu

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}

Page 20: AlgoPARC - hawaii.edu

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:

Page 21: AlgoPARC - hawaii.edu

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:

Page 22: AlgoPARC - hawaii.edu

ICS 491: Competitve Programming – Lecture 1: Introduction

Contest

Page 23: AlgoPARC - hawaii.edu

ICS 491: Competitve Programming – Lecture 1: Introduction

Problem A: TopK

Solutions:

Page 24: AlgoPARC - hawaii.edu

ICS 491: Competitve Programming – Lecture 1: Introduction

Problem A: TopK

Solutions:

Scan K times, looking for smallest and removing it

Page 25: AlgoPARC - hawaii.edu

ICS 491: Competitve Programming – Lecture 1: Introduction

Problem A: TopK

Solutions:

Scan K times, looking for smallest and removing it

O(K · N)

Page 26: AlgoPARC - hawaii.edu

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

Page 27: AlgoPARC - hawaii.edu

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 )

Page 28: AlgoPARC - hawaii.edu

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

Page 29: AlgoPARC - hawaii.edu

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)

Page 30: AlgoPARC - hawaii.edu

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

Page 31: AlgoPARC - hawaii.edu

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 )

Page 32: AlgoPARC - hawaii.edu

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 )

Page 33: AlgoPARC - hawaii.edu

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 )

Page 34: AlgoPARC - hawaii.edu

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?

Page 35: AlgoPARC - hawaii.edu

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

Page 36: AlgoPARC - hawaii.edu

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

Page 37: AlgoPARC - hawaii.edu

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

Page 38: AlgoPARC - hawaii.edu

ICS 491: Competitve Programming – Lecture 1: Introduction

Problem B: Delayed Work

Understand the problem

Page 39: AlgoPARC - hawaii.edu

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)

Page 40: AlgoPARC - hawaii.edu

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

Page 41: AlgoPARC - hawaii.edu

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

Page 42: AlgoPARC - hawaii.edu

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

Page 43: AlgoPARC - hawaii.edu

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)!

Page 44: AlgoPARC - hawaii.edu

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

Page 45: AlgoPARC - hawaii.edu

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

Page 46: AlgoPARC - hawaii.edu

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

Page 47: AlgoPARC - hawaii.edu

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

Page 48: AlgoPARC - hawaii.edu

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

Page 49: AlgoPARC - hawaii.edu

ICS 491: Competitve Programming – Lecture 1: Introduction

Problem D: OddPalindrom, |s| ≤ 100

Solution 1:

Page 50: AlgoPARC - hawaii.edu

ICS 491: Competitve Programming – Lecture 1: Introduction

Problem D: OddPalindrom, |s| ≤ 100

Solution 1:

Check every substring if it’s an even palindrom

Analysis

Page 51: AlgoPARC - hawaii.edu

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

Page 52: AlgoPARC - hawaii.edu

ICS 491: Competitve Programming – Lecture 1: Introduction

Problem D: OddPalindrom, |s| ≤ 100

Solution 2:

Page 53: AlgoPARC - hawaii.edu

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

Page 54: AlgoPARC - hawaii.edu

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

Page 55: AlgoPARC - hawaii.edu

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

Page 56: AlgoPARC - hawaii.edu

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

Page 57: AlgoPARC - hawaii.edu

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

Page 58: AlgoPARC - hawaii.edu

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...