libraries lecture 2: data structures and standard

53
ICS 491: Competitve Programming – Lecture 1: Introduction ICS 491: Competitive Programming Prof. Nodari Sitchinava www.algoparc.ics.hawaii.edu AlgoPARC Lecture 2: Data Structures and Standard Libraries

Upload: others

Post on 07-Jan-2022

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Libraries Lecture 2: Data Structures and Standard

ICS 491: Competitve Programming – Lecture 1: Introduction

ICS 491: Competitive ProgrammingProf. Nodari Sitchinava

www.algoparc.ics.hawaii.edu

AlgoPARC

Lecture 2: Data Structures and StandardLibraries

Page 2: Libraries Lecture 2: Data Structures and Standard

ICS 491: Competitve Programming – Lecture 1: Introduction

A word of advice for (mini-) contests

Test your program before submitting. At the very least:Compile it!Run on sample test cases

Page 3: Libraries Lecture 2: Data Structures and Standard

ICS 491: Competitve Programming – Lecture 1: Introduction

A word of advice for (mini-) contests

Test your program before submitting. At the very least:Compile it!Run on sample test cases

./my_program < sample.in > myoutput.out

java MyProgram < sample.in > myoutput.out

diff myoutput.out sample.out

Page 4: Libraries Lecture 2: Data Structures and Standard

ICS 491: Competitve Programming – Lecture 1: Introduction

A word of advice for (mini-) contests

Test your program before submitting. At the very least:Compile it!Run on sample test cases

Test boundary conditionsSmallest inputLargest input

Progressively increase input sizeGenerate large inputs automatically

For multiple input variables (e.g., N, K ):Test if N > K and K > N (unless specified)Test smallest and largest for both

./my_program < sample.in > myoutput.out

java MyProgram < sample.in > myoutput.out

diff myoutput.out sample.out

Page 5: Libraries Lecture 2: Data Structures and Standard

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)

e.g. Array out of boundsTest boundary conditions & large inputs

Good:

Bad:

Page 6: Libraries Lecture 2: Data Structures and Standard

ICS 491: Competitve Programming – Lecture 1: Introduction

Weekly Mini-Contest – 75 min

Compiling:

C:gcc -g -O2 -std=gnu11 -static 〈file.c〉 -lm

C++:g++ -g -O2 -std=gnu++14 -static 〈file.cpp〉Java:javac -encoding UTF-8 -sourcepath . -d . 〈file.java〉

Running Java:java -Dfile.encoding=UTF-8 -XX:+UseSerialGC -Xss64m

-Xms1920m -Xmx1920m 〈file〉

Page 7: Libraries Lecture 2: Data Structures and Standard

ICS 491: Competitve Programming – Lecture 1: Introduction

Solutions

Page 8: Libraries Lecture 2: Data Structures and Standard

ICS 491: Competitve Programming – Lecture 1: Introduction

Ad Hoc Problems

GamesCardChessOther (Tic Tac Toe, Rock-Paper-Scissors, Bingo, etc)

Page 9: Libraries Lecture 2: Data Structures and Standard

ICS 491: Competitve Programming – Lecture 1: Introduction

Ad Hoc Problems

GamesCardChessOther (Tic Tac Toe, Rock-Paper-Scissors, Bingo, etc)

Palindromes - does a word read the same forward andbackward?

Page 10: Libraries Lecture 2: Data Structures and Standard

ICS 491: Competitve Programming – Lecture 1: Introduction

Ad Hoc Problems

GamesCardChessOther (Tic Tac Toe, Rock-Paper-Scissors, Bingo, etc)

Palindromes - does a word read the same forward andbackward?Anagrams - do words contain the same set of characters?

Sort characters of both words and compare

Page 11: Libraries Lecture 2: Data Structures and Standard

ICS 491: Competitve Programming – Lecture 1: Introduction

Ad Hoc Problems

GamesCardChessOther (Tic Tac Toe, Rock-Paper-Scissors, Bingo, etc)

Palindromes - does a word read the same forward andbackward?Anagrams - do words contain the same set of characters?

Sort characters of both words and compare

TimeDates, Times, CalendarJava GregorianCalendar is very helpful

Page 12: Libraries Lecture 2: Data Structures and Standard

ICS 491: Competitve Programming – Lecture 1: Introduction

Ad Hoc Problems

GamesCardChessOther (Tic Tac Toe, Rock-Paper-Scissors, Bingo, etc)

Palindromes - does a word read the same forward andbackward?Anagrams - do words contain the same set of characters?

