0% found this document useful (0 votes)
126 views35 pages

Art of Programming Contest SE For Uva PDF

This document provides a summary of a lecture on data structures and competitive programming. It discusses linear data structures like arrays, vectors, stacks and queues that are supported by built-in libraries in C++ and Java. It also covers non-linear data structures like binary search trees, heaps and hash tables. The document recommends using built-in libraries for these structures where possible for competitive programming problems. It concludes with an overview of coding style recommendations from TopCoder for formatting, I/O, and using common data types and algorithms in a competitive programming context.

Uploaded by

gaurav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
126 views35 pages

Art of Programming Contest SE For Uva PDF

This document provides a summary of a lecture on data structures and competitive programming. It discusses linear data structures like arrays, vectors, stacks and queues that are supported by built-in libraries in C++ and Java. It also covers non-linear data structures like binary search trees, heaps and hash tables. The document recommends using built-in libraries for these structures where possible for competitive programming problems. It concludes with an overview of coding style recommendations from TopCoder for formatting, I/O, and using common data types and algorithms in a competitive programming context.

Uploaded by

gaurav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 35

This course material is now made available for public usage.

Special acknowledgement to School of Computing, National University of Singapore


for allowing Steven to prepare and distribute these teaching materials.

CS3233
CompetitiveProgramming
p
g
g
Dr.StevenHalim
Dr.
Steven Halim
Week02 DataStructures&Libraries
FocusonBitManipulation&BinaryIndexedTree
CS3233 CompetitiveProgramming,
StevenHalim,SoC,NUS

Outline
MiniContest1+Break(discussionofA/B)
/
SomeAdmins
DataStructuresWithBuiltinLibraries
Justaquickwalkthrough
Read/experimentwiththedetailsonyourown
Read/experiment with the details on your own

