introduction to hkoi gary wong. ice breaking and bond forming…

45
Introduction to HKOI Gary Wong

Upload: josephine-adams

Post on 19-Jan-2016

235 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Introduction to HKOI Gary Wong. Ice Breaking and bond forming…

Introduction to HKOI

Gary Wong

Page 2: Introduction to HKOI Gary Wong. Ice Breaking and bond forming…

Ice Breaking

and bond forming…

Page 3: Introduction to HKOI Gary Wong. Ice Breaking and bond forming…

Rules• Level 1Level 1• Form a big circleForm a big circle• The person holding the deck of cards will start the The person holding the deck of cards will start the

game, by introducing himself, and then passes the game, by introducing himself, and then passes the deck of cards to his left.deck of cards to his left.

• In each preceding turn, the person holding the deck In each preceding turn, the person holding the deck of cards will repeat what the previous person has of cards will repeat what the previous person has said, and then introduces himself. After that, he will said, and then introduces himself. After that, he will passes the deck to his left.passes the deck to his left.

• The game ends when the deck of cards return to the The game ends when the deck of cards return to the first person.first person.

Page 4: Introduction to HKOI Gary Wong. Ice Breaking and bond forming…

Rules• Level 2Level 2• Form a big circleForm a big circle• The person holding the deck of cards will start the game, by inThe person holding the deck of cards will start the game, by in

troducing himself and drawing a card from the deck. After thatroducing himself and drawing a card from the deck. After that, he will pass the deck of cards to the kt, he will pass the deck of cards to the kthth person on his left, w person on his left, where k is the number written on the card he draw.here k is the number written on the card he draw.

• In each preceding turn, the person holding the deck of cards In each preceding turn, the person holding the deck of cards will repeat what the previous person has said, and then introdwill repeat what the previous person has said, and then introduces himself. After that, he will draw a card from the deck and uces himself. After that, he will draw a card from the deck and pass the deck of cards to the kpass the deck of cards to the kthth person on his left, where k is t person on his left, where k is the number written on the card he draw.he number written on the card he draw.

• The game ends when the deck runs out of cards.The game ends when the deck runs out of cards.

Page 5: Introduction to HKOI Gary Wong. Ice Breaking and bond forming…

Why OI?

• Get medals?• Love solving problems?• Learn more?• Make friends?• …

• OI could be a thing to give you all these

Page 6: Introduction to HKOI Gary Wong. Ice Breaking and bond forming…

Agenda

• Algorithms, Data StructuresAlgorithms, Data Structures• ComplexityComplexity• OI Style ProgrammingOI Style Programming• Training SessionsTraining Sessions• Upcoming ChallengesUpcoming Challenges

Page 7: Introduction to HKOI Gary Wong. Ice Breaking and bond forming…

Algorithms, Data Structures

the best couple…

Page 8: Introduction to HKOI Gary Wong. Ice Breaking and bond forming…

Algorithms

• ““Informally, an algorithm is any well-defined Informally, an algorithm is any well-defined computational procedure that takes some computational procedure that takes some value, or set of values, as input and produces value, or set of values, as input and produces some value, or set of values, as output. An some value, or set of values, as output. An algorithm is thus a sequence of computational algorithm is thus a sequence of computational steps that transform the input into the steps that transform the input into the output.” [CLRS]output.” [CLRS]

• N.B.: CLRS = a book called “Introduction to N.B.: CLRS = a book called “Introduction to algorithms”algorithms”

Page 9: Introduction to HKOI Gary Wong. Ice Breaking and bond forming…

Algorithms

• In other words, a series of procedures to solve In other words, a series of procedures to solve a problema problem

• Example:Example:– Bubble Sort, Merge Sort, Quick SortBubble Sort, Merge Sort, Quick Sort– Dijkstra’s Algorithm, Bellman Ford’s AlgorithmDijkstra’s Algorithm, Bellman Ford’s Algorithm

• Common misconceptions:Common misconceptions:– Algorithm = ProgramAlgorithm = Program– Confusion between “algorithms” and “methods to Confusion between “algorithms” and “methods to

design algorithms”design algorithms”

Page 10: Introduction to HKOI Gary Wong. Ice Breaking and bond forming…

Data Structures

• Briefly speaking, the way to organize dataBriefly speaking, the way to organize data• Examples:Examples:

– Binary Search TreeBinary Search Tree– Hash TableHash Table– Segment TreeSegment Tree

• Different data structures have different Different data structures have different propertiesproperties

• Different algorithms use different data Different algorithms use different data structuresstructures

Page 11: Introduction to HKOI Gary Wong. Ice Breaking and bond forming…

Don’t worry!

• All the above-mentioned technical jargons will All the above-mentioned technical jargons will be taught later be taught later

• So, come to attend training! So, come to attend training!

Page 12: Introduction to HKOI Gary Wong. Ice Breaking and bond forming…

