0% found this document useful (0 votes)
36 views114 pages

01 Intro PDF

The document discusses an introductory lecture on competitive programming topics including basics, standard template library (STL), and bitwise tricks. The aim is to introduce students to algorithms, mathematical insights, and competitive programming which are useful for programming contests and jobs/internships. C++ will be emphasized as it supports STL and templates while being faster than Java, though other languages like C and Java will also be covered. Finally, some prominent programming competitions are listed like ACM-ICPC, Google Code Jam, and contests specific to Indian universities.

Uploaded by

Arka Prava Paul
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)
36 views114 pages

01 Intro PDF

The document discusses an introductory lecture on competitive programming topics including basics, standard template library (STL), and bitwise tricks. The aim is to introduce students to algorithms, mathematical insights, and competitive programming which are useful for programming contests and jobs/internships. C++ will be emphasized as it supports STL and templates while being faster than Java, though other languages like C and Java will also be covered. Finally, some prominent programming competitions are listed like ACM-ICPC, Google Code Jam, and contests specific to Indian universities.

Uploaded by

Arka Prava Paul
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/ 114

Aim and Clarications

Common Problems
Parsing a problem
Standard Template Library
Using Bitwise
Problems

Introductory Lecture
Topics: basics, resources, stl, bitwise tricks

League of Programmers

ACA, IIT Kanpur

October 21, 2012

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

1 Aim and Clarications

2 Common Problems

3 Parsing a problem

4 Standard Template Library

5 Using Bitwise

6 Problems

League of Programmers Introductory Lecture Topics: basics, resources, stl, bitwise


Aim and Clarications
Common Problems
Parsing a problem
Standard Template Library
Using Bitwise
Problems

Aim

Discussion camp not a lecture series. You need to show


motivation.

League of Programmers Introductory Lecture Topics: basics, resources, stl, bitwise


Aim and Clarications
Common Problems
Parsing a problem
Standard Template Library
Using Bitwise
Problems

Aim

Discussion camp not a lecture series. You need to show


motivation.
To help you introduce to the world of algorithms and
competitive programming

League of Programmers Introductory Lecture Topics: basics, resources, stl, bitwise


Aim and Clarications
Common Problems
Parsing a problem
Standard Template Library
Using Bitwise
Problems

Aim

Discussion camp not a lecture series. You need to show


motivation.
To help you introduce to the world of algorithms and
competitive programming
Many useful algorithms, mathematical insights

League of Programmers Introductory Lecture Topics: basics, resources, stl, bitwise


Aim and Clarications
Common Problems
Parsing a problem
Standard Template Library
Using Bitwise
Problems

Aim

Discussion camp not a lecture series. You need to show


motivation.
To help you introduce to the world of algorithms and
competitive programming
Many useful algorithms, mathematical insights
Useful for any programming contest that you may encounter

League of Programmers Introductory Lecture Topics: basics, resources, stl, bitwise


Aim and Clarications
Common Problems
Parsing a problem
Standard Template Library
Using Bitwise
Problems

Aim

Discussion camp not a lecture series. You need to show


motivation.
To help you introduce to the world of algorithms and
competitive programming
Many useful algorithms, mathematical insights
Useful for any programming contest that you may encounter
After this you can rock in job/internship interviews

League of Programmers Introductory Lecture Topics: basics, resources, stl, bitwise


Aim and Clarications
Common Problems
Parsing a problem
Standard Template Library
Using Bitwise
Problems

Aim

Discussion camp not a lecture series. You need to show


motivation.
To help you introduce to the world of algorithms and
competitive programming
Many useful algorithms, mathematical insights
Useful for any programming contest that you may encounter
After this you can rock in job/internship interviews
It's fun too!

League of Programmers Introductory Lecture Topics: basics, resources, stl, bitwise


Aim and Clarications
Common Problems
Parsing a problem
Standard Template Library
Using Bitwise
Problems

Aim

Discussion camp not a lecture series. You need to show


motivation.
To help you introduce to the world of algorithms and
competitive programming
Many useful algorithms, mathematical insights
Useful for any programming contest that you may encounter
After this you can rock in job/internship interviews
It's fun too!
There are handsome rewards - prestige, joy of learning new
things, and yes lots of money!

League of Programmers Introductory Lecture Topics: basics, resources, stl, bitwise


Aim and Clarications
Common Problems
Parsing a problem
Standard Template Library
Using Bitwise
Problems

Language specications

Language we will stress upon: C, C++, Java


ACM-ICPC Ocial languages. Allowed in almost every
contests

League of Programmers Introductory Lecture Topics: basics, resources, stl, bitwise


