oi-style programming

35
OI-style programming Gary Wong For any questions, please ask via Email: [email protected] MSN: [email protected]

Upload: sonel

Post on 16-Jan-2016

40 views

Category:

Documents


0 download

DESCRIPTION

OI-style programming. Gary Wong For any questions, please ask via Email: [email protected] MSN: [email protected]. 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 - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: OI-style programming

OI-style programming

Gary Wong

For any questions, please ask viaEmail: [email protected]

MSN: [email protected]

Page 2: OI-style programming

Before the training…

• I never assume you knowing a lot• It is okay to interrupt me whenever

you think of any question to ask

Page 3: OI-style programming

Contents

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

Page 4: OI-style programming

OI-oriented skills

• Use one word to describe the aim of a contestant in a competition.

WIN!

Page 5: OI-style programming

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…

Page 6: OI-style programming

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!

Page 7: OI-style programming

OI-oriented skills

• Recommended steps for solving problems in OI:1. Reading the problems2. Choosing a problem3. Reading the problem4. Thinking5. Coding6. Testing (and debugging)7. Finalizing the program

Page 8: OI-style programming

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

Page 9: OI-style programming

OI-oriented skills

• Choosing a problem– Problem setters might not expect

contestants to finish the whole paper– Usually from easy to difficult

Page 10: OI-style programming

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

Page 11: OI-style programming

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 tim

e for other problems.

Page 12: OI-style programming

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?

Page 13: OI-style programming

OI-oriented skills

• Coding– Short variable names

• Use i, j, m, n instead of no_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

Page 14: OI-style programming

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!

Page 15: OI-style programming

OI-oriented skills

• Testing– Sample Input/Output

“A problem has sample output for two reasons:1.To make you understand what the correct outpu

t format is2.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)

Page 16: OI-style programming

OI-oriented skills

• Testing– If time allows, cross check your “efficient”

program with a “slower” program

Page 17: OI-style programming

OI-oriented skills

• Debugging– Easiest method: writeln/printf/cout

• It is so-called “Debug message”– Use of debuggers:

• FreePascal IDE debugger• gdb debugger

Page 18: OI-style programming

OI-oriented skills

• Finalizing– Make sure that the output format is EXACTL

Y the same as in the problem– Remember to delete all “debug messages

”– Is your submitted code the most updated ve

rsion?– Try to allocate ~5 mins at the end for finalizi

ng

Page 19: OI-style programming

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 hi

gher scores than fast but wrong solutions

Page 20: OI-style programming

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

Page 21: OI-style programming

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

Page 22: OI-style programming

Contents

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

Page 23: OI-style programming

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”

Page 24: OI-style programming

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

Page 25: OI-style programming

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

Page 26: OI-style programming

Contents

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

Page 27: OI-style programming

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

Page 28: OI-style programming

0

600

1200

1800

2400

3000

0 5 10 15 20 25 30 35 40 45

f(n)=10n f(n)=30n f(n)=30n log n f(n)=n̂ 2 f(n)=n̂ 3 f(n)=2̂ n f(n)=3̂ n f(n)=n!

Page 29: OI-style programming

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

Page 30: OI-style programming

Complexity

• Example: Bubble Sort• For i := 1 to n do

For j := 2 to i doif 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 vari

ables• Space Complexity=32*n +32*3=O(n)+O(1)=O(n)

Page 31: OI-style programming

Complexity

• Another example: Binary search• While a<=b do

m=(a+b)/2If a[m]=key, Then return mIf a[m]<key, Then a=m+1If 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 vari

ables• Space Complexity = O(n)

Page 32: OI-style programming

Complexity

• What if…– An algorithm using bubble sort, followed by b

inary search?– O(f) + O(g) = max(O(f), O(g))– Take the “maximum” one only, ignore the

“smaller” one– Answer: O(n2)

Page 33: OI-style programming

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!

Page 34: OI-style programming

Any question?

Page 35: OI-style programming

Let’s know each other!