160 likes | 355 Views
Atlantis. HKOI2005 Final Event (Senior Group). Background. 9000 B.C. Other gods. Destroy!!. Atlantis. Zeus. Problem. There are N pairs of (island + controller) In each pair, the island is K meters above the controller for some fixed positive integer K
E N D
Atlantis HKOI2005 Final Event (Senior Group)
Background • 9000 B.C. Other gods Destroy!! Atlantis Zeus
Problem • There are N pairs of (island + controller) • 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?
Model • Two integer sequences A = (a1, a2, … , aN), B = (b1, b2, … , bN) are parallel if for some positive integer K, a1 = b1+K, a2 = b2+K, … , aN = bN+K • Given a collection C of 2N positive integers, partition it into 2 parallel sequences of size N, if possible
Smaller problems • How to determine if two sequences are parallel? • 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? • Sorting – O(NlgN) (O(N) for count sort) 1 3 5 8 1 9 5 3 3 7 +2 8 5 3 5 7 10
Algorithm 1 • Idea • Partition C into two collections of size N • Determine if two parallel sequences can be formed from the two collections (previous slide) • 2NCN ways to partition C • Exponential • Expected score: 50%
Observations • Out of the 2NCN different partitions, most of them should not be tested, for example • {1, 2, …, 2N} {1, 4, ??, …, ??}, {2, 3, ??, …, ??} • Why? • {1, 2, …, 2N} {1, 2, 4, ??, …, ??}, {3, ??, …, ??} • The smallest integer from one collection and the smallest integer from another collection form a pair
Almost there… • Suppose we know the smallest pair, can we reconstruct the partition? • Fact: The smallest pair must contain the smallest integer in the C
An Example • Given C = {2, 6, 1, 3, 7, 4, 8, 9} and suppose we are told that (1, 3) is the smallest pair (1, 3) (2, 4) 2 6 1 3 7 4 8 9 (6, 8) (7, 9) Smallest remaining integer = 6 7 2 Its partner = 6+2 = 8 7+2 = 9 2+2 = 4
Algorithm 2 • Idea • Suppose (a, b) is the smallest pair; let d = b – a • Repeat N times • Let s be the smallest integer in C • If s+d is not in C, return failure • 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?
Algorithm 2 - Analysis • How many different “smallest pairs” should be tried? • N • (smallest, 2nd smallest), (smallest, 3rd smallest), …, (smallest, (n+1)th smallest)
Algorithm 2 – Analysis (2) • The loop takes O(N2) time • Overall time complexity is O(N3) • Too slow when N = 1000 • Observation: The pairs we get are increasing • Both coordinates are increasing • (1, 3) ≤ (2, 4) ≤ (6, 8) ≤ (7, 9)
A B Algorithm 2A • Sort the integers in C • Keep two pointers (integers) (1, 3) (2, 4) 1 2 3 4 6 7 8 9 (6, 8) (7, 9) Smallest remaining integer = 6 2 7 Its partner = 7+2 = 9 6+2 = 8 2+2 = 4
Algorithm 2A • The loop takes O(N) time • Overall time complexity is O(N2) • Not a problem even when N = 10000
Algorithm 3 (Out of Syllabus) • For trainees with graph theory background • Fix a height difference, construct a graph, then find a maximum bipartite matching • Example: Height difference = 2 • Perfect matching Feasible • Overall time complexity: O(N3.5) or O(N3) 2 1 3 4 6 7 8 9
Extensions and Mutations • Constraints: • Real numbers instead of integers • Height sums instead of height differences • Height differences can lie in a range instead of a fixed number • Objectives: • Minimize sum of height differences • Minimize maximum height difference