Aim and Clarications
Common Problems
Parsing a problem
Standard Template Library
Using Bitwise
Problems

Language specications

Language we will stress upon: C, C++, Java


ACM-ICPC Ocial languages. Allowed in almost every
contests
But, Java is comparatively very slow, so sometimes an optimal
algorithm might time out on the judge

League of Programmers Introductory Lecture Topics: basics, resources, stl, bitwise


Aim and Clarications
Common Problems
Parsing a problem
Standard Template Library
Using Bitwise
Problems

Language specications

Language we will stress upon: C, C++, Java


ACM-ICPC Ocial languages. Allowed in almost every
contests
But, Java is comparatively very slow, so sometimes an optimal
algorithm might time out on the judge
C has too restrictive and does not support stl/templates
/classes

League of Programmers Introductory Lecture Topics: basics, resources, stl, bitwise


Aim and Clarications
Common Problems
Parsing a problem
Standard Template Library
Using Bitwise
Problems

Language specications

Language we will stress upon: C, C++, Java


ACM-ICPC Ocial languages. Allowed in almost every
contests
But, Java is comparatively very slow, so sometimes an optimal
algorithm might time out on the judge
C has too restrictive and does not support stl/templates
/classes
Use Library functions and Data Structures instead of writing
your own every time

League of Programmers Introductory Lecture Topics: basics, resources, stl, bitwise


Aim and Clarications
Common Problems
Parsing a problem
Standard Template Library
Using Bitwise
Problems

Programming competitions

ACM-ICPC

League of Programmers Introductory Lecture Topics: basics, resources, stl, bitwise


Aim and Clarications
Common Problems
Parsing a problem
Standard Template Library
Using Bitwise
Problems

Programming competitions

ACM-ICPC
Google Code Jam - Google's annual programming contest

League of Programmers Introductory Lecture Topics: basics, resources, stl, bitwise


Aim and Clarications
Common Problems
Parsing a problem
Standard Template Library
Using Bitwise
Problems

Programming competitions

ACM-ICPC
Google Code Jam - Google's annual programming contest
Facebook Hacker Cup - an easy gateway to facebook job

League of Programmers Introductory Lecture Topics: basics, resources, stl, bitwise


Aim and Clarications
Common Problems
Parsing a problem
Standard Template Library
Using Bitwise
Problems

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)

League of Programmers Introductory Lecture Topics: basics, resources, stl, bitwise


Aim and Clarications
Common Problems
Parsing a problem
Standard Template Library
Using Bitwise
Problems

Websites for practice

Compete against Indian coders in live contests: Codechef


Short Programming Contests: Codeforces, Topcoder
Problem set Archives: SPOJ, Project Euler, livearchive,
acm.sgu.ru and many more

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

1 Aim and Clarications

2 Common Problems

3 Parsing a problem

4 Standard Template Library

5 Using Bitwise

6 Problems

League of Programmers Introductory Lecture Topics: basics, resources, stl, bitwise


Aim and Clarications
Common Problems
Parsing a problem
Standard Template Library
Using Bitwise
Problems

Overow

#include<stdio.h>
int main()
{
int a, b;
scanf("%d %d", &a, &b);
printf("%d\n", a+b);
return 0;
}

League of Programmers Introductory Lecture Topics: basics, resources, stl, bitwise


Aim and Clarications
Common Problems
Parsing a problem
Standard Template Library
Using Bitwise
Problems

Overow

#include<stdio.h>
int main()
{
int a, b;
scanf("%d %d", &a, &b);
printf("%d\n", a+b);
return 0;
}

What if the given numbers are HUGE?

League of Programmers Introductory Lecture Topics: basics, resources, stl, bitwise


Aim and Clarications
Common Problems
Parsing a problem
Standard Template Library
Using Bitwise
Problems

Overow

#include<stdio.h>
int main()
{
int a, b;
scanf("%d %d", &a, &b);
printf("%d\n", a+b);
return 0;
}

What if the given numbers are HUGE?


Not all the input constraints are explicit

League of Programmers Introductory Lecture Topics: basics, resources, stl, bitwise


Aim and Clarications
Common Problems
Parsing a problem
Standard Template Library
Using Bitwise
Problems

Overow

#include<stdio.h>
int main()
{
int a, b;
scanf("%d %d", &a, &b);
printf("%d\n", a+b);
return 0;
}

What if the given numbers are HUGE?


Not all the input constraints are explicit
Always think about the worst case scenario, edge cases, etc.
League of Programmers Introductory Lecture Topics: basics, resources, stl, bitwise
Aim and Clarications
Common Problems
Parsing a problem
Standard Template Library
Using Bitwise
Problems

