Introduction to Competitive Programming Instructor: William W.Y
Total Page:16
File Type:pdf, Size:1020Kb
Introduction to Competitive Programming Instructor: William W.Y. Hsu › Course Preliminaries › Introduction CONTENTS › A taste of Ad Hoc Problems 9/11/2016 INTRODUCTION TO COMPETITIVE PROGRAMMING 2 Course Preliminaries Why and what? Another programming course? 9/11/2016 INTRODUCTION TO COMPETITIVE PROGRAMMING 3 What you will learn › Reading comprehension. › Problem evaluation. › Parsing and formatting text. › Tricks to reduce code and bugs. › Generating test cases for your code. 9/11/2016 INTRODUCTION TO COMPETITIVE PROGRAMMING 4 What you will learn › Algorithms that might be practiced – Dynamic Programming (DP) › State-space search, Games – Data structures › Binary indexed tree, Union-find › Computational geometry – Convex hull › Graph algorithms – Flow 9/11/2016 INTRODUCTION TO COMPETITIVE PROGRAMMING 5 Goal › Given a problem, we want to: – Solve it efficiently. – By using algorithms and data structures. – Convert our solution into a program. – Do it as quickly as possible (under pressure). – Do it correctly (without bugs)! › This course will exercise this process. 9/11/2016 INTRODUCTION TO COMPETITIVE PROGRAMMING 6 Why compete › Makes you a better programmer and thinker in many situations › Intangible skill that will set you apart in the workforce › Be first of NTOU to attend NCPC, ACM-ICPC › It's fun :) 9/11/2016 INTRODUCTION TO COMPETITIVE PROGRAMMING 7 Ultimate goal › Please note that being well-versed in competitive programming is not the end goal, but only a means to an end. › The true end goal is to produce all-rounder computer scientists/ programmers who are much readier to produce better software and to face harder CS research problems in the future. 9/11/2016 INTRODUCTION TO COMPETITIVE PROGRAMMING 8 Competitive Programming › Given well-known computer science problems, solve them as fast as possible. – Find a solution that reduces down to a well-known problems, not research problems. – Pass all the judge data correctly. – Solution should run fast enough. – Do not over-engineer the solution. 9/11/2016 INTRODUCTION TO COMPETITIVE PROGRAMMING 9 How? › Study common types of problems. › Show common applications of algorithms and data structures you already know from: – Prof. Lin & Prof. Wu (the algorithms course). – Prof. Lin & Prof. Hsieh (the data structures course). › Introduce other common algorithms and data structures. › Go over some commonly used theory. › Practice problem solving. › Practice programming. › More practice. 9/11/2016 INTRODUCTION TO COMPETITIVE PROGRAMMING 10 How? › More practice. 9/11/2016 INTRODUCTION TO COMPETITIVE PROGRAMMING 11 How? › More practice. 9/11/2016 INTRODUCTION TO COMPETITIVE PROGRAMMING 12 Possible topics › Introduction › Discrete structure › Mathematics › Divide and Conquer › Data Structures & Sorting › Dynamic Programming (DP) › Combinatorial Games › Greedy Algorithms › Binary Trees › Basic Graph Algorithms › Spanning Trees & Shortest Paths › Network Flow Problems › Computational Geometry › String Algorithms (Additional material: Suffix Arrays - A Programming Contest Approach) › Backtracking, Branch & Bound 9/11/2016 INTRODUCTION TO COMPETITIVE PROGRAMMING 13 Possible topics – decomposed › Arrays and iteration problems › backtracking problems › programming style / understanding › constructing subsets and permutations specifications › graph traversal problems › data structure problems › DFS/BFS › elementary data structures › graph algorithms problems › string problems › shortest paths and MST › strings and library functions › dynamic programming problems › sorting problems › edit distance and applications › sorting and library functions › grid problems › arithmetic problems › rectangular and hexagonal grids › arithmetic and algebra › geometry problems › combinatorics problems › geometric primitives and trigonometry › recurrence relations and counting › computational geometry › number theory problems › convex hulls and triangulation › divisibility and modular arithmetic 9/11/2016 INTRODUCTION TO COMPETITIVE PROGRAMMING 14 Forming Quiz Teams (uva10911) Abridged Problem Description: Let ( , ) be the coordinates of a student’s house on a 2D plane. There are students and we want to pair them into groups. Let be the distance between cost = the houses of 2 students in group . Form groups such that 2is minimized. =1 Output the minimum cost. Constraints: 1 8 and 0 , 1000∑ . ≤ ≤ ≤ ≤ Sample input: = 2; Coordinates of the = 4 houses are {1, 1}, {8, 6}, {6, 8}, and {1, 3}. Sample output: 2 cost = 4.83. Can you solve this problem? If so, how many minutes would you likely require to complete the working code 9/11/2016 INTRODUCTION TO COMPETITIVE PROGRAMMING 15 Where are you? › Uncompetitive programmer A (a.k.a. the blurry one): – Step 1: Reads the problem and becomes confused. (This problem is new for him). – Step 2: Tries to code something: Reading the non-trivial input and output. – Step 3: Realizes that all his attempts are not Accepted (AC): › Greedy: Repeatedly pairing the two remaining students with the shortest separating distances gives the Wrong Answer (WA). › Naive Complete Search: Using recursive backtracking and trying › all possible pairings yields Time Limit Exceeded (TLE). 9/11/2016 INTRODUCTION TO COMPETITIVE PROGRAMMING 16 Where are you? › Uncompetitive programmer B (Give up): – Step 1: Reads the problem and realizes that he has seen this problem before. › But also remembers that he has not learned how to solve this kind of problem... › He is not aware of the Dynamic Programming (DP) solution… – Step 2: Skips the problem and reads another problem in the problem set. 9/11/2016 INTRODUCTION TO COMPETITIVE PROGRAMMING 17 Where are you? › (Still) Uncompetitive programmer C (Slow): – Step 1: Reads the problem and realizes that it is a hard problem: ‘minimum weight perfect matching on a small general weighted graph’. › However, since the input size is small, this problem is solvable using DP. › The DP state is a bitmask that describes a matching status, and matching unmatched students and will turn on two bits and in the bitmask. – Step 2: Codes I/O routine, writes recursive top-down DP, tests, debugs >.<... – Step 3: After 3 hours, his solution obtains AC (passed all the secret test data). 9/11/2016 INTRODUCTION TO COMPETITIVE PROGRAMMING 18 Where are you? › Competitive programmer D: – Completes all the steps taken by uncompetitive programmer C in ≤ 30 minutes. › Very competitive programmer E: – A very competitive programmer (e.g. the red ‘target’ coders in TopCoder [32]) would solve this ‘well known’ problem ≤ 15 minutes... 9/11/2016 INTRODUCTION TO COMPETITIVE PROGRAMMING 19 Course Administration › Course website – http://140.121.196.121/wwyhsu/?page_id=959 › Teaching staffs: – Lecturer: ME! › [email protected] › Ext: 6657 (preferably using emails) – TA1: 黃珮婷 [email protected] – TA2: 王浩勳 [email protected] – TA3: 游旻叡 [email protected] › Office hours: TBA 9/11/2016 INTRODUCTION TO COMPETITIVE PROGRAMMING 20 Grading › Tentative grading policy: – Homework and written quizzes (20%) – In class simulations (30%) – Midterm exam (25%) – Final exam (25%) – Extra credit policy for attending public contests: › CPE: 2% + 1% per solved problem › ITSA (月賽, 中文題為主): 1% +1% per solved problem. – ITSA 每年五月另舉辦桂冠挑戰大賽! 5% + 2% per solved problem. › PTC (月賽,以英文題為主): 2.5% + 1.5% per solved problem. › ACM-ICPC Regionals: 5% + 2% per solved problem. › ACM-ICPC World finals: 再說吧~ 9/11/2016 INTRODUCTION TO COMPETITIVE PROGRAMMING 21 Execution › Weekly courses. › Practice (simulation sessions) – Team based. – Vote: AT or RT! › Written exams and homework. – Single solo mode. 9/11/2016 INTRODUCTION TO COMPETITIVE PROGRAMMING 22 Textbook › Steven Halim, Competitive Programming, 3rd Edition, ASIN: B00FG8MNN8. – http://www.amazon.com/Competitive-Programming-Edition- Steven-Halim/dp/B00FG8MNN8 – http://www.lulu.com/shop/http://www.lulu.com/shop/steven- halim/competitive-programming-3- hardcover/hardcover/product-21059922.html (Hardcover, $39.99) – http://www.lulu.com/shop/steven-halim/competitive- programming-3/ebook/product-21976335.html (E-book, $19.99) 9/11/2016 INTRODUCTION TO COMPETITIVE PROGRAMMING 23 References › S. S. Skiena, M. A. Revilla, Programming Challenges: The Programming Contest Training Manual, Springer, 2003 ed. (ISBN-10: 0387001638) › T. Cormen, C. Leiserson, R. Rivest, C. Stein, Introduction to Algorithms, 3rd ed., The MIT Press. (ISBN-10: 0262033844) › S. Skiena, The Algorithm Design Manual, 2nd ed., Springer. (ISBN-10: 1848000693) › R. Graham, D. Knuth, O. Patashnik, Concrete Mathematics: A Foundation for Computer Science. 9/11/2016 INTRODUCTION TO COMPETITIVE PROGRAMMING 24 This is an era of information explosion 9/11/2016 INTRODUCTION TO COMPETITIVE PROGRAMMING 25 References › ITSA: – http://algorithm.csie.ncku.edu.tw/ITSA_PTC/index_ITSA.php › PTC: – http://algorithm.csie.ncku.edu.tw/ITSA_PTC/index_PTC.php › CPE: – https://cpe.cse.nsysu.edu.tw/ › NCPC (程式設計組): – http://ncpc.ntnu.edu.tw/ncpc/showpage.php?id=3 9/11/2016 INTRODUCTION TO COMPETITIVE PROGRAMMING 26 Introduction Let’s get started! The problems › Typical programming contest problems usually consists of: – Problem description – Input description – Output description – Example input/output – A time limit in seconds – A memory limit in megabytes › You are asked to write a program that solves the problem for all valid inputs. – In IOI, partial solved problem receive partial score. › The program must not exceed time or memory limits! 9/11/2016 INTRODUCTION TO COMPETITIVE PROGRAMMING 28 Example problem Problem