Sort characters of both words and compare

TimeDates, Times, CalendarJava GregorianCalendar is very helpful

Time wastersSpecifically designed to be long and tedious

Page 13: Libraries Lecture 2: Data Structures and Standard

ICS 491: Competitve Programming – Lecture 1: Introduction

Other Ad Hoc Problems

Basic data structuresarrays, stacks, heap/PQ, deque, set, multiset, map, BST)

Page 14: Libraries Lecture 2: Data Structures and Standard

ICS 491: Competitve Programming – Lecture 1: Introduction

Other Ad Hoc Problems

Basic data structuresarrays, stacks, heap/PQ, deque, set, multiset, map, BST)

Mathbasic: arithmetic series, basic combinatorics, baseconversion, modulo operationsadvanced: number theory, advanced combinatorics,probability, game theory

Page 15: Libraries Lecture 2: Data Structures and Standard

ICS 491: Competitve Programming – Lecture 1: Introduction

Other Ad Hoc Problems

Basic data structuresarrays, stacks, heap/PQ, deque, set, multiset, map, BST)

Mathbasic: arithmetic series, basic combinatorics, baseconversion, modulo operationsadvanced: number theory, advanced combinatorics,probability, game theory

Strings

Page 16: Libraries Lecture 2: Data Structures and Standard

ICS 491: Competitve Programming – Lecture 1: Introduction

Other Ad Hoc Problems

Basic data structuresarrays, stacks, heap/PQ, deque, set, multiset, map, BST)

Mathbasic: arithmetic series, basic combinatorics, baseconversion, modulo operationsadvanced: number theory, advanced combinatorics,probability, game theory

Strings

(Computational) Geometry

Page 17: Libraries Lecture 2: Data Structures and Standard

ICS 491: Competitve Programming – Lecture 1: Introduction

Other Ad Hoc Problems

Mathbasic: arithmetic series, basic combinatorics, baseconversion, modulo operationsadvanced: number theory, advanced combinatorics,probability, game theory

Strings

(Computational) Geometry

Basic data structuresarrays, stacks, heap/PQ, deque, set, multiset, map, BST)

Page 18: Libraries Lecture 2: Data Structures and Standard

ICS 491: Competitve Programming – Lecture 1: Introduction

Standard Libraries: Linear Data Structures

Static Array1D, 2D, and 3D arraysOK to waste a bit extra space (for contests)

saves ‘off by one’ RTEs

Dynamic arraysC: mallocC++: STL vector

Java: ArrayListJava: Vector

Initialize size with reserve() or resize()

Page 19: Libraries Lecture 2: Data Structures and Standard

ICS 491: Competitve Programming – Lecture 1: Introduction

Algorithms for arrays

SortingC++ STL: sort, partial sort, stable sort

Java: Collections.sortO(n) sorting (Counting/Radix/Bucket Sort)

Must implement yourselfTypically just use O(n log n) library implementations

Page 20: Libraries Lecture 2: Data Structures and Standard

ICS 491: Competitve Programming – Lecture 1: Introduction

Algorithms for arrays

SortingC++ STL: sort, partial sort, stable sort

Java: Collections.sortO(n) sorting (Counting/Radix/Bucket Sort)

Must implement yourselfTypically just use O(n log n) library implementations

SearchingO(n) linear searchO(log n) binary search

C++ STL <algorithm>: lower bound, upper bound,binary search

Java: Collections.binarySearch

Page 21: Libraries Lecture 2: Data Structures and Standard

ICS 491: Competitve Programming – Lecture 1: Introduction

Working with Bits

C++ STL: bitset, vector<bool>Java: BitSet

Page 22: Libraries Lecture 2: Data Structures and Standard

ICS 491: Competitve Programming – Lecture 1: Introduction

Working with Bits

C++ STL: bitset, vector<bool>Java: BitSet

Faster to use bitmasks (bits within integers) instead:Use 32/64-bit integers for up to 30/62 bits

Page 23: Libraries Lecture 2: Data Structures and Standard

ICS 491: Competitve Programming – Lecture 1: Introduction

Working with Bits

C++ STL: bitset, vector<bool>Java: BitSet

Faster to use bitmasks (bits within integers) instead:Use 32/64-bit integers for up to 30/62 bitsRepresent the set {1, 5}

int S = 34 3410 = 1000102

Page 24: Libraries Lecture 2: Data Structures and Standard

ICS 491: Competitve Programming – Lecture 1: Introduction

Working with Bits

C++ STL: bitset, vector<bool>Java: BitSet