Others

Comparing doubles
Always keep a cushion of 

League of Programmers Introductory Lecture Topics: basics, resources, stl, bitwise


Aim and Clarications
Common Problems
Parsing a problem
Standard Template Library
Using Bitwise
Problems

Others

Comparing doubles
Always keep a cushion of 
double a, b;
a==b, not a very good idea

League of Programmers Introductory Lecture Topics: basics, resources, stl, bitwise


Aim and Clarications
Common Problems
Parsing a problem
Standard Template Library
Using Bitwise
Problems

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)

League of Programmers Introductory Lecture Topics: basics, resources, stl, bitwise


Aim and Clarications
Common Problems
Parsing a problem
Standard Template Library
Using Bitwise
Problems

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

League of Programmers Introductory Lecture Topics: basics, resources, stl, bitwise


Aim and Clarications
Common Problems
Parsing a problem
Standard Template Library
Using Bitwise
Problems

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

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

1 Aim and Clarications

2 Common Problems

3 Parsing a problem

4 Standard Template Library

5 Using Bitwise

6 Problems

League of Programmers Introductory Lecture Topics: basics, resources, stl, bitwise


Aim and Clarications
Common Problems
Parsing a problem
Standard Template Library
Using Bitwise
Problems

Problem Solving Methodology

Understand what the program is expected to do.

League of Programmers Introductory Lecture Topics: basics, resources, stl, bitwise


Aim and Clarications
Common Problems
Parsing a problem
Standard Template Library
Using Bitwise
Problems

Problem Solving Methodology

Understand what the program is expected to do.


Understand the Input/Output format and use exactly that
format

League of Programmers Introductory Lecture Topics: basics, resources, stl, bitwise


Aim and Clarications
Common Problems
Parsing a problem
Standard Template Library
Using Bitwise
Problems

Problem Solving Methodology

Understand what the program is expected to do.


Understand the Input/Output format and use exactly that
format
Meaning of constraints

League of Programmers Introductory Lecture Topics: basics, resources, stl, bitwise


Aim and Clarications
Common Problems
Parsing a problem
Standard Template Library
Using Bitwise
Problems

Problem Solving Methodology

Understand what the program is expected to do.


Understand the Input/Output format and use exactly that
format
Meaning of constraints
What do time limit and memory limit mean??

League of Programmers Introductory Lecture Topics: basics, resources, stl, bitwise


Aim and Clarications
Common Problems
Parsing a problem
Standard Template Library
Using Bitwise
Problems

Problem Solving Methodology

Understand what the program is expected to do.


Understand the Input/Output format and use exactly that
format
Meaning of constraints
What do time limit and memory limit mean??
Predict the order requirements of the given problem

League of Programmers Introductory Lecture Topics: basics, resources, stl, bitwise


Aim and Clarications
Common Problems
Parsing a problem
Standard Template Library
Using Bitwise
Problems

Problem Solving Methodology

Understand what the program is expected to do.


Understand the Input/Output format and use exactly that
format
Meaning of constraints
What do time limit and memory limit mean??
Predict the order requirements of the given problem
1sec (1 2) 108 operations

League of Programmers Introductory Lecture Topics: basics, resources, stl, bitwise


Aim and Clarications
Common Problems
Parsing a problem
Standard Template Library
Using Bitwise
Problems

Problem Solving Methodology

Understand what the program is expected to do.


Understand the Input/Output format and use exactly that
format
Meaning of constraints
What do time limit and memory limit mean??
Predict the order requirements of the given problem
1sec (1 2) 108 operations
xMB x /4 10 6
sized int arrays

League of Programmers Introductory Lecture Topics: basics, resources, stl, bitwise


Aim and Clarications
Common Problems
Parsing a problem
Standard Template Library
Using Bitwise
Problems

Problem Solving Methodology

Understand what the program is expected to do.


Understand the Input/Output format and use exactly that
format
Meaning of constraints
What do time limit and memory limit mean??
Predict the order requirements of the given problem
1sec (1 2) 108 operations
xMB x /4 10 6
sized int arrays
For example, 10 test cases with N = 10000 means O (N 2
) is
required

League of Programmers Introductory Lecture Topics: basics, resources, stl, bitwise


Aim and Clarications
Common Problems
Parsing a problem
Standard Template Library
Using Bitwise
Problems

Problem Solving Methodology

1 Understanding the problem, mathematical formulation of the


problem

League of Programmers Introductory Lecture Topics: basics, resources, stl, bitwise


Aim and Clarications
Common Problems
Parsing a problem
Standard Template Library
Using Bitwise
Problems

