Presentation is loading. Please wait.

Presentation is loading. Please wait.

Atlantis HKOI2005 Final Event (Senior Group). 2 Background 9000 B.C. 9000 B.C. Atlantis Zeus Other gods Destroy!!

Similar presentations


Presentation on theme: "Atlantis HKOI2005 Final Event (Senior Group). 2 Background 9000 B.C. 9000 B.C. Atlantis Zeus Other gods Destroy!!"— Presentation transcript:

1 Atlantis HKOI2005 Final Event (Senior Group)

2 2 Background 9000 B.C. 9000 B.C. Atlantis Zeus Other gods Destroy!!

3 3 Problem There are N pairs of (island + controller) There are N pairs of (island + controller) In each pair, the island is K meters above the controller for some fixed positive integer K In each pair, the island is K meters above the controller for some fixed positive integer K Given the heights of the 2N objects, which of them are controllers? What is K? Given the heights of the 2N objects, which of them are controllers? What is K?

4 4 Model Two integer sequences Two integer sequences A = (a 1, a 2, …, a N ), B = (b 1, b 2, …, b N ) are parallel if for some positive integer K, a 1 = b 1 +K, a 2 = b 2 +K, …, a N = b N +K Given a collection C of 2N positive integers, partition it into 2 parallel sequences of size N, if possible Given a collection C of 2N positive integers, partition it into 2 parallel sequences of size N, if possible

5 5 Smaller problems How to determine if two sequences are parallel? How to determine if two sequences are parallel? Trivial – O(N) Trivial – O(N) Given two collections of size N, is it possible to form a sequence of size N from each of the sets such that the two sequences are parallel? Given two collections of size N, is it possible to form a sequence of size N from each of the sets such that the two sequences are parallel? Sorting – O(NlgN) (O(N) for count sort) Sorting – O(NlgN) (O(N) for count sort) 1 35 8 9 73 5 1 3 5 8 3 5 7 10 +2

6 6 Algorithm 1 Idea Idea Partition C into two collections of size N Partition C into two collections of size N Determine if two parallel sequences can be formed from the two collections (previous slide) Determine if two parallel sequences can be formed from the two collections (previous slide) 2N C N ways to partition C 2N C N ways to partition C Exponential Exponential Expected score: 50% Expected score: 50%

7 7 Observations Out of the 2N C N different partitions, most of them should not be tested, for example Out of the 2N C N different partitions, most of them should not be tested, for example {1, 2, …, 2N}  {1, 4, ??, …, ??}, {2, 3, ??, …, ??} {1, 2, …, 2N}  {1, 4, ??, …, ??}, {2, 3, ??, …, ??} Why? Why? {1, 2, …, 2N}  {1, 2, 4, ??, …, ??}, {3, ??, …, ??} {1, 2, …, 2N}  {1, 2, 4, ??, …, ??}, {3, ??, …, ??} The smallest integer from one collection and the smallest integer from another collection form a pair The smallest integer from one collection and the smallest integer from another collection form a pair

8 8 Almost there… Suppose we know the smallest pair, can we reconstruct the partition? Suppose we know the smallest pair, can we reconstruct the partition? Fact: The smallest pair must contain the smallest integer in the C Fact: The smallest pair must contain the smallest integer in the C

9 9 6+2 = 87+2 = 9 67 An Example Given C = {2, 6, 1, 3, 7, 4, 8, 9} and suppose we are told that (1, 3) is the smallest pair Given C = {2, 6, 1, 3, 7, 4, 8, 9} and suppose we are told that (1, 3) is the smallest pair 2 6 1 3 7 4 8 9 (1, 3) Smallest remaining integer = Its partner = 2 2+2 = 4 (2, 4) (6, 8) (7, 9)

10 10 Algorithm 2 Idea Idea Suppose (a, b) is the smallest pair; let d = b – a Suppose (a, b) is the smallest pair; let d = b – a Repeat N times Repeat N times Let s be the smallest integer in C Let s be the smallest integer in C If s+d is not in C, return failure If s+d is not in C, return failure Otherwise (s, s+d) is a pair, remove s and s+d from C Otherwise (s, s+d) is a pair, remove s and s+d from C For how many times do we need to carry out the above steps? For how many times do we need to carry out the above steps?

11 11 Algorithm 2 - Analysis How many different “smallest pairs” should be tried? How many different “smallest pairs” should be tried? N (smallest, 2 nd smallest), (smallest, 3 rd smallest), …, (smallest, (n+1) th smallest) (smallest, 2 nd smallest), (smallest, 3 rd smallest), …, (smallest, (n+1) th smallest)

12 12 Algorithm 2 – Analysis (2) The loop takes O(N 2 ) time The loop takes O(N 2 ) time Overall time complexity is O(N 3 ) Overall time complexity is O(N 3 ) Too slow when N = 1000 Too slow when N = 1000 Observation: The pairs we get are increasing Observation: The pairs we get are increasing Both coordinates are increasing Both coordinates are increasing (1, 3) ≤ (2, 4) ≤ (6, 8) ≤ (7, 9) (1, 3) ≤ (2, 4) ≤ (6, 8) ≤ (7, 9)

13 13 2+2 = 4 2 6+2 = 8 6 7+2 = 9 7 Algorithm 2A Sort the integers in C Sort the integers in C Keep two pointers (integers) Keep two pointers (integers) 1 2 3 4 6 7 8 9 (1, 3) Smallest remaining integer = Its partner = (2, 4) (6, 8) (7, 9) AB

14 14 Algorithm 2A The loop takes O(N) time The loop takes O(N) time Overall time complexity is O(N 2 ) Overall time complexity is O(N 2 ) Not a problem even when N = 10000 Not a problem even when N = 10000

15 15 Algorithm 3 (Out of Syllabus) For trainees with graph theory background For trainees with graph theory background Fix a height difference, construct a graph, then find a maximum bipartite matching Fix a height difference, construct a graph, then find a maximum bipartite matching Example: Height difference = 2 Example: Height difference = 2 Perfect matching Perfect matching  Feasible Overall time complexity: Overall time complexity: O(N 3.5 ) or O(N 3 ) 1 2 4 3 6 7 8 9

16 16 Extensions and Mutations Constraints: Constraints: Real numbers instead of integers Real numbers instead of integers Height sums instead of height differences Height sums instead of height differences Height differences can lie in a range instead of a fixed number Height differences can lie in a range instead of a fixed number Objectives: Objectives: Minimize sum of height differences Minimize sum of height differences Minimize maximum height difference Minimize maximum height difference


Download ppt "Atlantis HKOI2005 Final Event (Senior Group). 2 Background 9000 B.C. 9000 B.C. Atlantis Zeus Other gods Destroy!!"

Similar presentations


Ads by Google