64
Introduction to Competitive Programming Instructor: William W.Y. Hsu

Introduction to Competitive Programming...Introduction. Let’s get started! › Typical programming contest problems usually consists of: – Problem description – Input description

  • Upload
    others

  • View
    8

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Introduction to Competitive Programming...Introduction. Let’s get started! › Typical programming contest problems usually consists of: – Problem description – Input description

Introduction to Competitive ProgrammingInstructor: William W.Y. Hsu

Page 2: Introduction to Competitive Programming...Introduction. Let’s get started! › Typical programming contest problems usually consists of: – Problem description – Input description

CONTENTS

› Course Preliminaries

› Introduction

› A taste of Ad Hoc Problems

9/23/2019 2INTRODUCTION TO COMPETITIVE PROGRAMMING

Page 3: Introduction to Competitive Programming...Introduction. Let’s get started! › Typical programming contest problems usually consists of: – Problem description – Input description

Course Preliminaries Why and what? Another programming course?

9/23/2019 3INTRODUCTION TO COMPETITIVE PROGRAMMING

Page 4: Introduction to Competitive Programming...Introduction. Let’s get started! › Typical programming contest problems usually consists of: – Problem description – Input description

› Reading comprehension.

› Problem evaluation.

› Parsing and formatting text.

› Tricks to reduce code and bugs.

› Generating test cases for your code.

What you will learn

9/23/2019 INTRODUCTION TO COMPETITIVE PROGRAMMING 4

Page 5: Introduction to Competitive Programming...Introduction. Let’s get started! › Typical programming contest problems usually consists of: – Problem description – Input description

› 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

What you will learn

9/23/2019 INTRODUCTION TO COMPETITIVE PROGRAMMING 5

Page 6: Introduction to Competitive Programming...Introduction. Let’s get started! › Typical programming contest problems usually consists of: – Problem description – Input description

› 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.

Goal

9/23/2019 INTRODUCTION TO COMPETITIVE PROGRAMMING 6

Page 7: Introduction to Competitive Programming...Introduction. Let’s get started! › Typical programming contest problems usually consists of: – Problem description – Input description

› 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 :)

Why compete

9/23/2019 INTRODUCTION TO COMPETITIVE PROGRAMMING 7

Page 8: Introduction to Competitive Programming...Introduction. Let’s get started! › Typical programming contest problems usually consists of: – Problem description – Input description

› 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.

Ultimate goal

9/23/2019 INTRODUCTION TO COMPETITIVE PROGRAMMING 8

Page 9: Introduction to Competitive Programming...Introduction. Let’s get started! › Typical programming contest problems usually consists of: – Problem description – Input description

› 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.

Competitive Programming

9/23/2019 INTRODUCTION TO COMPETITIVE PROGRAMMING 9

Page 10: Introduction to Competitive Programming...Introduction. Let’s get started! › Typical programming contest problems usually consists of: – Problem description – Input description

› 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.

How?

9/23/2019 INTRODUCTION TO COMPETITIVE PROGRAMMING 10

Page 11: Introduction to Competitive Programming...Introduction. Let’s get started! › Typical programming contest problems usually consists of: – Problem description – Input description

› More practice.

How?

9/23/2019 INTRODUCTION TO COMPETITIVE PROGRAMMING 11

Page 12: Introduction to Competitive Programming...Introduction. Let’s get started! › Typical programming contest problems usually consists of: – Problem description – Input description

› More practice.

How?

9/23/2019 INTRODUCTION TO COMPETITIVE PROGRAMMING 12

Page 13: Introduction to Competitive Programming...Introduction. Let’s get started! › Typical programming contest problems usually consists of: – Problem description – Input description

› 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

Possible topics

9/23/2019 INTRODUCTION TO COMPETITIVE PROGRAMMING 13

Page 14: Introduction to Competitive Programming...Introduction. Let’s get started! › Typical programming contest problems usually consists of: – Problem description – Input description

Possible topics – decomposed› Arrays and iteration problems