Problem Solving Methodology

1 Understanding the problem, mathematical formulation of the


problem
2 Categorizing the problem into one of the many types known

League of Programmers Introductory Lecture Topics: basics, resources, stl, bitwise


Aim and Clarications
Common Problems
Parsing a problem
Standard Template Library
Using Bitwise
Problems

Problem Solving Methodology

1 Understanding the problem, mathematical formulation of the


problem
2 Categorizing the problem into one of the many types known
3 Formulate a solution

League of Programmers Introductory Lecture Topics: basics, resources, stl, bitwise


Aim and Clarications
Common Problems
Parsing a problem
Standard Template Library
Using Bitwise
Problems

Problem Solving Methodology

1 Understanding the problem, mathematical formulation of the


problem
2 Categorizing the problem into one of the many types known
3 Formulate a solution
4 Verify with the sample test cases, make sure your solution
atleast works for them

League of Programmers Introductory Lecture Topics: basics, resources, stl, bitwise


Aim and Clarications
Common Problems
Parsing a problem
Standard Template Library
Using Bitwise
Problems

Problem Solving Methodology

1 Understanding the problem, mathematical formulation of the


problem
2 Categorizing the problem into one of the many types known
3 Formulate a solution
4 Verify with the sample test cases, make sure your solution
atleast works for them
5 Generate some small test cases of your own, the sample test
cases may not be include some boundary cases.

League of Programmers Introductory Lecture Topics: basics, resources, stl, bitwise


Aim and Clarications
Common Problems
Parsing a problem
Standard Template Library
Using Bitwise
Problems

Problem Solving Methodology

1 Understanding the problem, mathematical formulation of the


problem
2 Categorizing the problem into one of the many types known
3 Formulate a solution
4 Verify with the sample test cases, make sure your solution
atleast works for them
5 Generate some small test cases of your own, the sample test
cases may not be include some boundary cases.
6 Coding the solution (the easiest part of all)

League of Programmers Introductory Lecture Topics: basics, resources, stl, bitwise


Aim and Clarications
Common Problems
Parsing a problem
Standard Template Library
Using Bitwise
Problems

Problem Solving Methodology

1 Understanding the problem, mathematical formulation of the


problem
2 Categorizing the problem into one of the many types known
3 Formulate a solution
4 Verify with the sample test cases, make sure your solution
atleast works for them
5 Generate some small test cases of your own, the sample test
cases may not be include some boundary cases.
6 Coding the solution (the easiest part of all)
7 Debugging (TLE: time limit exceeded, WA : incorrect solution
etc.)

League of Programmers Introductory Lecture Topics: basics, resources, stl, bitwise


Aim and Clarications
Common Problems
Parsing a problem
Standard Template Library
Using Bitwise
Problems

Some optimizations

Not all operations are equally fast:


operations on unsigned ints/long long are faster
bitwise operators and shift operators (& |)
Using too much memory (> 10MB ) slows down programmes

League of Programmers Introductory Lecture Topics: basics, resources, stl, bitwise


Aim and Clarications
Common Problems
Parsing a problem
Standard Template Library
Using Bitwise
Problems

Some optimizations

Not all operations are equally fast:


operations on unsigned ints/long long are faster
bitwise operators and shift operators (& |)
Using too much memory (> 10MB ) slows down programmes
Look at other people's code for reference and optimisations

League of Programmers Introductory Lecture Topics: basics, resources, stl, bitwise


Aim and Clarications
Common Problems
Parsing a problem
Standard Template Library
Using Bitwise
Problems

Some optimizations

Not all operations are equally fast:


operations on unsigned ints/long long are faster
bitwise operators and shift operators (& |)
Using too much memory (> 10MB ) slows down programmes
Look at other people's code for reference and optimisations
Read this: http://www.codeproject.com/KB/cpp/C___
Code_Optimization.aspx

League of Programmers Introductory Lecture Topics: basics, resources, stl, bitwise


Aim and Clarications
Common Problems
Parsing a problem
Standard Template Library
Using Bitwise
Problems

Some optimizations

Not all operations are equally fast:


operations on unsigned ints/long long are faster
bitwise operators and shift operators (& |)
Using too much memory (> 10MB ) slows down programmes
Look at other people's code for reference and optimisations
Read this: http://www.codeproject.com/KB/cpp/C___
Code_Optimization.aspx
I/O: do NOT use cin/cout for large input output

League of Programmers Introductory Lecture Topics: basics, resources, stl, bitwise


Aim and Clarications
Common Problems
Parsing a problem
Standard Template Library
Using Bitwise
Problems

