ACM programming contest Introduction + Recursion.

Slides:



Advertisements
Similar presentations
CSED101 INTRODUCTION TO COMPUTING GREEDY APPROACH, DIVIDE AND CONQUER Hwanjo Yu.
Advertisements

Algorithms (and Datastructures) Lecture 3 MAS 714 part 2 Hartmut Klauck.
ACM Programming Team Bill Punch. ACM Programming Contest One of the premier programming competitions. Held every year since 1977 MSU was the winner that.
Jotto! JRsSRsElderly slate 3 This term's first class to guess another's word earns 1 problem... slate 2slate 1 This term's last class to have its word.
More Recursion: Permutations and Towers of Hanoi COP 3502.
Contest format 5 hours, around 8-12 problems One computer running (likely)Linux, plus printer 3 people on one machine No cell phones, calculators, USB.
CMSC 150 RECURSION CS 150: Mon 26 Mar Motivation : Bioinformatics Example  A G A C T A G T T A C  C G A G A C G T  Want to compare sequences.
Recursion Lecture 18: Nov 18.
Recursion. Idea: Some problems can be broken down into smaller versions of the same problem Example: n! 1*2*3*…*(n-1)*n n*factorial of (n-1)
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
1 10/20/08CS150 Introduction to Computer Science 1 do/while and Nested Loops Section 5.5 & 5.11.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 14: Recursion by.
CS 206 Introduction to Computer Science II 10 / 14 / 2009 Instructor: Michael Eckmann.
Recursion Gordon College CPS212
1 10/9/06CS150 Introduction to Computer Science 1 for Loops.
Recursion!. Question Stephen King’s books J. R. R. Tolkien’s books Recursion Do they have something in common?
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 16: Recursion.
Lecture 33 CSE 331 Nov 17, Online office hours Alex will host the office hours.
Introduction to Programming (in C++) Recursion Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC.
ACM Programming Contests
How to win in ACM/ICPC? Four levels of programmers 1. Implementation ◦ know the language well, translate idea to programs 2. Algorithms ◦
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 19: Recursion.
ACM Programming Contests Coordinator: Dr. Hubert Chan Coach: Luyi Mo Training Holder: Jonathan So Website: i.cs.hku.hk/~provinci.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Recursion Chapter Nature of Recursion t Problems that lend themselves to a recursive solution have the following characteristics: –One or more.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12: Recursion Problem Solving, Abstraction, and Design using C++
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Solving Recurrence Lecture 19: Nov 25. Some Recursive Programming (Optional?)
Recursion Textbook chapter Recursive Function Call a recursive call is a function call in which the called function is the same as the one making.
Recursion Jordi Cortadella Department of Computer Science.
Recursion. Circular Definition Circular definition Circular definition Dialectic materialism is materialism that is dialectic. Dialectic materialism is.
Recursion (part 1) HKUCS Provinci Training Chan Ho Leung.
Recursion. Hanoi Tower Legend: Inside a Vietnamese temple there are three rods (labeled as r 1, r 2, and r 3 ), surrounded by n golden disks of different.
Data Structures & Algorithms Recursion and Trees Richard Newman.
ACM programming contest Introduction. ACM International Collegiate Programming Contest (a.k.a. ACM ICPC, ACM competition, ICPC) is the largest university-level.
Java Programming: Guided Learning with Early Objects Chapter 11 Recursion.
Main Index Contents 11 Main Index Contents Building a Ruler: drawRuler() Building a Ruler: drawRuler() Merge Algorithm Example (4 slides) Merge Algorithm.
Lecture 5 Jianjun Hu Department of Computer Science and Engineering University of South Carolina CSCE350 Algorithms and Data Structure.
Provinci summer training 2010 June 17: Recursion and recursive decent parser June 24: Greedy algorithms and stable marriage July 1: Holiday July 8: Math.
Provinci summer training 2010 June 17: Recursion and recursive decent parser June 24: Greedy algorithms and stable marriage July 1: Holiday July 8: Math.
Welcoming Lecture Meeting/Discussion/Party By David/Denon.
22C:19 Discrete Math Advanced Counting Fall 2010 Sukumar Ghosh.
11/10/2016CS150 Introduction to Computer Science 1 Last Time  We covered “for” loops.
Recursion. Circular Definition (not useful) E.g., E.g., Projenitor: one who produces an offspring Projenitor: one who produces an offspring Offspring:
1 Chapter 8 Recursion. 2 Objectives  To know what is a recursive function and the benefits of using recursive functions (§8.1).  To determine the base.
Introduction to HKOI 13 January Benefits of Joining HKOI  Improve your problem solving skills  Prizes (you already have some)  Chances to represent.
1 Data Structures CSCI 132, Spring 2016 Notes 16 Tail Recursion.
Chapter 15 Recursion.
COP 3503 FALL 2012 Shayan Javed Lecture 15
Recursion Chapter 12.
Chapter 15 Recursion.
* 07/16/96 IOI / ACMPC Training 2005 *.
Introduction to Computer Science - Alice
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
Unit 18: Computational Thinking
Analysis and design of algorithm
Announcements Final Exam on August 17th Wednesday at 16:00.
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
Jordi Cortadella Department of Computer Science
Data Structures & Algorithms
Chapter 14: Recursion Starting Out with C++ Early Objects
Recursion.
CS150 Introduction to Computer Science 1
CSE 1342 Programming Concepts
When Will The World End “Temple of Brahma” legend from the Indian city of Benares Priests must move 64 gold disks from one end of the temple to the other.
The Hanoi Tower Problem
CS150 Introduction to Computer Science 1
Chapter 19: Recursion.
Recursive Algorithms 1 Building a Ruler: drawRuler()
CS150 Introduction to Computer Science 1
Presentation transcript:

ACM programming contest Introduction + Recursion

ACM International Collegiate Programming Contest (a.k.a. ACM ICPC, ACM competition, ICPC) is the largest university-level programming contest in the world. ACM - Association of Computing Machinery ◦ Largest association for computer science ◦ Organize research conference ◦ Publish journal, etc Background

Contest rule Each team: 3 students One computer per team 4- 6 hours problems (like the sudoku question) ◦ Input and output requirement ◦ Time limit Judge by some test cases (unknown to us) ◦ Pass all test cases = solve the question ◦ If you fail, you can resubmit Score ≈ "# of solved question" and "time used" Winner = most question solved, if draw, the one uses least time

Tentative schedule Topics Feb - April Basic topics: recursion, greedy, dynamic programming, graphs, breadth first search, depth first search MayTeam formation test for local contest June / JulyACM Hong Kong Local Contest July - August Intermediate topics: matching, coordinate geometry, math AugustTeam formation test for regional contest Sep - OctAdvanced topics: Contest like problems Oct / NovACM Asia Regional Contest MarchACM World Final Contest (by invitation)

Training tools Practice makes perfect - Online problem archive Ability ≈ number of problems you have solved Our first question: POJ 2719 Faulty Odometer Notice: input is between 1 to 999,999,999; 0 means the end Time limit: 1 second Every digit has only 9 possibilities; It is a base-9 number [0-3] -> [0-3] && [5-9] -> [4-8] See your ranking at

Recursion

Hanoi Tower Legend: Inside a Vietnamese temple there are three rods (labeled as r 1, r 2, and r 3 ), surrounded by n golden disks of different sizes. The monks of Hanoi wants to move these disks from r 1 to r 3 such that: at each step, take the upper disk of one rod and place it to the top of another rod no bigger disk can be placed on top of a smaller disk

Input and output format Input: A single integer n in [1, 20] Output: The steps to move the disks from r 1 to r 3 ; in the format of r i -> r j 3 r1 -> r3 r1 -> r2 r3 -> r1 r1 -> r3 r2 -> r1 r2 -> r3 r1 -> r3

Recursion Can we solve the problem by solving some smaller problems? To move n disks from r 1 to r 3 1.move n-1 disks from r 1 to r 2 2.move 1 disks from r 1 to r 3 3.move n-1 disks from r 2 to r 3 r1r1 r2r2 r3r3

Implementation Let hanoi( int n, int i, int j, int z ) be a function that will move n disks from rod i to rod j using rod k as the middle rod. void hanoi( int n, int i, int j, int k ){ if( n==1 ){ cout r" << j << endl; return; } hanoi( n-1, i, k, j ); hanoi( 1, i, j, k ); hanoi( n-1, k, j, i ); } int main(){ int n; cin >> n; hanoi( n ); return 0; }

How many moves are there? Let T(n) be the number of steps to move n disks T(1) = 1 T(n)= 2 T(n-1) + 1 = 4 T(n-2) =... = 2 n-1 T(1)+ 2 n = 2 n - 1 The running time is also Θ(2 n ) = exponential time Very slow But it is optimal already (prove by M.I. on n)

POJ 3601

Other recursion problems Look for easy problems first POJ 1920

POJ 2386 Lake counting

POJ 2227

Topological sort

POJ 1128

POJ 1094

Summary We have seen some examples on recursion. ◦ Hanoi tower ◦ Reverse of hanoi tower Flood fill is a recursive method to identify connected region. ◦ Running time = size of the region We can order a partial ordered set by topological sort ◦ Running time = number of edges Exercises: UVA 900, 254, 699, 10940, 527, 572, 657, 11110, 10305, 11686, 200