HeadStart 2 1
HeadStart 2 1
Note:
The articles mentioned contain theoretical concepts and should be
read by everyone. The codes might be given in C++, but we can
correspondingly develop the python code from the theory given
The articles from USACO Guide can also be accessed in both python
and C++ by changing the language using an option on the webpag
Python users can checkout our GitHub repo which contains the
code of some useful functions to be covered ahead.
At the core of the C++ Standard Template Library are the following three
well-structured components − containers, algorithms, and iterators.
Basic knowledge of STL is quite handy, and in fact, necessary for
competitive programming.
Resources
Vector: One of the most powerful and frequently used STL containers.
Vectors are the same as dynamic arrays with the ability to resize
themselves automatically when an element is inserted or deleted.
Study these functions only:
*The use of these containers and their functions will be explained in the
upcoming weeks
Priority Queue: Priority queues are queues such that the first element
is either the greatest or the smallest of all elements in the queue and
elements are in nonincreasing order.
List: Lists are used to store multiple values(they may not be of the
same data type) in a single variable, instead of declaring separate
variables for each value
Heap: The property of this data structure is that each time the
priority queue.
Math: It contains all the basic Math operations ranging from ceil, and
floor to factorial, pow, etc
Sys: It’s generally used for some specific functions revolving around
the std input and output. Eg: sys.stdin.readline() (which takes input
faster than the normal input function), sys.stdout.flush() (used in
interactive problems) , sys.setrecursionlimit(int) (to increase the
recursion limit) , et
and b .
O(len(list)) .
There are several sorting algorithms. Some basic sorting algorithms are
as following to give you an idea of sorting techniques
Insertion Sor
Articl
Video
2. Merge Sor
Articl
Video
While doing CP problems, we don’t write any sorting algorithm. C++ STL
provides a function to sort elements in any order in O(N log N) time.
SYNTAX - C++
Array : sort(arr, arr+arr.size());
Formally, sort( it1, it2) will sort elements in ascending order in the range
[it1, it2) .
SYNTAX - Python
Dictionary/Set : sorted(iterable,reverse=True/False)
Sorting a list of pairs can be done by the normal sort function itself. Let’s
You can learn more about custom sort using lambda function.
Practice Problem
Smallest Pai
Incinerat
Stick Length
Divisibility by 2^
Bombs (tough)
Additional Question
Delete Them
GREEDY ALGORITHM
A greedy algorithm, as the name suggests, always makes the choice that
seems to be the best at that moment. This means that it makes a locally-
optimal choice in the hope that this choice will lead to a globally-optimal
solution. Problem list is larger because greedy can be learned only
through problem solving.
Resource
Watch this lecture on Algomaniac
How to prove greedy algorithm is correct (not so important for CP)
Practice Problem
Different Difference
Kathmand
Berland Musi
Closing the Ga
Paprika and Permutatio
Tea with Tangarine
Zero Arra
Make it Goo
Rudolf and the Another Competitio
The Vowel Matri
Negative Prefixes
M-array
Knapsac
Particles (tough)
Additional Question
Problems by Algomaniacs
COMBINATORICS (Expected Time: 0-1 days)
Remember your JEE days? Well guess what… the stuff you have learnt for
JEE doesn’t go for waste, it has its own importance in CP too!
CALCULATING
Naïve method
Optimal method
You must have also learnt an alternate formula for nCr which is
n!/(r!*(n-r)!). This can be used to find out the binomial coefficients too.
Also we know that factorials tend to be pretty large which can overflow
the integer size (and in python even though you can store integers of
very long size, it takes up a lot of time giving TLE), so we generally we
calculate it modulo some number (which is generally prime). This can be
done in O(n+log(mod)) complexity by precomputing the factorials and
inverse factorials individually and then using them in the problem as
required. Code is available at USACO.
Practice Problem
Binomial Coefficient
Creating Strings I
Binary Strings are Fu
Close Tuples
Additional Question
USACO Problemset
Common Errors
Even if you try your best to write a flawless piece of code, you may still
encounter some error. Don’t worry it is a part of the journey. Refer this
blog on codeforces which almost covers all possible types of common
mistakes.
General Advice
We hope that you all are following this course so far and also giving
contests regularly. Problems of Week 1 contest were all doable. Do
give it a second try if you were not able to do it in the contest
Don’t worry about about the massive information revealed to you this
week in STL. Use Google whenever required. You will eventually learn
it by practice like Rotational Dynamics during JEE :
Always try to solve that one problem you could not do in contest, after
the contest ends. Consistent up-solving contributes significantly in
improving CP skills
If you turn to reading editorial, read hints/partial solution and give it a
try again. Implementing solutions on your own guarantees that you
properly understood the problem and the solution
Maintain healthy competition, Discuss your solutions with your
friends after contest ends. Doing CP with friends not only makes it
more exciting and fun but also helps in discovering different ways to
approach a problem.
You will definitely have contests where you under-perform and your
rating drops. Brush that negative feeling off as quick as possible. You
always have the next contest waiting for you :)
Sneak Peek
Excited for the next week? So are we! Expect a lot of new thrilling topics
and handful of challenges waiting to be conquered.
Solve plenty of problems in the mean time and cheers for coming this far!