Some Standard paradigms


Sorting
Searching
Preprocessing
Divide-and-Conquer
Dynamic Programming
Greedy Algorithms
Graph
Network Flow
Backtracking
Computational Geometry
Pure maths
Ad-hoc problems
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

1 Aim and Clarications

2 Common Problems

3 Parsing a problem

4 Standard Template Library

5 Using Bitwise

6 Problems

League of Programmers Introductory Lecture Topics: basics, resources, stl, bitwise


Aim and Clarications
Common Problems
Parsing a problem
Standard Template Library
Using Bitwise
Problems

Standard template library


Website: http://www.cplusplus.com/reference
1 Data Structures

League of Programmers Introductory Lecture Topics: basics, resources, stl, bitwise


Aim and Clarications
Common Problems
Parsing a problem
Standard Template Library
Using Bitwise
Problems

Standard template library


Website: http://www.cplusplus.com/reference
1 Data Structures

vector

League of Programmers Introductory Lecture Topics: basics, resources, stl, bitwise


Aim and Clarications
Common Problems
Parsing a problem
Standard Template Library
Using Bitwise
Problems

Standard template library


Website: http://www.cplusplus.com/reference
1 Data Structures

vector
stack

League of Programmers Introductory Lecture Topics: basics, resources, stl, bitwise


Aim and Clarications
Common Problems
Parsing a problem
Standard Template Library
Using Bitwise
Problems

Standard template library


Website: http://www.cplusplus.com/reference
1 Data Structures

vector
stack
queue

League of Programmers Introductory Lecture Topics: basics, resources, stl, bitwise


Aim and Clarications
Common Problems
Parsing a problem
Standard Template Library
Using Bitwise
Problems

Standard template library


Website: http://www.cplusplus.com/reference
1 Data Structures

vector
stack
queue
priority_queue

League of Programmers Introductory Lecture Topics: basics, resources, stl, bitwise


Aim and Clarications
Common Problems
Parsing a problem
Standard Template Library
Using Bitwise
Problems

Standard template library


Website: http://www.cplusplus.com/reference
1 Data Structures

vector
stack
queue
priority_queue
set

League of Programmers Introductory Lecture Topics: basics, resources, stl, bitwise


Aim and Clarications
Common Problems
Parsing a problem
Standard Template Library
Using Bitwise
Problems

Standard template library


Website: http://www.cplusplus.com/reference
1 Data Structures

vector
stack
queue
priority_queue
set
map

League of Programmers Introductory Lecture Topics: basics, resources, stl, bitwise


Aim and Clarications
Common Problems
Parsing a problem
Standard Template Library
Using Bitwise
Problems

Standard template library


Website: http://www.cplusplus.com/reference
1 Data Structures

vector
stack
queue
priority_queue
set
map
2 Algorithms

League of Programmers Introductory Lecture Topics: basics, resources, stl, bitwise


Aim and Clarications
Common Problems
Parsing a problem
Standard Template Library
Using Bitwise
Problems

Standard template library


Website: http://www.cplusplus.com/reference
1 Data Structures

vector
stack
queue
priority_queue
set
map
2 Algorithms

nd

League of Programmers Introductory Lecture Topics: basics, resources, stl, bitwise


Aim and Clarications
Common Problems
Parsing a problem
Standard Template Library
Using Bitwise
Problems

Standard template library


Website: http://www.cplusplus.com/reference
1 Data Structures

vector
stack
queue
priority_queue
set
map
2 Algorithms

nd
max, min

League of Programmers Introductory Lecture Topics: basics, resources, stl, bitwise


Aim and Clarications
Common Problems
Parsing a problem
Standard Template Library
Using Bitwise
Problems

Standard template library


Website: http://www.cplusplus.com/reference
1 Data Structures

vector
stack
queue
priority_queue
set
map
2 Algorithms

nd
max, min
sort

League of Programmers Introductory Lecture Topics: basics, resources, stl, bitwise


Aim and Clarications
Common Problems
Parsing a problem
Standard Template Library
Using Bitwise
Problems

Standard template library


Website: http://www.cplusplus.com/reference
1 Data Structures

vector
stack
queue
priority_queue
set
map
2 Algorithms

nd
max, min
sort
reverse

League of Programmers Introductory Lecture Topics: basics, resources, stl, bitwise


Aim and Clarications
Common Problems
Parsing a problem
Standard Template Library
Using Bitwise
Problems

Standard template library


Website: http://www.cplusplus.com/reference
1 Data Structures

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

1 Last in, rst out (LIFO)

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

1 Last in, rst out (LIFO)


