Libraries Lecture 2: Data Structures and Standard
Transcript of 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
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
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
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
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:
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〉
ICS 491: Competitve Programming – Lecture 1: Introduction
Solutions
ICS 491: Competitve Programming – Lecture 1: Introduction
Ad Hoc Problems
GamesCardChessOther (Tic Tac Toe, Rock-Paper-Scissors, Bingo, etc)
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?
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
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
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
ICS 491: Competitve Programming – Lecture 1: Introduction
Other Ad Hoc Problems
Basic data structuresarrays, stacks, heap/PQ, deque, set, multiset, map, BST)
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
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
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
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)
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()
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
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
ICS 491: Competitve Programming – Lecture 1: Introduction
Working with Bits
C++ STL: bitset, vector<bool>Java: BitSet
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
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
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
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
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
ICS 491: Competitve Programming – Lecture 1: Introduction
Working with Bits
Toggle j-th bitS ^ (1 << j) ˆ is bitwise XOR
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
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
ICS 491: Competitve Programming – Lecture 1: Introduction
Linked Lists
C++ STL: listJava: LinkedList
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
ICS 491: Competitve Programming – Lecture 1: Introduction
Stack
C++ STL: stackJava: Stack
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
ICS 491: Competitve Programming – Lecture 1: Introduction
Queue
C++ STL: queueJava: Queue
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
ICS 491: Competitve Programming – Lecture 1: Introduction
Deque: double-ended queue
C++ STL: dequeJava: Deque
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
ICS 491: Competitve Programming – Lecture 1: Introduction
Balanced BST
C++ STL: map/setJava: TreeMap/TreeSet
Map: key-value pairSet: only keys
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
ICS 491: Competitve Programming – Lecture 1: Introduction
Priority Queue
C++ STL: priority queue
Java: PriorityQueue
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
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
ICS 491: Competitve Programming – Lecture 1: Introduction
Questions about Standard Library Data Structures?
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
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
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
ICS 491: Competitve Programming – Lecture 1: Introduction
Example: Computational Geometry
Simple solution:Check distance between every pairHint: sufficient to compare ||p1, p2||2
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
ICS 491: Competitve Programming – Lecture 1: Introduction
Computational Geometry: Better Solution
What if N = 100, 000?
ICS 491: Competitve Programming – Lecture 1: Introduction
Computational Geometry: Better Solution
What if N = 100, 000?Consider only points on the convex hull
ICS 491: Competitve Programming – Lecture 1: Introduction
Computational Geometry: Better Solution
What if N = 100, 000?Consider only points on the convex hull
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
ICS 491: Competitve Programming – Lecture 1: Introduction
Questions?