Faster to use bitmasks (bits within integers) instead:

Is j-th item present? Is j-th bit of S set?S & (1 << j) & is bitwise AND

Use 32/64-bit integers for up to 30/62 bitsRepresent the set {1, 5}

int S = 34 3410 = 1000102

Page 25: Libraries Lecture 2: Data Structures and Standard

ICS 491: Competitve Programming – Lecture 1: Introduction

Working with Bits

C++ STL: bitset, vector<bool>Java: BitSet

Faster to use bitmasks (bits within integers) instead:

Is j-th item present? Is j-th bit of S set?S & (1 << j) & is bitwise AND

Use 32/64-bit integers for up to 30/62 bitsRepresent the set {1, 5}

int S = 34 3410 = 1000102

Set j-th bit of SS | (1 << j) | is bitwise OR

Page 26: Libraries Lecture 2: Data Structures and Standard

ICS 491: Competitve Programming – Lecture 1: Introduction

Working with Bits

C++ STL: bitset, vector<bool>Java: BitSet

Faster to use bitmasks (bits within integers) instead:

Is j-th item present? Is j-th bit of S set?S & (1 << j) & is bitwise AND

Use 32/64-bit integers for up to 30/62 bitsRepresent the set {1, 5}

int S = 34 3410 = 1000102

Set j-th bit of SS | (1 << j) | is bitwise OR

Clear j-th bit of SS & ~(1 << j) ˜ is bitwise NOT

Page 27: Libraries Lecture 2: Data Structures and Standard

ICS 491: Competitve Programming – Lecture 1: Introduction

Working with Bits

Toggle j-th bitS ^ (1 << j) ˆ is bitwise XOR

Page 28: Libraries Lecture 2: Data Structures and Standard

ICS 491: Competitve Programming – Lecture 1: Introduction

Working with Bits

Toggle j-th bitS ^ (1 << j) ˆ is bitwise XOR

return the value with only LSB setS & (-S) 40 & (−40) = 8

0...001010002 & 1..110110002 = 0...000010002

Page 29: Libraries Lecture 2: Data Structures and Standard

ICS 491: Competitve Programming – Lecture 1: Introduction

Working with Bits

Toggle j-th bitS ^ (1 << j) ˆ is bitwise XOR

return the value with only LSB setS & (-S) 40 & (−40) = 8

0...001010002 & 1..110110002 = 0...000010002

Set k LSBs to 1 (k < 30 or k < 62 for 32/64-bit integers)S = (1 << k) - 1

Page 30: Libraries Lecture 2: Data Structures and Standard

ICS 491: Competitve Programming – Lecture 1: Introduction

Linked Lists

C++ STL: listJava: LinkedList

Page 31: Libraries Lecture 2: Data Structures and Standard

ICS 491: Competitve Programming – Lecture 1: Introduction

Linked Lists

C++ STL: listJava: LinkedList

Slower than vectors/deques.Use only if need to insert items in the middle

Page 32: Libraries Lecture 2: Data Structures and Standard

ICS 491: Competitve Programming – Lecture 1: Introduction

Stack

C++ STL: stackJava: Stack

Page 33: Libraries Lecture 2: Data Structures and Standard

ICS 491: Competitve Programming – Lecture 1: Introduction

Stack

C++ STL: stackJava: Stack

Supports O(1) time operations:push() – insert to toppop() – delete from toptop() – return the top (without deleting)empty() – check if empty

Page 34: Libraries Lecture 2: Data Structures and Standard

ICS 491: Competitve Programming – Lecture 1: Introduction

Queue

C++ STL: queueJava: Queue

Page 35: Libraries Lecture 2: Data Structures and Standard

ICS 491: Competitve Programming – Lecture 1: Introduction

Queue

C++ STL: queueJava: Queue

Supports O(1) time operations:push() – insert to backpop() – delete from frontfront() – return the front (without deleting)back() – return the back (without deleting)empty() – check if empty

Page 36: Libraries Lecture 2: Data Structures and Standard

ICS 491: Competitve Programming – Lecture 1: Introduction

Deque: double-ended queue

C++ STL: dequeJava: Deque

Page 37: Libraries Lecture 2: Data Structures and Standard

ICS 491: Competitve Programming – Lecture 1: Introduction

Deque: double-ended queue

C++ STL: dequeJava: Deque

Supports O(1) time operations:push back() – insert to backpop front() – delete from frontpush front() – insert to frontpop back() – delete from back

Page 38: Libraries Lecture 2: Data Structures and Standard