2 Supports three constant-time operations

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

1 Last in, rst out (LIFO)


2 Supports three constant-time operations
Push(x): inserts x into the stack

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

1 Last in, rst out (LIFO)


2 Supports three constant-time operations
Push(x): inserts x into the stack
Pop(): removes the newest item

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

1 Last in, rst out (LIFO)


2 Supports three constant-time operations
Push(x): inserts x into the stack
Pop(): removes the newest item
Top(): returns the newest item

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

1 Last in, rst out (LIFO)


2 Supports three constant-time operations
Push(x): inserts x into the stack
Pop(): removes the newest item
Top(): returns the newest item
3 C++ and Java have implementations of stack

League of Programmers Introductory Lecture Topics: basics, resources, stl, bitwise


Aim and Clarications
Common Problems
Parsing a problem
Standard Template Library
Using Bitwise
Problems

Queue

1 First in, rst out (FIFO)

League of Programmers Introductory Lecture Topics: basics, resources, stl, bitwise


Aim and Clarications
Common Problems
Parsing a problem
Standard Template Library
Using Bitwise
Problems

Queue

1 First in, rst out (FIFO)


2 Supports three constant-time operations

League of Programmers Introductory Lecture Topics: basics, resources, stl, bitwise


Aim and Clarications
Common Problems
Parsing a problem
Standard Template Library
Using Bitwise
Problems

Queue

1 First in, rst out (FIFO)


2 Supports three constant-time operations
Enqueue(x): inserts x into the queue

League of Programmers Introductory Lecture Topics: basics, resources, stl, bitwise


Aim and Clarications
Common Problems
Parsing a problem
Standard Template Library
Using Bitwise
Problems

Queue

1 First in, rst out (FIFO)


2 Supports three constant-time operations
Enqueue(x): inserts x into the queue
Dequeue(): removes the oldest item

League of Programmers Introductory Lecture Topics: basics, resources, stl, bitwise


Aim and Clarications
Common Problems
Parsing a problem
Standard Template Library
Using Bitwise
Problems

Queue

1 First in, rst out (FIFO)


2 Supports three constant-time operations
Enqueue(x): inserts x into the queue
Dequeue(): removes the oldest item
Front(): returns the oldest item

League of Programmers Introductory Lecture Topics: basics, resources, stl, bitwise


Aim and Clarications
Common Problems
Parsing a problem
Standard Template Library
Using Bitwise
Problems

Queue

1 First in, rst out (FIFO)


2 Supports three constant-time operations
Enqueue(x): inserts x into the queue
Dequeue(): removes the oldest item
Front(): returns the oldest item
3 C++ and Java have implementations of queue

League of Programmers Introductory Lecture Topics: basics, resources, stl, bitwise


Aim and Clarications
Common Problems
Parsing a problem
Standard Template Library
Using Bitwise
Problems

Priority Queue

1 Each element in a PQ has a priority value

League of Programmers Introductory Lecture Topics: basics, resources, stl, bitwise


Aim and Clarications
Common Problems
Parsing a problem
Standard Template Library
Using Bitwise
Problems

Priority Queue

1 Each element in a PQ has a priority value


2 Three operations:

League of Programmers Introductory Lecture Topics: basics, resources, stl, bitwise


Aim and Clarications
Common Problems
Parsing a problem
Standard Template Library
Using Bitwise
Problems

Priority Queue

1 Each element in a PQ has a priority value


2 Three operations:
Insert(x, p): inserts x into the PQ, whose priority is p

League of Programmers Introductory Lecture Topics: basics, resources, stl, bitwise


Aim and Clarications
Common Problems
Parsing a problem
Standard Template Library
Using Bitwise
Problems

Priority Queue

1 Each element in a PQ has a priority value


2 Three operations:
Insert(x, p): inserts x into the PQ, whose priority is p
RemoveTop(): removes the element with the highest priority

League of Programmers Introductory Lecture Topics: basics, resources, stl, bitwise


Aim and Clarications
Common Problems
Parsing a problem
Standard Template Library
Using Bitwise
Problems

Priority Queue

1 Each element in a PQ has a priority value


2 Three operations:
Insert(x, p): inserts x into the PQ, whose priority is p
RemoveTop(): removes the element with the highest priority
Top(): returns the element with the highest priority

League of Programmers Introductory Lecture Topics: basics, resources, stl, bitwise


Aim and Clarications
Common Problems
Parsing a problem
Standard Template Library
Using Bitwise
Problems

Priority Queue

1 Each element in a PQ has a priority value


