0% found this document useful (0 votes)
36 views

Introductory Lecture Topics: Basics, Resources, STL, Bitwise Tricks, GDB

The document is notes from an introductory lecture on programming contests given by the League of Programmers. It discusses choosing C++ as the language for contests due to speed, common problems encountered like overflow and debugging with gdb. It also summarizes popular practice websites, contests, how to parse problems by understanding requirements, inputs/outputs and meaning of time/memory limits.

Uploaded by

Piyush Kapoor
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views

Introductory Lecture Topics: Basics, Resources, STL, Bitwise Tricks, GDB

The document is notes from an introductory lecture on programming contests given by the League of Programmers. It discusses choosing C++ as the language for contests due to speed, common problems encountered like overflow and debugging with gdb. It also summarizes popular practice websites, contests, how to parse problems by understanding requirements, inputs/outputs and meaning of time/memory limits.

Uploaded by

Piyush Kapoor
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 51

clarications Introduction to Programming contests Standard Template Library Bitwise Magic

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


League of Programmers
IIT Kanpur

September 30, 2011

League of Programmers

Introductory Lecture

clarications Introduction to Programming contests Standard Template Library Bitwise Magic

Aim of the Discussion Series

League of Programmers

Introductory Lecture

clarications Introduction to Programming contests Standard Template Library Bitwise Magic

Aim of the Discussion Series

Discussion camp not a lecture series. You need to show motivation.

League of Programmers

Introductory Lecture

clarications Introduction to Programming contests Standard Template Library Bitwise Magic

Aim of the Discussion Series

Discussion camp not a lecture series. You need to show motivation. To help you code better and faster, introduce you to new tricks mostly to help you do well in programming contests.

League of Programmers

Introductory Lecture

clarications Introduction to Programming contests Standard Template Library Bitwise Magic

Language Issues

Language We stress on: C++

League of Programmers

Introductory Lecture

clarications Introduction to Programming contests Standard Template Library Bitwise Magic

Language Issues

Language We stress on: C++ Slower languages like java and python time out on many judges.

League of Programmers

Introductory Lecture

clarications Introduction to Programming contests Standard Template Library Bitwise Magic

Language Issues

Language We stress on: C++ Slower languages like java and python time out on many judges. C has too restrictive and does not support stl/ templates / classes.

League of Programmers

Introductory Lecture

clarications Introduction to Programming contests Standard Template Library Bitwise Magic

Popular Websites to practice on

League of Programmers

Introductory Lecture

clarications Introduction to Programming contests Standard Template Library Bitwise Magic

Popular Websites to practice on

Compete against Indian coders in live contests Codechef

League of Programmers

Introductory Lecture

clarications Introduction to Programming contests Standard Template Library Bitwise Magic

Popular Websites to practice on

Compete against Indian coders in live contests Codechef As problem archive Codeforces/Spoj/UVA/Topcoder/Project Euler

League of Programmers

Introductory Lecture

clarications Introduction to Programming contests Standard Template Library Bitwise Magic

Popular Websites to practice on

Compete against Indian coders in live contests Codechef As problem archive Codeforces/Spoj/UVA/Topcoder/Project Euler Short Programming Contests Codeforces/Topcoder

League of Programmers

Introductory Lecture

clarications Introduction to Programming contests Standard Template Library Bitwise Magic

Popular Websites to practice on

Compete against Indian coders in live contests Codechef As problem archive Codeforces/Spoj/UVA/Topcoder/Project Euler Short Programming Contests Codeforces/Topcoder Tougher Problems acm.sgu.ru/Codechef Monthly challenge/IOI/ACM ICPC previous years archive/Topcoder level 3

League of Programmers

Introductory Lecture

clarications Introduction to Programming contests Standard Template Library Bitwise Magic

Popular Contests to participate in

India: ACM ICPC/ IOPC/ Shastra OPC/ Codecraft/ Codechef Monthly Challenge/ Codechef Cook o

League of Programmers

Introductory Lecture

clarications Introduction to Programming contests Standard Template Library Bitwise Magic

Popular Contests to participate in

India: ACM ICPC/ IOPC/ Shastra OPC/ Codecraft/ Codechef Monthly Challenge/ Codechef Cook o World: Topcode SRMs/ Codeforces/ Facebook Hacker Cup/ Google Code Jam

