Competitive Programming and Contests

This the Web page of "Competitive Programming and Contests" course at University of Pisa.

  • Teacher: Rossano Venturini
  • CFU: 6
  • Period: First semester
  • Language: English
  • Lectures schedule: Monday 16-18 Room C and Wednesday 14-16 Room C1
  • Telegram Group: here
  • Teams Group: here
  • Question time: After lectures or by appointment


Goals and opportunities

The goal of the course is to improve programming and problem solving skills of the students by facing them with difficult problems and by presenting the techniques that help their reasoning in the implementation of correct and efficient solutions. The importance of these skills has been recognized by the most important software companies worldwide, which evaluate candidates in their job interviews mostly by the ability in addressing such difficult problems (e.g., see here).

A natural goal is to involve the students in the intellectual pleasure of programming and problem solving, also preparing them for the most important international online contests, such as TopcoderCodeforcesLeetCode, HackerRank, CodeChef, Facebook Hacker Cup, Google Code Jam and so on, for internships in most important companies and their interviews. A desirable side-effect of the course could be to organize and prepare teams of students for online contests.

The course will provide the opportunity of:

  • facing with challenging algorithmic problems;
  • improving problem solving and programming skills;
  • getting in touch with some big companies for internships, scholarships, or thesis proposals.


Exam

You have two choices for the exam:

  • Option 1: Submit your solutions for the three hands-on assignments within the specified deadlines for each assignment.

  • Option 2: Submit your solutions for the three hands-on assignments, along with all the mandatory exercises, at a time that suits you. You can find the mandatory exercises highlighted in bold within the lecture materials provided below.

In either case, you will be required to participate in an oral examination covering all the topics covered in our course. You can schedule this oral exam by appointment.

Please note that all solutions must be written in Rust.

You have the opportunity to earn additional points through:

  • Active Participation: Actively engage in class discussions and activities.

  • Serious Participation in Online Contests: Engage seriously in online programming contests such as CodeForces, TopCoder, HackerRank, Google Code Jam, and more.

  • Successful Interviews with Major Companies: Achieving success in interviews with prominent companies.

It is highly recommended that you implement solutions for the problems presented in each lecture. This practice not only enhances your problem-solving skills but also provides valuable Rust programming experience.

I recommend creating a Github repository to centralize all your solutions and their corresponding descriptions. This repository can be set to either private or public visibility, ensuring that I can access it in either case. Please share the repository link with me, and make sure to keep it regularly updated so that I can monitor your progress effectively.


How to solve a problem

Here’s a step-by-step guide on how to effectively solve a problem:

  • Read the Problem Description: Begin by carefully reading the problem statement. Ensure that you have a clear understanding of what the problem is asking you to solve.
  • Understand with Examples: Examine any provided examples. Use these examples to validate your understanding of the problem’s requirements and constraints.
  • Design a Trivial Solution: Start by designing a straightforward and naive solution to the problem. This is often referred to as a brute-force approach. It may not be the most efficient solution, but it helps you get started and ensures that you have a working solution.
  • Brainstorm for Efficiency: Once you have a working solution, think about how you can optimize it. Consider different algorithms, data structures, and strategies that might lead to a more efficient solution. Use running examples to help you visualize and refine your approach.
  • Seek Help and Hints: If you’re stuck and unable to find a more efficient solution, don’t hesitate to seek help. Discuss the problem with other students, ask questions in the group, or consult resources like your course materials, relevant websites, or textbooks. Gathering hints and insights from others is a valuable part of the learning process.
  • Document Your Solution: Write a brief description of your chosen solution in English. Explain the logic behind it and provide an analysis of its time and space complexity. This documentation helps you clarify your thought process and is valuable for future reference.
  • Implement Your Solution: Write the code for your solution in Rust. Ensure that your code follows the algorithm you’ve designed and documented.
  • Test and Debug: Submit your implementation to the problem’s platform or testing environment. Work on debugging and refining your code until it passes all the tests and produces correct results.
  • Compare Solutions: Always compare your solution and implementation with existing ones if available. This can help you learn different approaches and best practices for solving similar problems.
  • Iterate and Learn: Solving problems is a learning process. Don’t be discouraged if you encounter difficulties. Keep practicing, learning from your mistakes, and applying new techniques to improve your problem-solving skills.

By following these steps, you can systematically approach problem-solving tasks and increase your proficiency in tackling a wide range of challenges.


Background

If you’re looking to brush up on your understanding of fundamental algorithms and data structures, I highly recommend delving into the renowned book Introduction to Algorithms, 4th Edition by Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, Clifford Stein.

I strongly suggest you to watch the following video lectures as soon as possible.


References

  • Introduction to Algorithms,  4th Edition, Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, Clifford Stein, The MIT Press, 2022 (Amazon) [CCLR]
  • Algorithms, 4th Edition, Robert Sedgewick, Kevin Wayne, Addison-Wesley Professional, 2011 (Amazon) [RS]
  • Algorithms, Sanjoy Dasgupta, Christos Papadimitriou, Umesh Vazirani, McGraw-Hill, 2006. (Amazon) [DPZ]
  • Programming Challenges: The Programming Contest Training Manual, Steven S. Skiena, Miguel A. Revilla, Springer, 2003 (Amazon) [SR]
  • Competitive Programming 4: The New Lower Bound of Programming Contests, Steven Halim, Felix Halim, (here) [HH]


Rust References



Lectures