Complexity

a performance indicator…

Page 13: Introduction to HKOI Gary Wong. Ice Breaking and bond forming…

Complexity

• We want to know how well an algorithm We want to know how well an algorithm “scales” in terms of amount of data“scales” in terms of amount of data– In BOTH time and spaceIn BOTH time and space

• Only consider the proportionality to number Only consider the proportionality to number of basic operations performedof basic operations performed– A reasonable implementation can passA reasonable implementation can pass– Minor improvements usually cannot helpMinor improvements usually cannot help

Page 14: Introduction to HKOI Gary Wong. Ice Breaking and bond forming…

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 15: Introduction to HKOI Gary Wong. Ice Breaking and bond forming…

Complexity

• Big-O notationBig-O notation• DefinitionDefinition

We say thatWe say thatf(x) is in O(g(x))f(x) is in O(g(x))

if and only ifif and only ifthere exist numbers xthere exist numbers x00 and M such that and M such that |f(x)| ≤ M |g(x)| for x > x|f(x)| ≤ M |g(x)| for x > x00

• You do not need to know this You do not need to know this

Page 16: Introduction to HKOI Gary Wong. Ice Breaking and bond forming…

Complexity

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

For j := 2 to i doFor j := 2 to i doif a[j] > a[j-1] then swap(a[j], a[j-if a[j] > a[j-1] then swap(a[j], a[j-

1]);1]);

• Worst case number of swaps = n(n-1)/2Worst case number of swaps = n(n-1)/2• Time Complexity = O(nTime Complexity = O(n22))• Total space needed = size of array + space of variableTotal space needed = size of array + space of variable

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

Page 17: Introduction to HKOI Gary Wong. Ice Breaking and bond forming…

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

m=(a+b)/2m=(a+b)/2

If a[m]=key, Then return mIf a[m]=key, Then return m

If a[m]<key, Then a=m+1If a[m]<key, Then a=m+1

If a[m]>key, Then b=m-1If a[m]>key, Then b=m-1

• Worst case number of iterations = lg n [lg means logWorst case number of iterations = lg n [lg means log22]]• Time Complexity = O(log n)Time Complexity = O(log n)• Total space needed = size of array + space of variablesTotal space needed = size of array + space of variables• Space Complexity = O(n)Space Complexity = O(n)

Page 18: Introduction to HKOI Gary Wong. Ice Breaking and bond forming…

What if…

• An algorithm involving both bubble sort and binary search?

• O(f) + O(g) = max(O(f), O(g))• Take the “maximum” one only, ignore the “sm

aller” one• Answer: O(n2)

Page 19: Introduction to HKOI Gary Wong. Ice Breaking and bond forming…

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 20: Introduction to HKOI Gary Wong. Ice Breaking and bond forming…

OI-Style Programming

from abstract theoryto (dirty) tricks…

Page 21: Introduction to HKOI Gary Wong. Ice Breaking and bond forming…

OI-Style Programming• Objective of Competition…• The winner is determined by:

– Fastest Program?– Amount of time used in coding?– Number of Tasks Solved?– Use of the most difficult algorithm?– Highest Score

• Rule of thumb: ALWAYS aim to get as many scores as you can

Page 22: Introduction to HKOI Gary Wong. Ice Breaking and bond forming…

OI-Style Programming

• Scoring:– A “black box” judging system– Test data is fed into the program– Output is checked for correctness– No source code is manually inspected– How to take advantage (without cheating of

course!) of the system?

Page 23: Introduction to HKOI Gary Wong. Ice Breaking and bond forming…

OI-Style Programming

• Steps for solving problems in OI:1. Reading the problems2. Choosing a problem3. Reading the problem4. Thinking5. Coding6. Testing7. Finalizing the program

Page 24: Introduction to HKOI Gary Wong. Ice Breaking and bond forming…

Reading the problems

• Problems in OI:– Title– Problem Description– Constraints– Input/Output Specification– Sample Input/Output– Scoring

Page 25: Introduction to HKOI Gary Wong. Ice Breaking and bond forming…

Reading the problems

• Constraints– Range of variables– Execution Time

• NEVER make assumptions yourself– Ask whenever you are not sure– (Do not be afraid to ask questions!)

• Read every word carefully• Make sure you understand before going on

Page 26: Introduction to HKOI Gary Wong. Ice Breaking and bond forming…

Thinking

• Classify the problem into certain type(s)• Rough works• Special cases, boundary cases• No idea? Give up first, do it later. Spend time f

or other problems.

Page 27: Introduction to HKOI Gary Wong. Ice Breaking and bond forming…

Thinking

• Make sure you know what you are doing before coding

• Points to note:– Complexity (BOTH time and space)– Coding difficulties

• What is the rule of thumb mentioned?

Page 28: Introduction to HKOI Gary Wong. Ice Breaking and bond forming…

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 ignor