› programming style / understanding specifications

› data structure problems

› elementary data structures

› string problems

› strings and library functions

› sorting problems

› sorting and library functions

› arithmetic problems

› arithmetic and algebra

› combinatorics problems

› recurrence relations and counting

› number theory problems

› divisibility and modular arithmetic

› backtracking problems

› constructing subsets and permutations

› graph traversal problems

› DFS/BFS

› graph algorithms problems

› shortest paths and MST

› dynamic programming problems

› edit distance and applications

› grid problems

› rectangular and hexagonal grids

› geometry problems

› geometric primitives and trigonometry

› computational geometry

› convex hulls and triangulation

9/23/2019 INTRODUCTION TO COMPETITIVE PROGRAMMING 14

Page 15: Introduction to Competitive Programming...Introduction. Let’s get started! › Typical programming contest problems usually consists of: – Problem description – Input description

Abridged Problem Description:

Let (𝑥𝑥,𝑦𝑦) be the coordinates of a student’s house on a 2D plane. There are 2𝑁𝑁students and we want to pair them into 𝑁𝑁 groups. Let 𝑑𝑑𝑖𝑖 be the distance between the houses of 2 students in group 𝑖𝑖. Form 𝑁𝑁 groups such that cost = ∑𝑖𝑖=1𝑁𝑁 𝑑𝑑𝑖𝑖 is minimized.

Output the minimum cost. Constraints: 1 ≤ 𝑁𝑁 ≤ 8 and 0 ≤ 𝑥𝑥,𝑦𝑦 ≤ 1000.

Sample input:

𝑁𝑁 = 2; Coordinates of the 2𝑁𝑁 = 4 houses are {1, 1}, {8, 6}, {6, 8}, and {1, 3}.

Sample output:

cost = 4.83.

Can you solve this problem?

If so, how many minutes would you likely require to complete the working code

Forming Quiz Teams (uva10911)

9/23/2019 INTRODUCTION TO COMPETITIVE PROGRAMMING 15

Page 16: Introduction to Competitive Programming...Introduction. Let’s get started! › Typical programming contest problems usually consists of: – Problem description – Input description

› 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).

Where are you?

9/23/2019 INTRODUCTION TO COMPETITIVE PROGRAMMING 16

Page 17: Introduction to Competitive Programming...Introduction. Let’s get started! › Typical programming contest problems usually consists of: – Problem description – Input description

› 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.

Where are you?

9/23/2019 INTRODUCTION TO COMPETITIVE PROGRAMMING 17

Page 18: Introduction to Competitive Programming...Introduction. Let’s get started! › Typical programming contest problems usually consists of: – Problem description – Input description

› (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).

Where are you?

9/23/2019 INTRODUCTION TO COMPETITIVE PROGRAMMING 18

Page 19: Introduction to Competitive Programming...Introduction. Let’s get started! › Typical programming contest problems usually consists of: – Problem description – Input description

› 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...

Where are you?

9/23/2019 INTRODUCTION TO COMPETITIVE PROGRAMMING 19

Page 20: Introduction to Competitive Programming...Introduction. Let’s get started! › Typical programming contest problems usually consists of: – Problem description – Input description

› Course website– http://www.deepsea9.taipei/aclab/index.php/introduction-to-

competitive-programming/

› Teaching staffs:– Lecturer: ME!

[email protected]› Ext: 6657 (preferably using emails)

› Teaching assistant:– 胡瑞興: [email protected]– 邱瀚毅: [email protected]– 趙俊彥: [email protected]– 林志謙: [email protected]– Office phone: 6636

› Office hours: Monday 12:00PM~2:00PM

Course Administration

9/23/2019 INTRODUCTION TO COMPETITIVE PROGRAMMING 20

Page 21: Introduction to Competitive Programming...Introduction. Let’s get started! › Typical programming contest problems usually consists of: – Problem description – Input description