2 Three operations:
Insert(x, p): inserts x into the PQ, whose priority is p
RemoveTop(): removes the element with the highest priority
Top(): returns the element with the highest priority
3 All operations can be done quickly if implemented using a heap

League of Programmers Introductory Lecture Topics: basics, resources, stl, bitwise


Aim and Clarications
Common Problems
Parsing a problem
Standard Template Library
Using Bitwise
Problems

Priority Queue

1 Each element in a PQ has a priority value


2 Three operations:
Insert(x, p): inserts x into the PQ, whose priority is p
RemoveTop(): removes the element with the highest priority
Top(): returns the element with the highest priority
3 All operations can be done quickly if implemented using a heap
4 C++ and Java have implementations of priority queue

League of Programmers Introductory Lecture Topics: basics, resources, stl, bitwise


Aim and Clarications
Common Problems
Parsing a problem
Standard Template Library
Using Bitwise
Problems

Heap

1 Complete binary tree with the heap property:


value of a node values of its children

League of Programmers Introductory Lecture Topics: basics, resources, stl, bitwise


Aim and Clarications
Common Problems
Parsing a problem
Standard Template Library
Using Bitwise
Problems

Heap

1 Complete binary tree with the heap property:


value of a node values of its children
2 The root node has the maximum value

League of Programmers Introductory Lecture Topics: basics, resources, stl, bitwise


Aim and Clarications
Common Problems
Parsing a problem
Standard Template Library
Using Bitwise
Problems

Heap

1 Complete binary tree with the heap property:


value of a node values of its children
2 The root node has the maximum value
3 Constant-time: top()

League of Programmers Introductory Lecture Topics: basics, resources, stl, bitwise


Aim and Clarications
Common Problems
Parsing a problem
Standard Template Library
Using Bitwise
Problems

Heap

1 Complete binary tree with the heap property:


value of a node values of its children
2 The root node has the maximum value
3 Constant-time: top()
4 Inserting/removing a node can be done in O(log n) time
without breaking the heap property

League of Programmers Introductory Lecture Topics: basics, resources, stl, bitwise


Aim and Clarications
Common Problems
Parsing a problem
Standard Template Library
Using Bitwise
Problems

Heap

1 Complete binary tree with the heap property:


value of a node values of its children
2 The root node has the maximum value
3 Constant-time: top()
4 Inserting/removing a node can be done in O(log n) time
without breaking the heap property
5 May need rearrangement of some nodes

League of Programmers Introductory Lecture Topics: basics, resources, stl, bitwise


Aim and Clarications
Common Problems
Parsing a problem
Standard Template Library
Using Bitwise
Problems

Heap

League of Programmers Introductory Lecture Topics: basics, resources, stl, bitwise


Aim and Clarications
Common Problems
Parsing a problem
Standard Template Library
Using Bitwise
Problems

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

League of Programmers Introductory Lecture Topics: basics, resources, stl, bitwise


Aim and Clarications
Common Problems
Parsing a problem
Standard Template Library
Using Bitwise
Problems

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

League of Programmers Introductory Lecture Topics: basics, resources, stl, bitwise


Aim and Clarications
Common Problems
Parsing a problem
Standard Template Library
Using Bitwise
Problems

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

League of Programmers Introductory Lecture Topics: basics, resources, stl, bitwise


Aim and Clarications
Common Problems
Parsing a problem
Standard Template Library
Using Bitwise
Problems

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)

League of Programmers Introductory Lecture Topics: basics, resources, stl, bitwise


Aim and Clarications
Common Problems
Parsing a problem
Standard Template Library
Using Bitwise
Problems

Heap

Deleting the Root Node


1 Remove the root, and bring the last node (rightmost node in
the last level) to the root

League of Programmers Introductory Lecture Topics: basics, resources, stl, bitwise


Aim and Clarications
Common Problems
Parsing a problem
Standard Template Library
Using Bitwise
Problems

Heap

Deleting the Root Node


1 Remove the root, and bring the last node (rightmost node in
the last level) to the root
2 If the root breaks the heap property, look at its children and
swap it with the larger one. Swapping can introduce another
conict

League of Programmers Introductory Lecture Topics: basics, resources, stl, bitwise


Aim and Clarications
Common Problems
Parsing a problem
Standard Template Library
Using Bitwise
Problems

Heap

Deleting the Root Node


1 Remove the root, and bring the last node (rightmost node in
the last level) to the root
2 If the root breaks the heap property, look at its children and
swap it with the larger one. Swapping can introduce another
conict
3 Repeat 2 until all conicts are resolved

League of Programmers Introductory Lecture Topics: basics, resources, stl, bitwise


Aim and Clarications
Common Problems
Parsing a problem
Standard Template Library
Using Bitwise
Problems

