Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
180 views

Introduction To Competitive Programming

This document provides an overview of competitive programming, including what it is, why people do it, and how to get started. It defines competitive programming as solving algorithmic problems within time and memory limits. Key reasons for doing it include improving programming skills, having fun solving puzzles, and gaining experience that is valuable for jobs interviews. The document recommends starting on sites like Codeforces and practicing common data structures and algorithms. It also discusses major programming contests like ICPC and resources for learning more about the topic.

Uploaded by

Pikachu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
180 views

Introduction To Competitive Programming

This document provides an overview of competitive programming, including what it is, why people do it, and how to get started. It defines competitive programming as solving algorithmic problems within time and memory limits. Key reasons for doing it include improving programming skills, having fun solving puzzles, and gaining experience that is valuable for jobs interviews. The document recommends starting on sites like Codeforces and practicing common data structures and algorithms. It also discusses major programming contests like ICPC and resources for learning more about the topic.

Uploaded by

Pikachu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 31

Competitive Programming

101
What, Why, and Where?
By Shubham Jain
Structure of talk
● Breaks every few slides - only ask questions by unmuting
mike then (feel free to keep asking questions in chat!)
● Actual Competitive Programming problems sprinkled
throughout the presentation, of various difficulties -
let’s solve some problems! :)
● No need to take notes, we’ll share this presentation with
you later
DISCLAIMER!

All opinions in these slides are my own and do


not represent the opinion of any other person
or organization :)
What is Competitive Programming?
● “Competitive Programming is a mind sport involving
participants trying to program according to some provided
specifications” - Wikipedia
● In a nutshell, creative problem solving using data
structures and algorithms as tools
● A close analogy would be solving mathematical puzzles,
which I’m sure most of you have done as kids!
What is Competitive Programming?
● Given some problem statement, participants write code
which solves this problem
● The code must obey time and memory limits over a number
of test cases to get an “Accepted” verdict
Try this problem!
Given a Fibonacci number, output 3 Fibonacci numbers (not
necessarily different) which sum to this number.

Refresher: Fibonacci numbers are of the form

F(n) = F(n-1)+F(n-2), F(0) = 1, F(1) = 1


Solution
Let’s expand the equation a bit!

Since F(n) = F(n-1) + F(n-2), and F(n-1) = F(n-2) + F(n-3),


we have F(n) = F(n-2) + F(n-2) + F(n-3)

Print F(n-2) two times and F(n-3) one time!

Code it yourself: https://codeforces.com/problemset/problem/199/A


Try this problem!
You are given a matrix A of size n x m. A matrix is called
“good” if no adjacent cells contain the same value, where
two cells are adjacent if they share a side.

Find a good matrix B that satisfies the following:

B[i][j] = A[i][j] or A[i][j]+1 for all i, j


Why do competitive programming?
● Master data structures and algorithms
● Reduce the amount of time to go from idea -> code
● Get used to the type of logical thinking required to
write complicated programs
● Learn to debug your (and other’s!) code quickly
● Easier to write smaller programs than try to build your
own large system from scratch
● A fun way to get comfortable in various programming
languages
Why do competitive programming?
● Participate and win swag such as T-shirts, bags,
electronics and cash from various competitions
● Makes you a desirable candidate to most companies -
Competitive Programming problems are very often used as
hiring tools in interviews or placement tests
● Most importantly - because it’s a very fun sport!
● Can potentially become a lifelong hobby :)
Try this problem!
You are given a matrix A of size n x m. A matrix is called
“good” if no adjacent cells contain the same value, where
two cells are adjacent if they share a side.

Find a good matrix B that satisfies the following:

B[i][j] = A[i][j] or A[i][j]+1 for all i, j


Solution
Look at the matrix as a “chessboard”, with white being an
odd number and black being an even number. If a matrix has
odd numbers at position where (i+j)%2 == 0 and even numbers
in other places, adjacent values can never be equal.

Also, for every value to convert it into odd/even, we will


require to add at most 1!

This was actually a relatively hard problem! :)

Code it yourself: https://codeforces.com/contest/1438/problem/C


Try this problem! - Interactive problem
I just thought of a polynomial. I guarantee you that every
coefficient of this polynomial is positive! You need to find
the polynomial I thought of in two moves. In a single move,
you can ask me the value of my polynomial at some point c.

(Note that no coefficient can be zero either!)

Eg. My polynomial

Your queries: value at 0? My answer: P(0) = 1

value at -1? My answer: P(-1) = -5