› Tentative grading policy:– In class simulations (30%) Teams of 3– Midterm exam (20%) Solo– Final exam (20%) Solo– Attending public contests (certified) (30%), extra score can be

earned:› ITSA (雙月賽, 中文題為主): 1% + 0.5% per solved problem.

– ITSA 每年五月另舉辦桂冠挑戰大賽! 5% + 2% per solved problem. (所以計概課程碰不到, 而且廢止了)

› 其他學校辦的公開賽, 如中區程式設計大賽, 靜宜杯, 北區大賽等: 1% + 1% per solved problem.

› CPE: 2% + 1% per solved problem› PTC (月賽,以英文題為主, 廢止了): 2% + 2% per solved problem› ICPC-TOPC: 2% + 2% per solved problem› NCPC (教育部大專程式設計競賽, ICPC前導賽): 3% + 2% per

solved problem (預賽也算)› ICPC Regionals: 5% + 2% per solved problem› ICPC World finals: 再說吧~

Grading

9/23/2019 INTRODUCTION TO COMPETITIVE PROGRAMMING 21

Page 22: Introduction to Competitive Programming...Introduction. Let’s get started! › Typical programming contest problems usually consists of: – Problem description – Input description

› In class simulation and term exams are computed using ratings.

Grading

9/23/2019 INTRODUCTION TO COMPETITIVE PROGRAMMING 22

Page 23: Introduction to Competitive Programming...Introduction. Let’s get started! › Typical programming contest problems usually consists of: – Problem description – Input description

› Weekly courses.

› Practice (simulation sessions)– Team based.– Vote: AT or RT!

› Written exams and homework.– Single solo mode.

Execution

9/23/2019 INTRODUCTION TO COMPETITIVE PROGRAMMING 23

Page 24: Introduction to Competitive Programming...Introduction. Let’s get started! › Typical programming contest problems usually consists of: – Problem description – Input description

› 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)

Textbook

9/23/2019 24INTRODUCTION TO COMPETITIVE PROGRAMMING

Page 25: Introduction to Competitive Programming...Introduction. Let’s get started! › Typical programming contest problems usually consists of: – Problem description – Input description

› 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.

References

9/23/2019 INTRODUCTION TO COMPETITIVE PROGRAMMING 25

Page 26: Introduction to Competitive Programming...Introduction. Let’s get started! › Typical programming contest problems usually consists of: – Problem description – Input description

This is an era of information explosion

9/23/2019 INTRODUCTION TO COMPETITIVE PROGRAMMING 26

Page 27: Introduction to Competitive Programming...Introduction. Let’s get started! › Typical programming contest problems usually consists of: – Problem description – Input description

› 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

References

9/23/2019 INTRODUCTION TO COMPETITIVE PROGRAMMING 27

Page 28: Introduction to Competitive Programming...Introduction. Let’s get started! › Typical programming contest problems usually consists of: – Problem description – Input description

IntroductionLet’s get started!

Page 29: Introduction to Competitive Programming...Introduction. Let’s get started! › Typical programming contest problems usually consists of: – Problem description – Input description

› 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!

The problems

9/23/2019 INTRODUCTION TO COMPETITIVE PROGRAMMING 29

Page 30: Introduction to Competitive Programming...Introduction. Let’s get started! › Typical programming contest problems usually consists of: – Problem description – Input description

Problem descriptionWrite a program that multiplies pairs of integers.

Input descriptionInput starts with one line containing an integer 𝑇𝑇, where 1 ≤𝑇𝑇 ≤ 100, denoting the number of test cases. Then 𝑇𝑇 linesfollow, each containing a test case. Each test case consists oftwo integers 𝐴𝐴,𝐵𝐵, where −220 ≤ A,𝐵𝐵 ≤ 220, separated by asingle space.

Output descriptionFor each test case, output one line containing the value of 𝐴𝐴 ×𝐵𝐵.

Example problem

9/23/2019 INTRODUCTION TO COMPETITIVE PROGRAMMING 30