League of Programmers

Introductory Lecture

clarications Introduction to Programming contests Standard Template Library Bitwise Magic

Common Problems
Overow

League of Programmers

Introductory Lecture

clarications Introduction to Programming contests Standard Template Library Bitwise Magic

Common Problems
Overow Use data type of appropriate size. Choose data type of every variable carefully based on the constraints of the problem Passing Multi Dimensional Arrays

League of Programmers

Introductory Lecture

clarications Introduction to Programming contests Standard Template Library Bitwise Magic

Common Problems
Overow Use data type of appropriate size. Choose data type of every variable carefully based on the constraints of the problem Passing Multi Dimensional Arrays Solution: either use vector stl(convenient) or global arrays of large enough size(faster)

League of Programmers

Introductory Lecture

clarications Introduction to Programming contests Standard Template Library Bitwise Magic

Common Problems
Overow Use data type of appropriate size. Choose data type of every variable carefully based on the constraints of the problem Passing Multi Dimensional Arrays Solution: either use vector stl(convenient) or global arrays of large enough size(faster) Comparing Doubles

League of Programmers

Introductory Lecture

clarications Introduction to Programming contests Standard Template Library Bitwise Magic

Common Problems
Overow Use data type of appropriate size. Choose data type of every variable carefully based on the constraints of the problem Passing Multi Dimensional Arrays Solution: either use vector stl(convenient) or global arrays of large enough size(faster) Comparing Doubles Solution: Always keep cushion of a small .

League of Programmers

Introductory Lecture

clarications Introduction to Programming contests Standard Template Library Bitwise Magic

Common Problems
Overow Use data type of appropriate size. Choose data type of every variable carefully based on the constraints of the problem Passing Multi Dimensional Arrays Solution: either use vector stl(convenient) or global arrays of large enough size(faster) Comparing Doubles Solution: Always keep cushion of a small . Segmentation faults

League of Programmers

Introductory Lecture

clarications Introduction to Programming contests Standard Template Library Bitwise Magic

Common Problems
Overow Use data type of appropriate size. Choose data type of every variable carefully based on the constraints of the problem Passing Multi Dimensional Arrays Solution: either use vector stl(convenient) or global arrays of large enough size(faster) Comparing Doubles Solution: Always keep cushion of a small . Segmentation faults Solution: Use gdb gdb tutorial

League of Programmers

Introductory Lecture

clarications Introduction to Programming contests Standard Template Library Bitwise Magic

Common Problems
Overow Use data type of appropriate size. Choose data type of every variable carefully based on the constraints of the problem Passing Multi Dimensional Arrays Solution: either use vector stl(convenient) or global arrays of large enough size(faster) Comparing Doubles Solution: Always keep cushion of a small . Segmentation faults Solution: Use gdb gdb tutorial From Your side

League of Programmers

Introductory Lecture

clarications Introduction to Programming contests Standard Template Library Bitwise Magic

How to parse a problem???


Understand what the program is expected to do.

League of Programmers

Introductory Lecture

clarications Introduction to Programming contests Standard Template Library Bitwise Magic

How to parse a problem???


Understand what the program is expected to do. Understand the Input/Output format and use exactly that format

League of Programmers

Introductory Lecture

clarications Introduction to Programming contests Standard Template Library Bitwise Magic

How to parse a problem???


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

clarications Introduction to Programming contests Standard Template Library Bitwise Magic

How to parse a problem???


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

clarications Introduction to Programming contests Standard Template Library Bitwise Magic

How to parse a problem???


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?? 1 sec (1 2) 108 operations

League of Programmers

Introductory Lecture

clarications Introduction to Programming contests Standard Template Library Bitwise Magic

How to parse a problem???


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?? 1 sec (1 2) 108 operations x MB x/4 106 sized int arrays

League of Programmers

Introductory Lecture

clarications Introduction to Programming contests Standard Template Library Bitwise Magic

How to parse a problem???


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?? 1 sec (1 2) 108 operations x MB x/4 106 sized int arrays Not all operations are equally fast:

League of Programmers

Introductory Lecture

clarications Introduction to Programming contests Standard Template Library Bitwise Magic

