OI-style programming Gary Wong For any questions, please ask via Email: [email protected] MSN:...
-
Upload
douglas-bates -
Category
Documents
-
view
220 -
download
0
Transcript of OI-style programming Gary Wong For any questions, please ask via Email: [email protected] MSN:...
OI-style programming
Gary Wong
For any questions, please ask viaEmail: [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• Algorithms and data structures• Concept of “complexity”
OI-oriented skills
• Use one word to describe the aim of a contestant in a competition.
WIN!
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…
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!
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
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
OI-oriented skills
• Choosing a problem– Problem setters might not expect
contestants to finish the whole paper– Usually from easy to difficult
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
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.
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?
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
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!
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)
OI-oriented skills
• Testing– If time allows, cross check your “efficient”
program with a “slower” program
OI-oriented skills
• Debugging– Easiest method: writeln/printf/cout
• It is so-called “Debug message”– Use of debuggers:
• FreePascal IDE debugger• gdb debugger
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
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
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
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
Contents
• OI-oriented skills• Algorithms and data structures• Concept of “complexity”
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”
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
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
Contents
• OI-oriented skills• Algorithms and data structures• Concept of “complexity”
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
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!
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
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)
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)
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)
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!
Any question?
Let’s know each other!