Page 31: Introduction to Competitive Programming...Introduction. Let’s get started! › Typical programming contest problems usually consists of: – Problem description – Input description

Sample input Sample output4 123 4 013 0 81 8 10000100 100

Example problem

9/23/2019 INTRODUCTION TO COMPETITIVE PROGRAMMING 31

Page 32: Introduction to Competitive Programming...Introduction. Let’s get started! › Typical programming contest problems usually consists of: – Problem description – Input description

› The solutions are incorrect!

› If 𝐴𝐴 = 𝐵𝐵 = 220, the output is 0.– Correct solution is 240.

› 32-bit integer is too small, overflow will occur.– Can switch to 64-bit integer mode. (Architecture dependant)

› Remedy:– long long int

Correctness?

9/23/2019 INTRODUCTION TO COMPETITIVE PROGRAMMING 32

Page 33: Introduction to Competitive Programming...Introduction. Let’s get started! › Typical programming contest problems usually consists of: – Problem description – Input description

› Automatic judging system will be declared per simulation.– DomJudge– Virtual Judge– IOI CMS

› You can submit in any of the supported languages:– C– C++– Java (IOI spec 2015)– Python

Automatic judging

9/23/2019 INTRODUCTION TO COMPETITIVE PROGRAMMING 33

Page 34: Introduction to Competitive Programming...Introduction. Let’s get started! › Typical programming contest problems usually consists of: – Problem description – Input description

› Feedback about solutions is limited

› You will (usually) receive one of:– Accepted– Wrong Answer– Compile Error– Run Time Error– Time Limit Exceeded– Memory Limit Exceeded

Judge verdicts (ACM-ICPC)

9/23/2019 INTRODUCTION TO COMPETITIVE PROGRAMMING 34

Page 35: Introduction to Competitive Programming...Introduction. Let’s get started! › Typical programming contest problems usually consists of: – Problem description – Input description

› You get more information

› You will (usually) receive one of:– Percent of the test case completed

› With detail listing of successful test case and unsuccessful ones.› However, the test case will not be revealed.

– Compile Error– Run Time Error– Time Limit Exceeded– Memory Limit Exceeded

Judge verdicts (IOI CMS)

9/23/2019 INTRODUCTION TO COMPETITIVE PROGRAMMING 35

Page 36: Introduction to Competitive Programming...Introduction. Let’s get started! › Typical programming contest problems usually consists of: – Problem description – Input description

› There are a couple of tips and guidelines you can keep in mind towards becoming a more effective programmer and better problem solver.

Tips

9/23/2019 INTRODUCTION TO COMPETITIVE PROGRAMMING 36

Page 37: Introduction to Competitive Programming...Introduction. Let’s get started! › Typical programming contest problems usually consists of: – Problem description – Input description

› Become a faster/better typist.

› Know your IDE (in ICPC competition, Eclipse!)

› Don’t let your fingers be the limiting factor of solving problems quickly.

› Good problem solvers have simple solutions; they don’t have to type as much, but it’s still important to type in quickly

› TypeRacer is a fun and effective way to practice:

http://play.typeracer.com/

Tip 0: Faster typing

9/23/2019 INTRODUCTION TO COMPETITIVE PROGRAMMING 37

Page 38: Introduction to Competitive Programming...Introduction. Let’s get started! › Typical programming contest problems usually consists of: – Problem description – Input description

› Sometimes in competition, you may bring in a limited set of notes which contain code that you can type from the paper!

Tip 0: Faster Typing

9/23/2019 INTRODUCTION TO COMPETITIVE PROGRAMMING 38

Page 39: Introduction to Competitive Programming...Introduction. Let’s get started! › Typical programming contest problems usually consists of: – Problem description – Input description

› Practice quickly identifying problem types.

› Rate of appearance of different problem types in recent ICPC Asia Regional problem sets (which usually consists of 7-11 problems):

Tip 1: Quickly classify problems

9/23/2019 INTRODUCTION TO COMPETITIVE PROGRAMMING 39