Heap

Deleting the Root Node


1 Remove the root, and bring the last node (rightmost node in
the last level) to the root
2 If the root breaks the heap property, look at its children and
swap it with the larger one. Swapping can introduce another
conict
3 Repeat 2 until all conicts are resolved
4 Running time = O(log n)

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

1 Aim and Clarications

2 Common Problems

3 Parsing a problem

4 Standard Template Library

5 Using Bitwise

6 Problems

League of Programmers Introductory Lecture Topics: basics, resources, stl, bitwise


Aim and Clarications
Common Problems
Parsing a problem
Standard Template Library
Using Bitwise
Problems

Introduction to Bitwise Operators

1 Numbers are stored in binary and processing on bits is way


faster.

League of Programmers Introductory Lecture Topics: basics, resources, stl, bitwise


Aim and Clarications
Common Problems
Parsing a problem
Standard Template Library
Using Bitwise
Problems

Introduction to Bitwise Operators

1 Numbers are stored in binary and processing on bits is way


faster.
2 Our weapons:
<< (left shift); >> (right shift); & (bitwise and);
| (bitwise or);  (bitwise xor); (bitwise not)

League of Programmers Introductory Lecture Topics: basics, resources, stl, bitwise


Aim and Clarications
Common Problems
Parsing a problem
Standard Template Library
Using Bitwise
Problems

Introduction to Bitwise Operators

1 Numbers are stored in binary and processing on bits is way


faster.
2 Our weapons:
<< (left shift); >> (right shift); & (bitwise and);
| (bitwise or);  (bitwise xor); (bitwise not)
3 Speed up the code by upto 100 times. Caution: try to use
bitwise operations on unsigned integers only

League of Programmers Introductory Lecture Topics: basics, resources, stl, bitwise


Aim and Clarications
Common Problems
Parsing a problem
Standard Template Library
Using Bitwise
Problems

Beauty of Bitwise
1 Example:

League of Programmers Introductory Lecture Topics: basics, resources, stl, bitwise


Aim and Clarications
Common Problems
Parsing a problem
Standard Template Library
Using Bitwise
Problems

Beauty of Bitwise
1 Example:
Any subset of 0,1. . . 31 is a single int

League of Programmers Introductory Lecture Topics: basics, resources, stl, bitwise


Aim and Clarications
Common Problems
Parsing a problem
Standard Template Library
Using Bitwise
Problems

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

League of Programmers Introductory Lecture Topics: basics, resources, stl, bitwise


Aim and Clarications
Common Problems
Parsing a problem
Standard Template Library
Using Bitwise
Problems

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:

League of Programmers Introductory Lecture Topics: basics, resources, stl, bitwise


Aim and Clarications
Common Problems
Parsing a problem
Standard Template Library
Using Bitwise
Problems

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 .

League of Programmers Introductory Lecture Topics: basics, resources, stl, bitwise


Aim and Clarications
Common Problems
Parsing a problem
Standard Template Library
Using Bitwise
Problems

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

League of Programmers Introductory Lecture Topics: basics, resources, stl, bitwise


Aim and Clarications
Common Problems
Parsing a problem
Standard Template Library
Using Bitwise
Problems

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

League of Programmers Introductory Lecture Topics: basics, resources, stl, bitwise


Aim and Clarications
Common Problems
Parsing a problem
Standard Template Library
Using Bitwise
Problems

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

League of Programmers Introductory Lecture Topics: basics, resources, stl, bitwise


Aim and Clarications
Common Problems
Parsing a problem
Standard Template Library
Using Bitwise
Problems

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

League of Programmers Introductory Lecture Topics: basics, resources, stl, bitwise


Aim and Clarications
Common Problems
Parsing a problem
Standard Template Library
Using Bitwise
Problems

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

League of Programmers Introductory Lecture Topics: basics, resources, stl, bitwise


Aim and Clarications
Common Problems
Parsing a problem
Standard Template Library
Using Bitwise
Problems

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

League of Programmers Introductory Lecture Topics: basics, resources, stl, bitwise


Aim and Clarications
Common Problems
Parsing a problem
Standard Template Library
Using Bitwise
Problems

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

1 Aim and Clarications

2 Common Problems

3 Parsing a problem

4 Standard Template Library

5 Using Bitwise

6 Problems

League of Programmers Introductory Lecture Topics: basics, resources, stl, bitwise


Aim and Clarications
Common Problems
Parsing a problem
Standard Template Library
Using Bitwise
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/

League of Programmers Introductory Lecture Topics: basics, resources, stl, bitwise

You might also like