Date Lecture References Problems
16/09/2024 Introduction Slides Leaders in array, Kadane’s algorithm, and Missing number in array
18/09/2024 Introduction to Rust Slides  
23/09/2024 Solutions of Trapping rain water and Sliding window maximum Rossano’s notes Trapping rain water and Sliding window maximum
25/09/2024 Binary search Rossano’s notes and USACO Guide Find First and Last Position of Element in Sorted Array, Find the minimum in a rotated sorted array, and Search for a peak in an (unsorted) array
30/09/2024 Trees: representation, traversals, and Binary Search Tree Tree traversals Maximum path sum
02/10/2024 Trees: representation, traversals, and Binary Search Tree. Two pointers trick [CCLR] Chapters 10.4 and 12 Frogs and Mosquitoes
07/10/2024 Hands-On 1. Deadline: 21/10/2024    
09/10/2024 Sweep Line Algorithm Rossano’s notes. Two pointers technique Check if All the Integers in a Range Are Covered, Cow Steeplechase II, and Longest k-Good Segment
14/10/2024 Prefix Sums Rossano’s notes Subarray Sum Equals K, Continuous Subarray Sum, and Good Subarrays
16/10/2024 Prefix sum: Fenwick tree (aka Binary Indexed Tree (BIT)) Rossano’s notes*. Wikipediatutorialvideo, and visualgo. Update the array
21/10/2024 Applications of Fenwick tree and Range update with Fenwick tree. Rossano’s notes*. Range updates on BIT. Tutorial. RMQ with BIT (optional) Nested segments and Pashmak and Parmida’s problem
23/10/2024 Segment Trees Segment Tree: description, tutorial 1, tutorial 2, tutorial 3, video, and visualgo. Solve Nested segments with Segment trees.
28/10/2024 Segment trees: Applications    
30/10/2024 Segment Trees: Lazy Propagation and Persistency Lazy propagation: tutorial and video. Circular RMQ
04/11/2024 Mo’s algorithm on sequences and trees Rossano’s notes*. Optional: Mo’s algorithm on Trees Powerful array and Tree and queries
06/11/2024 Dynamic Programming: Fibonacci numbers, Rod cutting, and Shortest path on a DAG Rossano’s notes*. [CCLR] Chapter 15.  
06/11/2024 Hands-On 2. Deadline: 24/11/2024    
13/11/2024 Dynamic Programming: Minimum cost path and Longest common subsequence Rossano’s notes*. Martin Gardner on Minimum cost path. Longest common subsequence and Minimum number of jumps
18/11/2024 Dynamic Programming: 0/1 Knapsack, Fractional knapsack, and Subset sum. Rossano’s notes*. 0/1 Knapsack problem: tutorial Subset sum and 0-1 knapsack
20/11/2024 Dynamic Programming: Longest increasing subsequence and Coin change Rossano’s notes* Longest increasing subsequence
25/11/2024 Greedy algorithms: Activity Selection, Job sequencing, and Fractional knapsack problem. Dynamic Programming: Longest bitonic subsequence, and Largest independent set on trees Notes by Jeff Erickson. Job sequencing. Fractional Knapsack Problem N meetings in one room, Magic numbers, Wilbur and array, and Alternative thinking
27/11/2024 Greedy Algorithms: Boxes and Hero Boxes and Hero. Huffman code: Section 7.4 in Notes by Jeff Erickson (optional). Lexicographically maximum subsequence, Woodcutters, and Queue
02/12/2024 Boyer-Moore algorithm for majority element. Misra–Gries heavy hitters algorithm. Finding a duplicated element in a array: Solution with or without destroying the array, ir with log n passes.    
04/12/2024 Suffix Array Suffix Array: Tutorial and implementation: here and here.  
05/12/2024 Hands-On 3. Deadline: 10/01/2025    



Last Year Lectures


Topics from past years

Date Lecture References Problems
2017 Centroid Decomposition of trees Centroid Decomposition of trees: here, here, here, and here  
2017 String algorithms: Knuth-Morris-Pratt and Suffix array Knuth-Morris-Pratt from [CLRS] Chapter 32.3. Knuth-Morris-Pratt. Optional: Rabin-Karp here from Algorithms on strings, trees, and sequences, D. Gusfield, Cambridge university press, here, and here. Suffix Array: Tutorial and implementation: here and here. Optional: Suffix Array in linear time here Longest prefix suffix and Shift the string
2017 String algorithms: LCP array, trie and ternary search trie Computing LCP array: Kasai et al.’s algorithm and here. Ternary search trie and a video.  
2018 Heavy-light Decomposition of trees. This lecture is not mandatory. Heavy-light Decomposition of trees: here, here, and here. QTREE, QTREE2, QTREE3, GOT, and Chef and the tree.
2021 Dynamic Programming: Edit distance, Longest palindromic subsequence, and Weighted job scheduling   Edit distance, Vertex cover, and Longest palindromic subsequence
2022 Static RMQ with sparse table RMQ and sparse table: tutorial 1, tutorial 2, paper, and code. Static RMQ in 2n + o(n) bits and constant time(optional).  
2023 Graph algorithms: BFS and DFS [CCLR] Chapter 22 X total shapes, IsBipartite, and Fox and names
2023 Graph algorithms: Strongly Connected Components and Single-Source Shortest Path [CCLR] Chapters 22 and 23 Learning languages and Checkposts
2023 Graph algorithms: Minimum Spanning Tree (and Disjoint Sets data structures) [CCLR] Chapters 21 and 23. Kruskal: code, Disjoint Set: tutorial Minimum spanning tree


Further (optional) topics