1 / 35

OI-style programming

OI-style programming. Gary Wong For any questions, please ask via Email: garywong612@gmail.com MSN: gary_wong612@hotmail.com. Before the training…. I never assume you knowing a lot It is okay to interrupt me whenever you think of any question to ask. Contents. OI-oriented skills

sonel
Download Presentation

OI-style programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. OI-style programming Gary Wong For any questions, please ask via Email: garywong612@gmail.com MSN: gary_wong612@hotmail.com

  2. Before the training… • I never assume you knowing a lot • It is okay to interrupt me whenever you think of any question to ask

  3. Contents • OI-oriented skills • Algorithms and data structures • Concept of “complexity”

  4. OI-oriented skills • Use one word to describe the aim of a contestant in a competition. WIN!

  5. OI-oriented skills • How to win? • Number of tasks attempted? • Using the most elegant solution? • Using the least time to “finish”? • Highest score! • Let us quickly review the scoring method used in OI competitions…

  6. OI-oriented skills • Scoring • A set of test data is fed into your program • Get the score when it outputs correctly • ALWAYS remember: judging systems never score you by reading the codes!

  7. OI-oriented skills • Recommended steps for solving problems in OI: 1. Reading the problems 2. Choosing a problem 3. Reading the problem 4. Thinking 5. Coding 6. Testing (and debugging) 7. Finalizing the program

  8. OI-oriented skills • Reading the problemS • Have a quick look • You should at least look at: • Length of problem statement • Input/Output format • Constraints • Range of variables • Time limit

  9. OI-oriented skills • Choosing a problem • Problem setters might not expect contestants to finish the whole paper • Usually from easy to difficult

  10. OI-oriented skills • Reading the problem • My own advice: read every single word! • Underline keywords if possible • NEVER make assumptions yourself • Ask if you are not sure

  11. OI-oriented skills • Thinking • Classify the problem into certain type(s) • Rough works • Special cases, boundary cases • No idea? Give up first, do it later. Spend time for other problems.

  12. OI-oriented skills • Thinking • Make sure you know what you are doing before coding • Points to note: • Expected running time of your program? • How much memory will be used? • Coding difficulties?

  13. OI-oriented skills • Coding • Short variable names • Usei, j, m, ninstead ofno_of_schools, name_of_students, etc. • No comments needed • As long as YOU understand YOUR code, okay to ignore all “appropriate“ coding practices • My opinion: poor coding style will eventually kill you once in your life

  14. OI-oriented skills • Coding • Edsger Wyber Dijkstra • A famous Dutch computer scientist • One of his great work is “Dijkstra’s algorithm” for finding shortest paths • He hates “spaghetti codes” a lot!

  15. OI-oriented skills • Testing • Sample Input/Output“A problem has sample output for two reasons: • To make you understand what the correct output format is • To make you believe that your incorrect solution has solved the problem correctly ” • Always create your own test data • Manually • Using a program • Test for ALL possible cases (including tricky cases)

  16. OI-oriented skills • Testing • If time allows, cross check your “efficient” program with a “slower” program

  17. OI-oriented skills • Debugging • Easiest method: writeln/printf/cout • It is so-called “Debug message” • Use of debuggers: • FreePascal IDE debugger • gdb debugger

  18. OI-oriented skills • Finalizing • Make sure that the output format is EXACTLY the same as in the problem • Remember to delete all “debug messages” • Is your submitted code the most updated version? • Try to allocate ~5 mins at the end for finalizing

  19. Tricks • Solve for simple cases • 50% (e.g. slower solution, brute force) • Special cases (smallest, largest, etc) • Incorrect greedy algorithmS • Very often, slow and correct solutions get higher scores than fast but wrong solutions

  20. Tricks • Hard Code • “No solution” • Stupid Hardcode: • begin writeln(random(100)); end. • Naïve hardcode: “if input is x, output hc(x)” • More “intelligent” hardcode (sometimes not possible): pre-compute the values, and only save some of them

  21. Common pitfalls • Even experienced contestants might have such problems! • Misunderstanding the problem • Not familiar with competition environment • Output format • Using complex algorithms unnecessarily • Choosing the hardest problem first • Too confident with himself/herself

  22. Contents • OI-oriented skills • Algorithms and data structures • Concept of “complexity”

  23. Algorithms • “Informally, an algorithm is any well-defined computational procedure that takes some value, or set of values, as input and produces some value, or set of values, as output. An algorithm is thus a sequence of computational steps that transform the input into the output.” [CLRS] • N.B.: CLRS = a book called “Introduction to algorithms”

  24. Algorithms • In other words, a series of procedures to solve a problem • Example: • Bubble Sort, Merge Sort, Quick Sort • Dijkstra’s Algorithm, Bellman Ford’s Algorithm • Common misconceptions: • Algorithm = Program • Confusion between “algorithms” and “methods to design algorithms” • E.g. “recursion” is NOT an algorithm

  25. Data structures • Briefly speaking, the way to organize data • Examples: • Binary Search Tree • Hash Table • Segment Tree • Different data structures have different properties • Efficiency • Amount of memory used • Different algorithms use different data structures

  26. Contents • OI-oriented skills • Algorithms and data structures • Concept of “complexity”

  27. Complexity • We want to know how well an algorithm “scales” in terms of amount of data • In BOTH time and space • Only consider the proportionality to number of basic operations performed • A reasonable implementation can pass • Minor improvements usually cannot help

  28. Complexity • Big-O notation • Definition We say that f(x) is in O(g(x)) if and only if there exist numbers x0 and M such that |f(x)| ≤ M |g(x)| for x > x0 • You do not need to know this 

  29. Complexity • Example: Bubble Sort • For i := 1 to n do For j := 2 to i do if a[j] > a[j-1] then swap(a[j], a[j-1]); • Worst case number of swaps = n(n-1)/2 • Time Complexity=O(n(n-1)/2)=O(n2/2–n/2)=O(n2) • Total space needed = size of array + space of variables • Space Complexity=32*n +32*3=O(n)+O(1)=O(n)

  30. Complexity • Another example: Binary search • While a<=b do m=(a+b)/2 If a[m]=key, Then return m If a[m]<key, Then a=m+1 If a[m]>key, Then b=m-1 • In worst case, • number of iterations = lg n [lg means log2] • Time Complexity = O(lg n) • Total space needed = size of array + space of variables • Space Complexity = O(n)

  31. Complexity • What if… • An algorithm using bubble sort, followed by binary search? • O(f) + O(g) = max(O(f), O(g)) • Take the “maximum” one only, ignore the “smaller” one • Answer: O(n2)

  32. Complexity • Points to note: • Speed of algorithm is machine-dependent • Use suitable algorithms to solve problems • E.g., if n=1000 and runtime limit is 1s, would you use: • O(n2)? • O(n!)? • O(n3)? • Constant hidden by Big-O notation • Testing is required!

  33. Any question?

  34. Let’s know each other! 

More Related