Page 40: Introduction to Competitive Programming...Introduction. Let’s get started! › Typical programming contest problems usually consists of: – Problem description – Input description

Category Sub-Category FrequencyAd Hoc Straightforward 1~2Ad Hoc Simulation 0~1Complete Search Iterative 0~1Complete Search Backtracking 0~1Divide & Conquer 0~1Greedy Classic 0Greedy Original 1Dynamic Programming Classic 0Dynamic Programming Original 1~3Graph 1~2Mathematics 1~2String Processing 1Computational Geometry 1Harder Problems 0~1

9/23/2019 INTRODUCTION TO COMPETITIVE PROGRAMMING 40

Page 41: Introduction to Competitive Programming...Introduction. Let’s get started! › Typical programming contest problems usually consists of: – Problem description – Input description

› Moreover, identify whether or not you can solve the problem type:– Solved before / can solve again quickly.– Solved before / will take time to solve again.– Seen before / will solve this if all easier ones are solved.– Not sure! (驚~)

Tip 1: Quickly classify problems

9/23/2019 INTRODUCTION TO COMPETITIVE PROGRAMMING 41

Page 42: Introduction to Competitive Programming...Introduction. Let’s get started! › Typical programming contest problems usually consists of: – Problem description – Input description

Exercise: What kind of problem is this?

Given an 𝑀𝑀 × 𝑁𝑁 integer matrix 𝑄𝑄, check if there exists a sub‐matrix of 𝑄𝑄 of size 𝐴𝐴 × 𝐵𝐵 where mean(𝑄𝑄) = 7?

1 ≤ 𝑀𝑀,𝑁𝑁 ≤ 501 ≤ 𝐴𝐴 ≤ 𝑀𝑀1 ≤ 𝐵𝐵 ≤ 𝑁𝑁

Tip 1: Quickly classify problems

9/23/2019 INTRODUCTION TO COMPETITIVE PROGRAMMING 42

Page 43: Introduction to Competitive Programming...Introduction. Let’s get started! › Typical programming contest problems usually consists of: – Problem description – Input description

› When solving a problem, our solution has to be fast enough and can not use too much memory.

› We also want our solution to be as simple as possible.› We can use Algorithm analysis to determine if a solution

will run within the time limit.– Rule of thumb: 109 operations per second.

› We want to sort 𝑛𝑛 ≤ 106 integers, and we have 3 seconds.– Can we use a simple O(𝑛𝑛2) bubble sort?– What about a more complex 𝑂𝑂 𝑛𝑛 log𝑛𝑛 merge sort?

› We want to sort 𝑛𝑛 ≤ 103 integers, and we have 3 seconds.– Can we now use the simple 𝑂𝑂(2) bubble sort?

› Always go for the simplest solution that will pass the time limit!

Tip 2: Do algorithm analysis

9/23/2019 INTRODUCTION TO COMPETITIVE PROGRAMMING 43

Page 44: Introduction to Competitive Programming...Introduction. Let’s get started! › Typical programming contest problems usually consists of: – Problem description – Input description

› You should practice doing approximate mental calculations.– Rule of thumb: 210 ≈ 103.

› Sometimes you have a solution that you’re not sure is correct:– Try to prove it’s correct!– Even if you don’t manage to prove or disprove it, you will

probably get a better understanding of the problem!

› After discovering a solution, convince yourself that it runs in time and memory!– Look at the constraints of the problem.– Worst-case analysis before starting to code.

Tip 2: Do algorithm analysis

9/23/2019 INTRODUCTION TO COMPETITIVE PROGRAMMING 44

Page 45: Introduction to Competitive Programming...Introduction. Let’s get started! › Typical programming contest problems usually consists of: – Problem description – Input description