How to parse a problem???


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?? 1 sec (1 2) 108 operations x MB x/4 106 sized int arrays Not all operations are equally fast: operations on unsigned ints/long long are faster

League of Programmers

Introductory Lecture

clarications Introduction to Programming contests Standard Template Library Bitwise Magic

How to parse a problem???


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?? 1 sec (1 2) 108 operations x MB x/4 106 sized int arrays Not all operations are equally fast: operations on unsigned ints/long long are faster bitwise operators and shift operators (&| >><<)

League of Programmers

Introductory Lecture

clarications Introduction to Programming contests Standard Template Library Bitwise Magic

How to parse a problem???


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?? 1 sec (1 2) 108 operations x MB x/4 106 sized int arrays Not all operations are equally fast: operations on unsigned ints/long long are faster bitwise operators and shift operators (&| >><<) Using too much memory ( > 10 MB) slows down programmes

League of Programmers

Introductory Lecture

clarications Introduction to Programming contests Standard Template Library Bitwise Magic

STL
Website: http://www.cplusplus.com/reference what is template ?

League of Programmers

Introductory Lecture

clarications Introduction to Programming contests Standard Template Library Bitwise Magic

STL
Website: http://www.cplusplus.com/reference what is template ? Vector: Dynamic size arrays with insertion/deletion to/from arbitrary locations

League of Programmers

Introductory Lecture

clarications Introduction to Programming contests Standard Template Library Bitwise Magic

STL
Website: http://www.cplusplus.com/reference what is template ? Vector: Dynamic size arrays with insertion/deletion to/from arbitrary locations Set: Just like a mathematical set. Problem:spoj.pl/problems/WEIRDFN Problem:http://www.spoj.pl/problems/HOMO/ Problem:spoj.pl/problems/HISTOGRA

League of Programmers

Introductory Lecture

clarications Introduction to Programming contests Standard Template Library Bitwise Magic

STL
Website: http://www.cplusplus.com/reference what is template ? Vector: Dynamic size arrays with insertion/deletion to/from arbitrary locations Set: Just like a mathematical set. Problem:spoj.pl/problems/WEIRDFN Problem:http://www.spoj.pl/problems/HOMO/ Problem:spoj.pl/problems/HISTOGRA Map: Advanced set Problem:spoj.pl/problems/SUBSEQ

League of Programmers

Introductory Lecture

clarications Introduction to Programming contests Standard Template Library Bitwise Magic

STL
Website: http://www.cplusplus.com/reference what is template ? Vector: Dynamic size arrays with insertion/deletion to/from arbitrary locations Set: Just like a mathematical set. Problem:spoj.pl/problems/WEIRDFN Problem:http://www.spoj.pl/problems/HOMO/ Problem:spoj.pl/problems/HISTOGRA Map: Advanced set Problem:spoj.pl/problems/SUBSEQ string class and stringstream to parse strings useful mostly for topcoder SRM problems

League of Programmers

Introductory Lecture

clarications Introduction to Programming contests Standard Template Library Bitwise Magic

STL
Website: http://www.cplusplus.com/reference what is template ? Vector: Dynamic size arrays with insertion/deletion to/from arbitrary locations Set: Just like a mathematical set. Problem:spoj.pl/problems/WEIRDFN Problem:http://www.spoj.pl/problems/HOMO/ Problem:spoj.pl/problems/HISTOGRA Map: Advanced set Problem:spoj.pl/problems/SUBSEQ string class and stringstream to parse strings useful mostly for topcoder SRM problems memset/ll: initialize arrays

League of Programmers

Introductory Lecture

clarications Introduction to Programming contests Standard Template Library Bitwise Magic

STL
Website: http://www.cplusplus.com/reference what is template ? Vector: Dynamic size arrays with insertion/deletion to/from arbitrary locations Set: Just like a mathematical set. Problem:spoj.pl/problems/WEIRDFN Problem:http://www.spoj.pl/problems/HOMO/ Problem:spoj.pl/problems/HISTOGRA Map: Advanced set Problem:spoj.pl/problems/SUBSEQ string class and stringstream to parse strings useful mostly for topcoder SRM problems memset/ll: initialize arrays Queue can be use as queue and stack
League of Programmers Introductory Lecture