LinearDataStructures(CS1010/1st halfofCS2020)
nd halfofCS2020))
NonLinearDataStructures(CS2010/2
(
Focusontheredhighlights

TopCoderCodingStyle (overview) +Break


DataStructuresWithOurOwnLibraries
FocusonBinaryIndexed(Fenwick)Tree
CS3233 CompetitiveProgramming,
StevenHalim,SoC,NUS

Basic knowledge that all ICPC/IOIers


BasicknowledgethatallICPC/IOI
ersmusthave!
must have!

LINEARDATASTRUCTURES
WITHBUILTINLIBRARIES
CS3233 CompetitiveProgramming,
StevenHalim,SoC,NUS

I am
Iam
1. ApureCcoder
2. ApureC++coder
A pure C++ coder
3. Amixbetween
C/C
C/C++coder
d
4. ApureJavacoder
p
5. Amultilingual
coder:C/C++/Java
d C/C++/J
0
0 of 120

CS3233 CompetitiveProgramming,
1
StevenHalim,SoC,NUS

0
5

Linear DS + Built In Libraries (1)


LinearDS+BuiltInLibraries(1)
1. StaticArray,builtinsupportinC/C++/Java
2. Resize
Resizeable:
able:C++STLvector,JavaVector
C++ STL vector, Java Vector
BothareveryusefulinICPCs/IOIs

Thereare2verycommonoperationsonArray:
There are 2 very common operations on Array:
Sorting
Searching
S
hi
Letstakealookatefficientwaystodothem
CS3233 CompetitiveProgramming,
StevenHalim,SoC,NUS

Two fundamental
Two
fundamental CSproblems
CS problems

SORTING+SEARCHING
INVOLVINGARRAY
CS3233 CompetitiveProgramming,
StevenHalim,SoC,NUS

Sorting (1)
Sorting(1)
Definition:
Givenunsortedstuffs,sortthem*
,

PopularSortingAlgorithms
O(n
O( 2)algorithms:Bubble/Selection/InsertionSort
) l ih
B bbl /S l i /I
i S
O(nlogn)algorithms:Merge/Quick^/HeapSort
Specialpurpose:Counting/Radix/BucketSort

Reference:
http://en.wikipedia.org/wiki/Sorting_algorithm
CS3233 CompetitiveProgramming,
StevenHalim,SoC,NUS

Sorting (2)
Sorting(2)
InICPC,youcanforgetallthese
Ingeneral,ifyouneedtosortsomething,
g
, y
g ,
justusetheO(nlogn)sortinglibrary:
C
C++STLalgorithm::sort
STL algorithm:: sort
JavaCollections.sort

In
InICPC,sortingiseitherusedaspreliminarystep
ICPC sorting is either used as preliminary step
formorecomplexalgorithmortobeautifyoutput
Familiaritywithsortinglibrariesisamust!
Familiarity with sorting libraries is a must!

CS3233 CompetitiveProgramming,
StevenHalim,SoC,NUS

Sorting (3)
Sorting(3)
SortingroutinesinC++STLalgorithm
sort abugfreeimplementationofintrosort*
g
p
Fast,itrunsinO(nlogn)
Cansortbasicdatatypes(ints,doubles,chars),Abstract
Can sort basic data types (ints, doubles, chars), Abstract
DataTypes(C++class),multifieldsorting(2criteria)

partial_sort
partial sort implementationofheapsort
implementation of heapsort
CandoO(klogn)sorting,ifwejustneedtopksorted!

stable_sort
stable sort
Ifyouneedtohavethesortingstable,keyswithsame
values appear in the same order as in input
valuesappearinthesameorderasininput
CS3233 CompetitiveProgramming,
StevenHalim,SoC,NUS

Searching in Array
SearchinginArray
Twovariants:
Whenthearrayissortedversusnotsorted
y

MustdoO(n)linearscanifnotsorted trivial
CanuseO(logn)binarysearchwhensorted
(
)
PS:mustrunanO(nlogn)sortingalgorithmonce
(
g )
g g

Binarysearchistrickytocode!
Instead,useC++STLalgorithm::lower_bound
I t d
C STL l ith l
b
d

CS3233 CompetitiveProgramming,
StevenHalim,SoC,NUS

Linear DS + Built In Libraries (2)


LinearDS+BuiltInLibraries(2)
3. ArrayofBoolean:C++STLbitset
Fasterthanarrayofbools
y
orvector<bool>!
NospecificAPIinJavathatissimilartothis

4 Bitmask
4.
Bit
k
a.k.a.lightweightsetofBooleanorbitstring
Explanationvia:
http://www.comp.nus.edu.sg/~stevenha/visualization/bitmask.html

CS3233 CompetitiveProgramming,
StevenHalim,SoC,NUS

Linear DS + Built In Libraries (3)


LinearDS+BuiltInLibraries(3)
5. LinkedList,C++STLlist,JavaLinkedList
UsuallynotusedinICPCs/IOIs
y
/
Ifyouneedaresizeable list,justusevector!

6 Stack,C++STLstack,JavaStack
6.
St k C STL t k J
St k
UsedbydefaultinRecursion,PostfixCalculation,
BracketMatching,etc

7. Queue,C
Queue, C++STLqueue,JavaQueue
STL queue, Java Queue
UsedinBreadthFirstSearch,TopologicalSort,etc
PS:Deque,usedinSlidingWindowalgorithm
PS D
d i Slidi Wi d l ith
CS3233 CompetitiveProgramming,
StevenHalim,SoC,NUS

More efficient data structures


Moreefficientdatastructures

NONLINEARDATASTRUCTURES
WITHBUILTINLIBRARIES
CS3233 CompetitiveProgramming,
StevenHalim,SoC,NUS

Binary Search Tree (1)


BinarySearchTree(1)
ADTTable(key data)
BinarySearchTree(BST)
Binary Search Tree (BST)
AdvertisedO(logn)forinsert,search,anddelete
Requirement:theBSTmustbebalanced!
R
i
h BST
b b l
d!
AVLtree,RedBlackTree,etc*argh*

Fretnot,justuse:C++STLmap(JavaTreeMap)
UVa10226
UVa 10226 (HardwoodSpecies)
(Hardwood Species)*

CS3233 CompetitiveProgramming,
StevenHalim,SoC,NUS

Binary Search Tree (2)


BinarySearchTree(2)
ADTTable(keyexistsornot)
Set(SingleSet)
Set (Single Set)
C++STLset,similartoC++STLmap
mapstoresa(key,data)
t
(k d t ) pair
i
setstoresjustthekey

InJava:TreeSet

Example:
p
UVa 11849 CD
CS3233 CompetitiveProgramming,
StevenHalim,SoC,NUS

Heap
Heap
C++STLalgorithm
g
hassomeheapalgorithms
p g
partial_sort usesheapsort

C++STLpriority_queue
C++ STL priority queue (JavaPriorityQueue)isheap
(Java PriorityQueue) is heap
PrimsandDijkstras algorithmsusepriorityqueue

But,werarelyseepureheapproblemsinICPC
B t
l
h
bl
i ICPC

CS3233 CompetitiveProgramming,
StevenHalim,SoC,NUS

Hash Table
HashTable
HashTable
AdvertisedO(1)forinsert,search,anddelete,but:
( )
,
,
,
Thehashfunctionmustbegood!
ThereisnoHashTableinC++STL(
There is no Hash Table in C++ STL ( inJavaAPI)
in Java API)

Nevertheless,O(logn)usingmap isusuallyok

DirectAddressingTable(DAT)
Di t Add
i T bl (DAT)
Ratherthanhashing,wemorefrequentlyuseDAT
UVa11340 (Newspaper)
CS3233 CompetitiveProgramming,
StevenHalim,SoC,NUS

Top Coder Coding Style


TopCoderCodingStyle

SUPPLEMENTARY

CS3233 CompetitiveProgramming,
StevenHalim,SoC,NUS

Top Coder Coding Style (1)


TopCoderCodingStyle(1)
Youmaywanttofollowthiscodingstyle(C++)
1. Includeimportant
Include important headers
headers

#include <algorithm>
#include <cmath>
#include <cstdio>
cstdio
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#i l d <
#include
<vector>
t >
using namespace std;

Want More?
Add libraries that you frequently
use into this template, e.g.:
ctype.h
t
h
bitset
etc

CS3233 CompetitiveProgramming,
StevenHalim,SoC,NUS

Top Coder Coding Style (2)


TopCoderCodingStyle(2)
2. Useshortcutsforcommondatatypes

typedef
typedef
typedef
typedef

long long
vector<int>
pair<int, int>
vector<ii>

ll;
vi;
ii;
vii;

3 SimplifyRepetitions/Loops!
3.
Si lif R titi /L
!

#define REP(i, a, b)
for (int i = int(a); i <= int(b); i++)
#define REPN(i, n)
REP (i, 1, int(n))
#define REPD(i, a, b)
for (int i = int(a); i >= int(b); i--)
#define TRvi(c, it) \
for (vi::iterator it = (c).begin(); it != (c).end(); it++)
#define TRvii(c,
TRvii(c it) \
for (vii::iterator it = (c).begin(); it != (c).end(); it++)

Define your own loops


style and stick with it!
CS3233 CompetitiveProgramming,
StevenHalim,SoC,NUS

Top Coder Coding Style (3)


TopCoderCodingStyle(3)
4. Moreshortcuts

for (i = ans = 0; i < n; i++) // do variable assignment in for loop


while (scanf(
(scanf("%d",
%d , n), n) { // read input + do value test together
while (scanf("%d", n) != EOF) { // read input and do EOF test

5. STL/Librariesalltheway!
/
y

isalpha (ctype.h)
inline bool isletter(char c) {
return (c>='A'&&c<='Z')||(c>='a'&&c<='z'); }
abs (math.h)
inline int abs(int a) { return a >= 0 ? a : -a; }
pow (math.h)
a int b) {
int power(int a,
int res=1; for (; b>=1; b--) res*=a; return res; }

UseSTLdatastructures:vector,stack,queue,priority_queue,map,set,etc
UseSTLalgorithms:sort,lower
g
,
_bound,max,min,max
,
,
,
_element,next
,
_p
permutation,etc
,
CS3233 CompetitiveProgramming,
StevenHalim,SoC,NUS

Top Coder Coding Style (4)


TopCoderCodingStyle(4)
6. UseI/ORedirection

int main() {
// freopen(
freopen("input.txt",
input.txt , "r",
r , stdin); // don
don't
t retype test cases!
// freopen("output.txt", "w", stdout);
scanf and printf as per normal; // I prefer scanf/printf than
// cin/cout, C style is much easier

7. Usememset/assign/constructoreffectively!

memset(dist, 127, sizeof(dist));


// useful to initialize shortest path distances, set INF to 127!
memset(dp_memo, -1, sizeof(dp_memo));
// useful to initialize DP memoization table
memset(arr,
(
, 0,
, sizeof(arr));
(
)); // useful to clear array
y of integers
g
vector<int> dist(v, 2000000000);
dist.assign(v, -1);

CS3233 CompetitiveProgramming,
StevenHalim,SoC,NUS

Top Coder Coding Style (5)


TopCoderCodingStyle(5)
8. Declare(large)staticDSasglobalvariable
Allinputsizeisknown,declaredatastructuresizeLARGERthanneededtoavoidsillybugs
Avoiddynamicdatastructuresthatinvolvepointers,etc
Avoid dynamic data structures that involve pointers etc
Useglobalvariabletoreducestacksizeissue

Nowourcodingtasksaremuchsimpler
Typinglesscode=shortercodingtime
Typing less code = shorter coding time
=betterrankinprogrammingcontests

CS3233 CompetitiveProgramming,
StevenHalim,SoC,NUS

Quick Check
QuickCheck
1. Icancopewiththis
p
pace
2. Iamlostwithso
many new
manynew
informationinthe
pastfewslides
f
0
0 of 120

CS3233 CompetitiveProgramming,1
StevenHalim,SoC,NUS

0
2

5 Minutes Break
5MinutesBreak
Onedatastructureswithout builtinlibraries
willbediscussedinthelastpart
p
BinaryIndexed(Fenwick)Tree
Graph,UnionFindDisjointSets,andSegmentTree
Graph Union Find Disjoint Sets and Segment Tree
arenotdiscussedinthisyearsCS3233Week02
GraphDSiscoveredindetailsinCS2010/CS2020
G h DS i
d i d t il i CS2010/CS2020
UFDSiscoveredbrieflyinCS2010/CS2020
PleasestudySegmentTreeonyourown
Pl
t d S
tT
WetrynotsetanycontestprobleminvolvingSegmentTree
CS3233 CompetitiveProgramming,
StevenHalim,SoC,NUS

Time Check:
8.30pm

Graph (notdiscussedtoday,revisitedinWeek05/08)
U i Fi d Di j i t S t (notdiscussedtoday,readCh2onyourown)
UnionFindDisjointSets
( t di
dt d
d Ch2
)
SegmentTree (notdiscussedtoday,readCh2onyourown)
Fenwick Tree (discussedtoday)
FenwickTree
(discussed today)

DATASTRUCTURES
WITHOUTBUILTINLIBRARIES
CS3233 CompetitiveProgramming,
StevenHalim,SoC,NUS

Fenwick Tree (1)


FenwickTree(1)
CumulativeFrequencyTable
Example,s={2,4,5,5,6,6,6,7,7,8}(alreadysorted)
Index/Score/Symbol

Frequency

CumulativeFrequency

10

Fenwick Tree (2)


FenwickTree(2)
FenwickTree(inventor=PeterM.Fenwick)
AlsoknownasBinaryIndexed Tree,veryaptly named
Implementedasanarray,letcallthearraynameasft
Eachindex offt isresponsibleforcertainrange (seediagram)
Key/Index

Binary

Range

CF

FT

0000

N/A

N/A

N/A

N/A

0001

0010

1..2

0011

0100

1..4

0101

0110

5..6

0111

1000

1..8

10

10

1001

10

Do you notice
any particular pattern?

Fenwick Tree (3)


FenwickTree(3)
Togetthecumulativefrequencyfromindex1
h
l
f
f
d
tob,
useft_rsq(ft, b)
The
Theansweristhesumofsubfrequenciesstoredinarrayft
answer is the sum of sub frequencies stored in array ft with
with
indicesrelatedtob viathisformulab' = b - LSOne(b)
RecallthatLSOne(b) = b & (-b)
Thatis,striptheleastsignificantbit ofb

Applythisformulaiterativelyuntilb is0
Analysis:
A
l i
This is
O(log n)
Why?

Example:ft_rsq(ft,
Example: ft rsq(ft, 6)
b=6=0110,b=b LSOne(b)=0110 0010,b'=4=0100
b'=4=0100,b=b LSOne(b)=0100 0100,b''=0,stop

Sum
Sumft[6]+ft[4]=5+2=7
ft[6] + ft[4] = 5 + 2 = 7
(seethebluearea
thatcoversrange
[1 4] + [5 6] = [1 6])
[1..4]+[5..6]=[1..6])

Fenwick Tree (4)


FenwickTree(4)
Togetthecumulativefrequencyfromindexa
h
l
f
f
d
tob,
useft_rsq(ft, a, b)
If
Ifa
a isnotone,wecanuse:
is not one we can use:
ft_rsq(ft, b) ft_rsq(ft, a - 1)
togettheanswer
Analysis:
This is
O(2 log n) =
O(l n))
O(log
Why?

Example:ft_rsq(ft, 3, 6) =
ft_rsq(ft, 6) ft_rsq(ft, 3 1) =
ft_rsq(ft, 6) ft_rsq(ft, 2) =

blue area minusgreenarea


minus green area =
bluearea
(5+2) (0+1) =
7 1 =6

Fenwick Tree (5)


FenwickTree(5)
Toupdatethefrequencyofankey/indexk,byv
d
h f
f k / d
b (either
( h
positiveornegative),useft_adjust(ft, k, v)
Indices
Indicesthatarerelatedtok
that are related to k viak'
via k' = k + LSOne(k)
willbeupdatedbyv whenk < ft.size()
Example:ft_adjust(ft, 5, 2)
Analysis:
A
l i
This is also
O(log n)
Why?

k=5=0101,k'=k+LSOne(k)=0101 +0001,k'=6=0110
k'=6=0110,k''=k'+LSOne(k')=0110+0010,k''=8=1000
Andsoonwhilek<ft.size()

Observethatthedottedredline inthefigurebelowstabsthrough
therangesthatareundertheresponsibilityofindices5,6,and8
ft[5]
ft[5], 2updatedto4
2 updated to 4
ft[6], 5updatedto7
ft[8], 10updatedto12

Fenwick Tree (6) Library


FenwickTree(6)
typedef
t
d f vector<int>
t <i t> vi;
i
#define LSOne(S) (S & (-S))
ft_create(vi
create(vi &ft, int n) { ft.assign(n + 1, 0); }
void ft
int ft_rsq(const vi &ft, int b) {
int sum = 0; for (; b; b -= LSOne(b)) sum += ft[b];
return sum; }

// init: n+1
n 1 zeroes
// returns RSQ(1, b)

int ft_rsq(const vi &t, int a, int b) {


// returns RSQ(a, b)
return
t
ft
ft_rsq(t,
(t b) - (a
( == 1 ? 0 : ft_rsq(t,
ft
(t a - 1))
1)); }
// adjusts value of the k-th element by v (v can be +ve/inc or -ve/dec)
j
(
&ft,
, int k,
, int v)
) {
void ft_adjust(vi
for (; k < (int)ft.size(); k += LSOne(k)) ft[k] += v; }

CS3233 CompetitiveProgramming,
StevenHalim,SoC,NUS

FT/BIT is in IOI syllabus!

Fenwick Tree (7) Application


FenwickTree(7)
FenwickTreeisverysuitablefordynamic RSQs
(cumulativefrequencytable)whereeachupdate
occursonacertainindexonly
Now,thinkofpotentialreallifeapplications!
http://uhunt.felixhalim.net/id/32900
Considercoderunningtimeof[0.000 9.999]
foraparticularUVa problem
Thereareupto9+millionsubmissions/codes
Aboutthousandssubmissionsperproblem
About thousands submissions per problem

Ifyourcoderunsin0.342secs,whatisyourrank?

HowtouseFenwickTreetodealwiththisproblem?
p
CS3233 CompetitiveProgramming,
StevenHalim,SoC,NUS

Quick Check
QuickCheck
1. IamlostwithFenwickTree
2. Iunderstandthebasicsof
F
FenwickTree,butsincethis
i kT
b t i
thi
isnewforme,Imay/may
not be able to recognize
notbeabletorecognize
problemssolvablewithFT
3. IhavesolvedseveralFT
relatedproblemsbefore

0
0 of 120

CS3233 CompetitiveProgramming,
1
StevenHalim,SoC,NUS

0
2

0
3

Summary
TherearealotofgreatDataStructuresoutthere
Weneedthemostefficientoneforourproblem
DifferentDSsuitsdifferentproblem!

Manyofthemhavebuiltinlibraries
Forsomeothers,wehavetobuildourown(focusonFT)
Studytheselibraries!Donotrebuildthemduringcontests!

FromWeek03onwardsandfutureICPCs/IOIs,
useC++STLand/orJavaAPIandourbuiltinlibraries!
Now,yourteamshouldbeinrank3045(from60)
(stillsolving~12problemsoutof10,butfaster)
CS3233 CompetitiveProgramming,
StevenHalim,SoC,NUS

You might also like