𝒏𝒏 Slowest Accepted Algorithm Example≤ 10 𝑂𝑂 𝑛𝑛! ,𝑂𝑂 𝑛𝑛6 Enumerating a permutation≤ 15 𝑂𝑂(2𝑛𝑛 × 𝑛𝑛2) DP TSP≤ 20 𝑂𝑂 2𝑛𝑛 ,𝑂𝑂 𝑛𝑛5 DP + bitmask technique≤ 50 𝑂𝑂(𝑛𝑛4) DP with 3 dimensions + 𝑂𝑂(𝑛𝑛)

loop, choosing 𝐶𝐶𝑘𝑘𝑛𝑛 = 4.≤ 102 𝑂𝑂(𝑛𝑛3) Floyd Warshall’s≤ 103 𝑂𝑂(𝑛𝑛2) Bubble/Selection/Insertion sort≤ 105 𝑂𝑂(𝑛𝑛 log𝑛𝑛) Merge sort, Quick sort, building

a segment tree≤ 106 𝑂𝑂 𝑛𝑛 ,𝑂𝑂(log𝑛𝑛),𝑂𝑂(1) Contest problem input have 𝑛𝑛 ≤

106 size.

Tip 2: Do algorithm analysis

9/23/2019 INTRODUCTION TO COMPETITIVE PROGRAMMING 45

Page 46: Introduction to Competitive Programming...Introduction. Let’s get started! › Typical programming contest problems usually consists of: – Problem description – Input description

› You should know your programming language like the back of your hand.

› After thinking of a solution, convey the solution in code as quickly as possible.

› Use libraries, shortcuts, and write simple code.

› This includes your programming language’s library:– C++’s Standard Template Library (STL)– The Java Class Library

› If it’s already implemented in the standard library, you usually don’t need to implement it yourself!

Tip 3: Master programming languages

9/23/2019 INTRODUCTION TO COMPETITIVE PROGRAMMING 46

Page 47: Introduction to Competitive Programming...Introduction. Let’s get started! › Typical programming contest problems usually consists of: – Problem description – Input description

› Java:– Scanner, BigInteger, String static functions,– Collections, diferent data types– Integer.parseInt()– String.substring()– etc

› C++– next_permutation(), sort(), vector<T>, …

› C– qsort(), malloc(), …

Tip 3: Master programming languages

9/23/2019 INTRODUCTION TO COMPETITIVE PROGRAMMING 47

Page 48: Introduction to Competitive Programming...Introduction. Let’s get started! › Typical programming contest problems usually consists of: – Problem description – Input description

› You want to make sure your solution is correct and

› runs within the time limit.

› Or you already know it’s wrong, but don’t know why…– Try to break your solution by finding a counterexample.– (an input for which your solution gives incorrect output, or

takes too long to compute an answer)

› Try edge cases, large inputs, ...

› Submit correctly!– Competitions only care about correct code.– Is it worth the 20 minute penalty to submit without test cases?– The best teams write test cases before submitting their solutions!

Tip 4: Test your solution

9/23/2019 INTRODUCTION TO COMPETITIVE PROGRAMMING 48

Page 49: Introduction to Competitive Programming...Introduction. Let’s get started! › Typical programming contest problems usually consists of: – Problem description – Input description

› Problem solving and programming skills come with practice.

› Lots of online judges that let you solve problems from past contests.

› Some of these online judges also hold contests frequently!

› UVa, Codeforces, TopCoder, ZeroJudge, PTC, CPE, ...

Tip 5: Practice, more practice, and more practice

9/23/2019 INTRODUCTION TO COMPETITIVE PROGRAMMING 49

Page 50: Introduction to Competitive Programming...Introduction. Let’s get started! › Typical programming contest problems usually consists of: – Problem description – Input description

› IOI is 1-on-1, but ICPC is 3-on-3.– Knowing your teammates.

› Delegating problems to each other.– Sharing the computer time effectively.– Creating test cases for each other.– Being able to convey ideas.– Pair programming.

Tip 6: Teamwork

9/23/2019 INTRODUCTION TO COMPETITIVE PROGRAMMING 50

Page 51: Introduction to Competitive Programming...Introduction. Let’s get started! › Typical programming contest problems usually consists of: – Problem description – Input description