clarications Introduction to Programming contests Standard Template Library Bitwise Magic

STL
Website: http://www.cplusplus.com/reference what is template ? Vector: Dynamic size arrays with insertion/deletion to/from arbitrary locations Set: Just like a mathematical set. Problem:spoj.pl/problems/WEIRDFN Problem:http://www.spoj.pl/problems/HOMO/ Problem:spoj.pl/problems/HISTOGRA Map: Advanced set Problem:spoj.pl/problems/SUBSEQ string class and stringstream to parse strings useful mostly for topcoder SRM problems memset/ll: initialize arrays Queue can be use as queue and stack bitset(next section)
League of Programmers Introductory Lecture

clarications Introduction to Programming contests Standard Template Library Bitwise Magic

STL
Website: http://www.cplusplus.com/reference what is template ? Vector: Dynamic size arrays with insertion/deletion to/from arbitrary locations Set: Just like a mathematical set. Problem:spoj.pl/problems/WEIRDFN Problem:http://www.spoj.pl/problems/HOMO/ Problem:spoj.pl/problems/HISTOGRA Map: Advanced set Problem:spoj.pl/problems/SUBSEQ string class and stringstream to parse strings useful mostly for topcoder SRM problems memset/ll: initialize arrays Queue can be use as queue and stack bitset(next section) League of Programmers dene your own MACROS: for Introductory Lecture blitz-coding

clarications Introduction to Programming contests Standard Template Library Bitwise Magic

Introduction to bitwise operators

League of Programmers

Introductory Lecture

clarications Introduction to Programming contests Standard Template Library Bitwise Magic

Introduction to bitwise operators

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

League of Programmers

Introductory Lecture

clarications Introduction to Programming contests Standard Template Library Bitwise Magic

Introduction to bitwise operators

Numbers are stored in binary and processing on bits is way faster Our weapons: << (left shift), >> (right shift), &(bitwise and), |(bitwise or ), (bitwise xor ) (bitwise not)

League of Programmers

Introductory Lecture

clarications Introduction to Programming contests Standard Template Library Bitwise Magic

Introduction to bitwise operators

Numbers are stored in binary and processing on bits is way faster Our weapons: << (left shift), >> (right shift), &(bitwise and), |(bitwise or ), (bitwise xor ) (bitwise not) speed up the code by upto 100 times.

League of Programmers

Introductory Lecture

clarications Introduction to Programming contests Standard Template Library Bitwise Magic

Introduction to bitwise operators

Numbers are stored in binary and processing on bits is way faster Our weapons: << (left shift), >> (right shift), &(bitwise and), |(bitwise or ), (bitwise xor ) (bitwise not) speed up the code by upto 100 times. caution: try to use bitwise operations on unsigned integers only

League of Programmers

Introductory Lecture

clarications Introduction to Programming contests Standard Template Library Bitwise Magic

Beauty of Bitwise

League of Programmers

Introductory Lecture

clarications Introduction to Programming contests Standard Template Library Bitwise Magic

Beauty of Bitwise
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

clarications Introduction to Programming contests Standard Template Library Bitwise Magic

Beauty of Bitwise
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 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

clarications Introduction to Programming contests Standard Template Library Bitwise Magic

Beauty of Bitwise
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 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 Never multiply or divide or take remainder modulo power of 2
League of Programmers Introductory Lecture

clarications Introduction to Programming contests Standard Template Library Bitwise Magic

Practice Probems

More tricks: graphics.stanford.edu/seander/bithacks.html lab.polygonal.de/2007/05/10/bitwise-gems-fast-integer-math bits.stephan-brumme.com Problems: codechef.com/problems/TEAMSEL spoj.pl/problems/PIZZALOC acm.sgu.ru/problem.php?contest=0&problem=249 codechef.com/problems/SEQUENCE spoj.pl/problems/NGM2 spoj.pl/problems/VILLAGES bitwise dp: community.topcoder.com/stat?c=problem statement&pm=6725&rd=101 community.topcoder.com/stat?c=problem statement&pm=6095&rd=991 community.topcoder.com/stat?c=problem statement&pm=6400&rd=100
League of Programmers Introductory Lecture

You might also like