ICS 491: Competitve Programming – Lecture 1: Introduction

Balanced BST

C++ STL: map/setJava: TreeMap/TreeSet

Map: key-value pairSet: only keys

Page 39: Libraries Lecture 2: Data Structures and Standard

ICS 491: Competitve Programming – Lecture 1: Introduction

Balanced BST

C++ STL: map/setJava: TreeMap/TreeSet

Supports O(log n) time operations:search(key)

insert(key)

findMin()

findMax()

successor(key)

predecessor(key)

delete(key)

Map: key-value pairSet: only keys

Page 40: Libraries Lecture 2: Data Structures and Standard

ICS 491: Competitve Programming – Lecture 1: Introduction

Priority Queue

C++ STL: priority queue

Java: PriorityQueue

Page 41: Libraries Lecture 2: Data Structures and Standard

ICS 491: Competitve Programming – Lecture 1: Introduction

Priority Queue

C++ STL: priority queue

Java: PriorityQueue

Supports O(log n) time operations:ExtractMax()/pop() – remove and return largestelementInsert(v)/push()

top() – look up the largest element

Page 42: Libraries Lecture 2: Data Structures and Standard

ICS 491: Competitve Programming – Lecture 1: Introduction

Priority Queue

C++ STL: priority queue

Java: PriorityQueue

Supports O(log n) time operations:ExtractMax()/pop() – remove and return largestelementInsert(v)/push()

top() – look up the largest element

For MinHeap – insert negative elements

Page 43: Libraries Lecture 2: Data Structures and Standard

ICS 491: Competitve Programming – Lecture 1: Introduction

Questions about Standard Library Data Structures?

Page 44: Libraries Lecture 2: Data Structures and Standard

ICS 491: Competitve Programming – Lecture 1: Introduction

Other Ad Hoc problems

Basic data structuresarrays, stacks, heap/PQ, deque, set, multiset, map, BST)

Mathbasic: arithmetic series, basic combinatorics, baseconversion, modulo operationsadvanced: number theory, advanced combinatorics,probability, game theory

Strings

(Computational) Geometry

Page 45: Libraries Lecture 2: Data Structures and Standard

ICS 491: Competitve Programming – Lecture 1: Introduction

Example: Computational Geometry

Not covered in ICS 311At least one problem in ICPCOften hard to get right, especially without preparation

Page 46: Libraries Lecture 2: Data Structures and Standard

ICS 491: Competitve Programming – Lecture 1: Introduction

Example: Computational Geometry

Not covered in ICS 311At least one problem in ICPCOften hard to get right, especially without preparation

Problem 1. Given a set of N points randomly scattered on a 2Dplane, find the pair of points with the greatest separatingEuclidean distance.

Points are defined by (x , y ) coordinates.Euclidean distance between p1 = (x1, y1) and p2 = (x2, y2) isdefined as ||p1, p2|| =

√(x1 − x2)2 + (y1 − y2)2

Page 47: Libraries Lecture 2: Data Structures and Standard

ICS 491: Competitve Programming – Lecture 1: Introduction

Example: Computational Geometry

Simple solution:Check distance between every pairHint: sufficient to compare ||p1, p2||2

Page 48: Libraries Lecture 2: Data Structures and Standard

ICS 491: Competitve Programming – Lecture 1: Introduction

Example: Computational Geometry

Simple solution:Check distance between every pairHint: sufficient to compare ||p1, p2||2

Analysis:Number of pairs: O(n2)Fine if N ≤ 1, 000

Page 49: Libraries Lecture 2: Data Structures and Standard

ICS 491: Competitve Programming – Lecture 1: Introduction

Computational Geometry: Better Solution

What if N = 100, 000?

Page 50: Libraries Lecture 2: Data Structures and Standard

ICS 491: Competitve Programming – Lecture 1: Introduction

Computational Geometry: Better Solution

What if N = 100, 000?Consider only points on the convex hull

Page 51: Libraries Lecture 2: Data Structures and Standard

ICS 491: Competitve Programming – Lecture 1: Introduction

Computational Geometry: Better Solution

What if N = 100, 000?Consider only points on the convex hull

Page 52: Libraries Lecture 2: Data Structures and Standard

ICS 491: Competitve Programming – Lecture 1: Introduction

Computational Geometry: Better Solution

What if N = 100, 000?Consider only points on the convex hull

Analysis: Expected ≈ O(√

N) points on the convex hull

Page 53: Libraries Lecture 2: Data Structures and Standard

ICS 491: Competitve Programming – Lecture 1: Introduction

Questions?