Ad Hoc problemsIQ testing problems

Page 52: Introduction to Competitive Programming...Introduction. Let’s get started! › Typical programming contest problems usually consists of: – Problem description – Input description

› The simplest kind of problem.

› Just do what the problem description tells you.

› Straightforward or a simulation.

› Time limit is not an issue.

› Sometimes long and misleading problem descriptions.

› Sometimes tricky edge cases.

› Complex problems can be hard to implement.

Ad Hoc problems

9/23/2019 INTRODUCTION TO COMPETITIVE PROGRAMMING 52

Page 53: Introduction to Competitive Programming...Introduction. Let’s get started! › Typical programming contest problems usually consists of: – Problem description – Input description

› Problem narrative.– Can be unnecessarily long or misleading…..

› Input and output description.– Usually very precise.– You may assume that all input will be formatted like this.

› Sample input and output.– One or more inputs and expected outputs.

Problem recipe

9/23/2019 INTRODUCTION TO COMPETITIVE PROGRAMMING 53

Page 54: Introduction to Competitive Programming...Introduction. Let’s get started! › Typical programming contest problems usually consists of: – Problem description – Input description

Last month Alice nonchalantly entered her name in a draw for aTapmaster 4000. Upon checking her mail today, she found aletter that read:

“Congratulations, Alice! You have won a Tapmaster4000. To claim your prize, you must answer the following skilltesting question.”Alice's initial feelings of surprised joy turned quickly to thoseof dismay. Her lifetime record for skill testing questions is anabysmal 3 right and 42 wrong.Mad Skills, the leading skill testing question developmentcompany, was hired to provide skill testing questions for thisparticular Tapmaster 4000 draw. They decided to create adifferent skill testing question to each winner so that thewinners could not collaborate to answer the question.Can you help Alice win the Tapmaster 4000 by solving the skilltesting question?

Problem: Automatic Answer (uva 11547)

9/23/2019 INTRODUCTION TO COMPETITIVE PROGRAMMING 54

Page 55: Introduction to Competitive Programming...Introduction. Let’s get started! › Typical programming contest problems usually consists of: – Problem description – Input description

Input

The input begins with 𝑡𝑡 (1 ≤ 𝑡𝑡 ≤ 100), the number oftest cases. Each test case contains an integer 𝑛𝑛 (−1000 ≤𝑛𝑛 ≤ 1000) on a line by itself. This 𝑛𝑛 should besubstituted into the skill testing question below.

Output

For each test case, output the answer to the following skilltesting question on a line by itself:

“Multiply 𝑛𝑛 by 567, then divide the result by 9, then add7492, then multiply by 235, then divide by 47, thensubtract 498. What is the digit in the tens column?”

Problem: Automatic Answer (uva 11547)

9/23/2019 INTRODUCTION TO COMPETITIVE PROGRAMMING 55

Page 56: Introduction to Competitive Programming...Introduction. Let’s get started! › Typical programming contest problems usually consists of: – Problem description – Input description

Sample input Sample output2 1637 3-120

Problem: Automatic Answer (uva 11547)

9/23/2019 INTRODUCTION TO COMPETITIVE PROGRAMMING 56

Page 57: Introduction to Competitive Programming...Introduction. Let’s get started! › Typical programming contest problems usually consists of: – Problem description – Input description

› Most problems will have numerous test cases.

› Different problems ask for different ways of parsing test cases:– Automatic Answer tells you how many test cases there are.– Some problems say “parse until a termination line of all zeros”.

› Others will have you read until end of file <EOF>.

Parsing test cases

9/23/2019 INTRODUCTION TO COMPETITIVE PROGRAMMING 57

Page 58: Introduction to Competitive Programming...Introduction. Let’s get started! › Typical programming contest problems usually consists of: – Problem description – Input description

Company XYZ have been badly hit by recession and istaking a lot of cost cutting measures. Some of thesemeasures include giving up office space, going opensource, reducing incentives, cutting on luxuries andissuing pink slips.