How to do competitive programming?
● Understand basics of writing a program - CS101 sufficient
(What is an array, pointer, etc)
● Start practicing basic implementation problems on a
competitive programming website (names later)
● Understand complexity analysis - space and time
● Learn the various data structures or STL in your favorite
language (preferred C++) - map, vector, set, list, etc.
● After basic implementations, start studying algorithms
and data structures and continue solving!
● Take part in weekly contests and track your progress :)
Which programming language to use?
Strongly prefer C++! Advantages:

● Fast and efficient


● Already learnt in CS101
● Lots of templates and pre-written code available
● Most time limits are set wrt C++, so you might get TLE
with other languages. Some(most) of the times, authors
don’t write solutions in alternate languages at all!

Can alternatively use: Java, Python, Kotlin

If you want to be ridiculous: OCaml, Javascript, Delphi...


Where do I write and check code? (mostly for C++)
Ubuntu: Vim, SublimeText

Windows: Codeblocks, SublimeText

MacOS: Codeblocks, Atom / SublimeText, Vim

Online IDES: Is slower than offline IDEs due to high traffic


during competitions. Might want to eventually move to
offline IDEs. CSAcademy, Codechef

Thanks to Siddhartha Srivastava, IITK for this slide


Which programming sites to practice on?
My personal preference list in terms of quality problems:

● Codeforces - All kinds of quality problems


● Atcoder - Many math-y problems, less coding
● Codechef - Many data structure problems, more code
● USACO - Good problems, contests 4 times / year
● CSAcademy - Dead site, good problems
● Hackerrank - Almost no contests now
● Topcoder - Many boring problems, sometimes good
● Hackerearth - Very low quality bar, sometimes good
● Leetcode - Best for placement/internship purposes
Which programming contests to do?
There are a variety of major programming contests I like or
recommend people to participate in, including:

● ICPC (3 person, 5h, olympiad of programming)


● Google Codejam
● Facebook Hackercup
● Google Hashcode (4 person, 4h, approximation problems)
● Quora Programming Contest (Returning this year!)
● Topcoder Open (TCO) / Topcoder Collegiate Contest (TCC)

(only including ones I know are being held in 2021)


ICPC - when?
This year - uncertain dates, but will probably happen

Usually, timeline:

Registration of teams: opens August-September

Online Round: Mid October

Regional Rounds: Month of December

Super-regional (Asia West Championship): Mid January

World Finals: March - June


ICPC - how?
● Online Round - usually consists of 5-8 problems to be
solved by a team of 3 people in 3 hours
● Qualification to regionals - around top 15-20 teams of
IITB qualify to regionals depending on quota. Some
regionals only allow 1 team from an institute -
Amritapuri will take top-300ish merit based
● Regional Rounds - usually 10-14 problems, 5 hour contest
● Same format followed for super-regional and WF
How does IITB do at ICPC?
We almost always have a team at world finals, but there are
not many teams participating from the institution itself.

● 2013 - ? - Honourable Mention


● 2014 - Dracarys - Honourable Mention
● 2015 - codetemplars - World #64
● 2016 - Resilient - Honourable Mention
● 2017 - Team Rocket - World #34
● 2018 - N/A
● 2019 - DeathNote - Honourable Mention
● 2020 - BhagwanBharose - ??
What to study for ICPC?
● Standard Template Library (STL) - set, vector, etc.
● Binary Search, Ternary Search etc.
● Sorting algorithms
● Writing brute force algorithms, backtracking, pruning
● Greedy algorithms
● Dynamic Programming
● Graph algorithms
● Mathematics - Linear algebra, Combinatorics, Number
Theory, Geometry
● String algorithms - KMP, Z-algo, etc.
● Other advanced algos - Flows, Suffix structures, etc.
Resources to study Competitive programming?
● https://cp-algorithms.com/
● “Competitive Programmer’s Handbook”, Antti Laaksonen
● Competitive Programming 4 (CP4), Steven Halim
● List of useful tutorials -
https://codeforces.com/blog/entry/23054
● Introduction to Algorithms, CLRS
Try this problem! - Interactive problem
I just thought of a polynomial. I guarantee you that every
coefficient of this polynomial is positive! You need to find
the polynomial I thought of in two moves. In a single move,
you can ask me the value of my polynomial at some point c.

(Note that no coefficient can be zero either!)

Eg. My polynomial

Your queries: value at 0? My answer: P(0) = 1

value at -1? My answer: P(-1) = -5


Solution
Sounds impossible, doesn’t it? ;)

First, query P(1). This will give you summation of the


coefficients of the polynomial. Now, query P(P(1)), and
you’re done, you can express everything in base P(1)!

Explanation: Let’s take

Querying P(1) = 1+4+5+7 = 17

Querying P(P(1)) =

Any number can be expressed uniquely in base P(1)! (Why?)


Questions?

You might also like