01 Intro PDF
01 Intro PDF
Common Problems
Parsing a problem
Standard Template Library
Using Bitwise
Problems
Introductory Lecture
Topics: basics, resources, stl, bitwise tricks
League of Programmers
Outline
2 Common Problems
3 Parsing a problem
5 Using Bitwise
6 Problems
Aim
Aim
Aim
Aim
Aim
Aim
Aim
Language specications
Language specications
Language specications
Language specications
Programming competitions
ACM-ICPC
Programming competitions
ACM-ICPC
Google Code Jam - Google's annual programming contest
Programming competitions
ACM-ICPC
Google Code Jam - Google's annual programming contest
Facebook Hacker Cup - an easy gateway to facebook job
Programming competitions
ACM-ICPC
Google Code Jam - Google's annual programming contest
Facebook Hacker Cup - an easy gateway to facebook job
IOPC (IITK), Shaastra (IITM), Bitwise (IITKgp)
Outline
2 Common Problems
3 Parsing a problem
5 Using Bitwise
6 Problems
Overow
#include<stdio.h>
int main()
{
int a, b;
scanf("%d %d", &a, &b);
printf("%d\n", a+b);
return 0;
}
Overow
#include<stdio.h>
int main()
{
int a, b;
scanf("%d %d", &a, &b);
printf("%d\n", a+b);
return 0;
}
Overow
#include<stdio.h>
int main()
{
int a, b;
scanf("%d %d", &a, &b);
printf("%d\n", a+b);
return 0;
}
Overow
#include<stdio.h>
int main()
{
int a, b;
scanf("%d %d", &a, &b);
printf("%d\n", a+b);
return 0;
}
Others
Comparing doubles
Always keep a cushion of
Others
Comparing doubles
Always keep a cushion of
double a, b;
a==b, not a very good idea
Others
Comparing doubles
Always keep a cushion of
double a, b;
a==b, not a very good idea
instead, do the following
#define EPS 0.0000001
(a-EPS<b and a+EPS>b)
Others
Comparing doubles
Always keep a cushion of
double a, b;
a==b, not a very good idea
instead, do the following
#define EPS 0.0000001
(a-EPS<b and a+EPS>b)
Segmentation fault
Others
Comparing doubles
Always keep a cushion of
double a, b;
a==b, not a very good idea
instead, do the following
#define EPS 0.0000001
(a-EPS<b and a+EPS>b)
Segmentation fault
Invalid memory reference
Using too much memory than provided
Outline
2 Common Problems
3 Parsing a problem
5 Using Bitwise
6 Problems
Some optimizations
Some optimizations
Some optimizations
Some optimizations
Outline
2 Common Problems
3 Parsing a problem
5 Using Bitwise
6 Problems
vector
vector
stack
vector
stack
queue
vector
stack
queue
priority_queue
vector
stack
queue
priority_queue
set
vector
stack
queue
priority_queue
set
map
vector
stack
queue
priority_queue
set
map
2 Algorithms
vector
stack
queue
priority_queue
set
map
2 Algorithms
nd
vector
stack
queue
priority_queue
set
map
2 Algorithms
nd
max, min
vector
stack
queue
priority_queue
set
map
2 Algorithms
nd
max, min
sort
vector
stack
queue
priority_queue
set
map
2 Algorithms
nd
max, min
sort
reverse
vector
stack
queue
priority_queue
set
map
2 Algorithms
nd
max, min
sort
reverse
swap
League of Programmers Introductory Lecture Topics: basics, resources, stl, bitwise
Aim and Clarications
Common Problems
Parsing a problem
Standard Template Library
Using Bitwise
Problems
Stack
Stack
Stack
Stack
Stack
Stack
Queue
Queue
Queue
Queue
Queue
Queue
Priority Queue
Priority Queue
Priority Queue
Priority Queue
Priority Queue
Priority Queue
Priority Queue
Heap
Heap
Heap
Heap
Heap
Heap
Heap
Inserting a Node
1 Make a new node in the last level, as far left as possible. If the
last level is full, make a new one
Heap
Inserting a Node
1 Make a new node in the last level, as far left as possible. If the
last level is full, make a new one
2 If the new node breaks the heap property, swap with its parent
node. The new node moves up the tree, which may introduce
another conict
Heap
Inserting a Node
1 Make a new node in the last level, as far left as possible. If the
last level is full, make a new one
2 If the new node breaks the heap property, swap with its parent
node. The new node moves up the tree, which may introduce
another conict
3 Repeat 2 until all conicts are resolved
Heap
Inserting a Node
1 Make a new node in the last level, as far left as possible. If the
last level is full, make a new one
2 If the new node breaks the heap property, swap with its parent
node. The new node moves up the tree, which may introduce
another conict
3 Repeat 2 until all conicts are resolved
4 Running time = tree height = O(log n)
Heap
Heap
Heap
Heap
Outline
2 Common Problems
3 Parsing a problem
5 Using Bitwise
6 Problems
Beauty of Bitwise
1 Example:
Beauty of Bitwise
1 Example:
Any subset of 0,1. . . 31 is a single int
Beauty of Bitwise
1 Example:
Any subset of 0,1. . . 31 is a single int
Do set union/intersection/complement in one operation
increment/decrement all elements by x in one operation
Beauty of Bitwise
1 Example:
Any subset of 0,1. . . 31 is a single int
Do set union/intersection/complement in one operation
increment/decrement all elements by x in one operation
2 Even more:
Beauty of Bitwise
1 Example:
Any subset of 0,1. . . 31 is a single int
Do set union/intersection/complement in one operation
increment/decrement all elements by x in one operation
2 Even more:
Find if x S .
Beauty of Bitwise
1 Example:
Any subset of 0,1. . . 31 is a single int
Do set union/intersection/complement in one operation
increment/decrement all elements by x in one operation
2 Even more:
Find if x S .
Generate all subsets of S in 2|S | time
Beauty of Bitwise
1 Example:
Any subset of 0,1. . . 31 is a single int
Do set union/intersection/complement in one operation
increment/decrement all elements by x in one operation
2 Even more:
Find if x S .
Generate all subsets of S in 2|S | time
Generate all subsets of 1..n changing one bit at a time
Beauty of Bitwise
1 Example:
Any subset of 0,1. . . 31 is a single int
Do set union/intersection/complement in one operation
increment/decrement all elements by x in one operation
2 Even more:
Find if x S .
Generate all subsets of S in 2|S | time
Generate all subsets of 1..n changing one bit at a time
Generate all subsets of S which have exactly t elements
Beauty of Bitwise
1 Example:
Any subset of 0,1. . . 31 is a single int
Do set union/intersection/complement in one operation
increment/decrement all elements by x in one operation
2 Even more:
Find if x S .
Generate all subsets of S in 2|S | time
Generate all subsets of 1..n changing one bit at a time
Generate all subsets of S which have exactly t elements
Count the number of elements of elements in a set S
Beauty of Bitwise
1 Example:
Any subset of 0,1. . . 31 is a single int
Do set union/intersection/complement in one operation
increment/decrement all elements by x in one operation
2 Even more:
Find if x S .
Generate all subsets of S in 2|S | time
Generate all subsets of 1..n changing one bit at a time
Generate all subsets of S which have exactly t elements
Count the number of elements of elements in a set S
Remove smallest element from S
Beauty of Bitwise
1 Example:
Any subset of 0,1. . . 31 is a single int
Do set union/intersection/complement in one operation
increment/decrement all elements by x in one operation
2 Even more:
Find if x S .
Generate all subsets of S in 2|S | time
Generate all subsets of 1..n changing one bit at a time
Generate all subsets of S which have exactly t elements
Count the number of elements of elements in a set S
Remove smallest element from S
Check if |S| = 1
Beauty of Bitwise
1 Example:
Any subset of 0,1. . . 31 is a single int
Do set union/intersection/complement in one operation
increment/decrement all elements by x in one operation
2 Even more:
Find if x S .
Generate all subsets of S in 2|S | time
Generate all subsets of 1..n changing one bit at a time
Generate all subsets of S which have exactly t elements
Count the number of elements of elements in a set S
Remove smallest element from S
Check if |S| = 1
3 Never multiply or divide or take remainder modulo power of 2
League of Programmers Introductory Lecture Topics: basics, resources, stl, bitwise
Aim and Clarications
Common Problems
Parsing a problem
Standard Template Library
Using Bitwise
Problems
Outline
2 Common Problems
3 Parsing a problem
5 Using Bitwise
6 Problems
Problems
Added on the contest on VOC http://ahmed-aly.com/voc/
Contest ID: 2578
Name: ACA, IITK LOP 01
Author: pnkjjindal
Links:
1 http://spoj.pl/problems/WEIRDFN
2 http://www.spoj.pl/problems/HOMO/
3 http://spoj.pl/problems/HISTOGRA
4 http://spoj.pl/problems/SUBSEQ
5 http://www.spoj.pl/problems/NGM2/
6 http://www.spoj.pl/problems/JOCHEF
7 http://www.spoj.pl/problems/SWTHIN/
8 http://www.spoj.pl/problems/LAZYPROG/