They have got three (3) employees working in theaccounts department and are going to lay-off two (2) ofthem. After a series of meetings, they have decided todislodge the person who gets the most salary and the onewho gets the least. This is usually the general trend duringcrisis like this. You will be given the salaries of these 3employees working in the accounts department. You haveto find out the salary of the person who survives.

Problem: Cost Cutting

9/23/2019 INTRODUCTION TO COMPETITIVE PROGRAMMING 58

Page 59: Introduction to Competitive Programming...Introduction. Let’s get started! › Typical programming contest problems usually consists of: – Problem description – Input description

Input

The first line of input is an integer 𝑇𝑇 (𝑇𝑇 < 20) that indicates the number of test cases. Each case consists of a line with 3 distinct positive integers. These 3 integers represent the salaries of the three employees. All these integers will be in the range [1000,10000].

Output

For each case, output the case number followed by the salary of the person who survives.

Problem: Cost Cutting

9/23/2019 INTRODUCTION TO COMPETITIVE PROGRAMMING 59

Page 60: Introduction to Competitive Programming...Introduction. Let’s get started! › Typical programming contest problems usually consists of: – Problem description – Input description

Sample input Sample output3 Case 1: 20001000 2000 3000 Case 2: 25003000 2500 1500 Case 3: 15001500 1200 1800

Problem: Cost Cutting

9/23/2019 INTRODUCTION TO COMPETITIVE PROGRAMMING 60

Page 61: Introduction to Competitive Programming...Introduction. Let’s get started! › Typical programming contest problems usually consists of: – Problem description – Input description

Cell phones have become an essential part of modern life.In addition to making voice calls, cell phones can be usedto send text messages, which are known as SMS for short.Unlike computer keyboards, most cell phones havelimited number of keys. To accommodate all alphabets,letters are compacted into single key. Therefore, to typecertain characters, a key must be repeatedly pressed untilthat character is shown on the display panel.

In this problem we are interested in finding out thenumber of times keys on a cell phone must be pressed totype a particular message.

Problem: SMS Typing

9/23/2019 INTRODUCTION TO COMPETITIVE PROGRAMMING 61

Page 62: Introduction to Competitive Programming...Introduction. Let’s get started! › Typical programming contest problems usually consists of: – Problem description – Input description

In this problem we will assume that the key pad of our cellphone is arranged as follows.

In the above grid each cell represents one key. Here <SP>means a space. In order to type the letter ‘a’, we mustpress that key once, however to type ‘b’ the same keymust be repeatedly pressed twice and for ‘c’ three times.In the same manner, one key press for ‘d’, two for ‘e’ andthree for ‘f’. This is also applicable for the remaining keysand letters. Note that it takes a single press to type a space.

Problem: SMS Typing

9/23/2019 INTRODUCTION TO COMPETITIVE PROGRAMMING 62

abc defghi jkl mnopqrs tuv wxyz

<SP>

Page 63: Introduction to Competitive Programming...Introduction. Let’s get started! › Typical programming contest problems usually consists of: – Problem description – Input description

Input

The first line of input will be a positive integer 𝑇𝑇 where 𝑇𝑇denotes the number of test cases. 𝑇𝑇 lines will then followeach containing only spaces and lower case letters. Eachline will contain at least 1 and at most 100 characters.

Output

For every case of input there will be one line of output. Itwill first contain the case number followed by the numberof key presses required to type the message of that case.Look at the sample output for exact formatting.

Problem: SMS Typing

9/23/2019 INTRODUCTION TO COMPETITIVE PROGRAMMING 63

Page 64: Introduction to Competitive Programming...Introduction. Let’s get started! › Typical programming contest problems usually consists of: – Problem description – Input description

› Form teams of 3.– AT or RT

› Usual rule:– 1 PC per team.– Printing available.– Can discuss problems with teammates (keep volume low).– Balloons!

First Simulation

9/23/2019 INTRODUCTION TO COMPETITIVE PROGRAMMING 64