e all “appropriate“ coding practices• NEVER use 16 bit integers (unless memory is limited)

– 16 bit integer may be slower! (PC’s are usually 32-bit, even 64 bit architectures should be somewhat-optimized for 32 bit)

Page 29: Introduction to HKOI Gary Wong. Ice Breaking and bond forming…

Coding

• Use goto, break, etc in the appropriate situations– Never mind what Dijkstra has to say

• Avoid using floating point variables if possible (eg. real, double, etc)

• Do not do small (aka useless) “optimizations” to your code

• Save and compile frequently

Page 30: Introduction to HKOI Gary Wong. Ice Breaking and bond forming…

Testing• Sample Input/Output

“A problem has sample output for two reasons:1. To make you understand what the correct output format is2. To make you believe that your incorrect solution has

solved the problem correctly ”• Manual Test Data• Program-generated Test Data (if time allows)• Boundary Cases (0, 1, other smallest cases)• Large Cases (to check for TLE, overflows, etc)• Tricky Cases• Test by self-written program (again, if time allows)

Page 31: Introduction to HKOI Gary Wong. Ice Breaking and bond forming…

Debugging

• Debugging – find out the bug, and remove it• Easiest method: writeln/printf/cout

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

– FreePascal IDE debugger– gdb debugger

Page 32: Introduction to HKOI Gary Wong. Ice Breaking and bond forming…

Finalizing• Check output format

– Any trailing spaces? Missing end-of-lines? (for printf users, this is quite common)

– better test once more with sample output– Remember to clear those debug messages

• Check I/O – filename? stdio?• Check exe/source file name• Is the executable updated?• Method of submission?• Try to allocate ~5 mins at the end of competition for finalizing

Page 33: Introduction to HKOI Gary Wong. Ice Breaking and bond forming…

OI-Style Programming

• 2nd time to ask: What is the rule of thumb?• Tricks might be needed (Without violating

rules, of course)

Page 34: Introduction to HKOI Gary Wong. Ice Breaking and bond forming…

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 t

han fast but wrong solutions• 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): pr

e-compute the values, and only save some of them

Page 35: Introduction to HKOI Gary Wong. Ice Breaking and bond forming…

Pitfalls

• Misunderstanding the problem• Not familiar with competition environment• Output format• Using complex algorithms unnecessarily• Choosing the hardest problem first

Page 36: Introduction to HKOI Gary Wong. Ice Breaking and bond forming…

Training Sessions

a moment for inspiration…

Page 37: Introduction to HKOI Gary Wong. Ice Breaking and bond forming…

Training Sessions

• Intermediate and Advanced• ALL topics are open to ALL trainees• Tips: Pre-requisites are often needed for

advanced topics

Page 38: Introduction to HKOI Gary Wong. Ice Breaking and bond forming…

Training Sessions

• On Saturday• Room 123, Ho Sin-Hang Engineering Building,

Chinese University of Hong Kong • AM session: 10:00-12:30• Lunch• PM session: 13:30-16:00• http://www.hkoi.org for more details,

including latest training schedule and notes

Page 39: Introduction to HKOI Gary Wong. Ice Breaking and bond forming…

Training Sessions

• A gross overview of topics covered:– Algorithms and Data StructuresAlgorithms and Data Structures– LinuxLinux

• Free, popular and powerfulFree, popular and powerful• Competition environmentCompetition environment

– C++C++• Advantage of Stardard Template Library (STL)Advantage of Stardard Template Library (STL)

Page 40: Introduction to HKOI Gary Wong. Ice Breaking and bond forming…

Upcoming Challenges

go for it!!!

Page 41: Introduction to HKOI Gary Wong. Ice Breaking and bond forming…

Upcoming Challenges

• Asia-Pacific Informatics Olympiad (7 May 2011)

• Team Formation Test / TFT (28 May 2011)• Provided that you can get through TFT,

– International Olympiad in Informatics– National Olympiad in Informatics– ACM Local

Page 42: Introduction to HKOI Gary Wong. Ice Breaking and bond forming…

Upcoming Challenges

• How can I prepare for these challenges?– Attend trainings– Participate into mini-competitions– Search for learning materials in Internet– Read books– Practice, practice, practice

• PERFECT practice makes perfect– HKOI Online Judge: http://judge.hkoi.org– Other online judges (UVa, POJ, etc.)

Page 43: Introduction to HKOI Gary Wong. Ice Breaking and bond forming…

Hard sell…

• Intermediate Topic: “Searching and Sorting” (10:00-12:30, 22 Jan 2011) by Gary Wong

Page 44: Introduction to HKOI Gary Wong. Ice Breaking and bond forming…

Thank you

for your tolerance =P

Page 45: Introduction to HKOI Gary Wong. Ice Breaking and bond forming…

Reference

• PowerPoint for HKOI 2010 Training Session 1– “Introduction to HKOI”– “Algorithms, OI Style Programming”