PROBLEM LINKS:
DIFFICULTY
Simple
PREREQUISITES
arrays, divide-and-conquer, recursion
PROBLEM
Given an array A[0…N+1], consider the indices [1…N] as spots for the soldiers and index 0 as the left tower and the index N+1 as the right tower. Initially set A[0] = 1, A[N+1] = 1 and rest all have 0s. Given a permutation P[1…N] of the indices {1, 2,…,N}, we fill them with 1s in that order and find the length of used wire as follows.
used_length = 0 for i = 1 to N used_length += ( distance from index P[i] to nearest index having 1 to left ); used_length += ( distance from index P[i] to nearest index having 1 to right ); A[ P[i] ] = 1;
You can see that the used_length depends on the permutation P and given an integer M, we need to find the minimum length of unused wire. It is same as asking for the maximum possible used_length ≤ M for some permutation P. If M is not sufficient for any permutation P, output -1.
QUICK EXPLANATION
If you have solved the problem or almost got it yourself, this explanation is for you. If you have not solved it and eager to know how to build the idea from scratch, skip this part and go to the Detailed explanation section below.
There is a simple bruteforce solution and other clever solution. They are explained below.
-
N = 30 is so small that you can generate all possible used_lengths for each N = 1 to 30. For a given N, we just need to fix the first position and that partitions the problem in to two independent smaller subproblems and we can merge the answers to these smaller subproblems. If we consider the number of possible used_lengths for a given N to be of order O(N2), this requires time O(N5), which is fast enough for N = 30.
-
Given N, we can find the minimum and maximum used_lengths.
For minimum, its always better to try partitioning the problem into two equal subproblems.
minLen[n] = (n+1) + minLen[n/2] + minLen[n-1-n/2]
For maximum, we just need to place in the order {1,2,3…N}
maxLen[n] = (n+1) + maxLen[n-1] , this is same as maxLen[n] = (n(n+3))/2*
Now, can you prove that all the values between [ minLen[n], maxLen[n] ] are possible ? An idea is given in tester’s approach.
DETAILED EXPLANATION
If you haven’t solved this problem yet, take some time now and try different cases on paper. eg. Consider N = 4 and try to find all possible sum_of_lengths for different permutations of {1,2,3,4}. You can also take a look at the explanation of sample cases under the problem statement for better understanding.
First lets see an almost bruteforce solution. Consider the array A for N = 9.
Initially A = |, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
and we need to fill the array with 1s in some order. Suppose if we set A4 = 1,
then A = |, 0, 0, 0, 1, 0, 0, 0, 0, 0, |
and length of wire on left side = 4, length of wire on right side = 6, so we need wire of length 4 + 6 = 10.
Note that if we place the first soldier among N spots at any index, we need wire of length (N+1). Not only that, the first soldier now acts as a barrier between his left and right sides. There will never be a wire which cross this soldier. So we have two independent problems now. If the first solder is placed at index I, then we need (N+1) length wire for this soldier and solve subproblem with (I-1) spots on left side and subproblem with (N-I) spots on right side.
A = |, 0, 0, 0, 1, 0, 0, 0, 0, 0, | is same as solving
A = |, 0, 0, 0, | and |, 0, 0, 0, 0, 0, |
So we can store all possible values for each N = 1 to 30, and for a given N try all possible positions for the first soldier and mix the answers of the two independent subproblems to get all possible values. As the constraint on N is 30, you can just bruteforce each of these steps.
For an alternate simple approach, see point 2 in the quick explanation section.
SETTER’S SOLUTION
Can be found here
APPROACH
The problem setter used the approach given in the detailed explanation above.
TESTER’S SOLUTION
Can be found here
APPROACH
The problem setter and the tester have independently observed that each of the values between [min, max] can be achieved for some permutation P. This method takes only O(N) time. Here is a rough idea of the proof using induction, in tester’s words.
max[0] = min[0] = 0
max[n] = max[n-1] + n+1
min[n] = min[n/2] + min[n - n/2 - 1] + n+1
Therefore max[n] = n * (n+3) / 2, min[n] = O(n log n).
Here we assume that for k = 1,2,…,n-1, [ min[k], max[k] ] are all possible,
then we should prove [ min[n], max[n] ] are all possible.
From min[n/2] + min[n - n/2 - 1] + n+1 to max[n/2] + max[n - n/2 - 1] + n+1 are all possible by the assumption.
If min[a+1]+min[n-a-2] ≤ max[a]+max[n-a-1] for all a=n/2, n/2+1, …, n-2, then we can show
[ min[n], max[n] ] are all possible.
L.H.S. = O(n log n), R.H.S = O(n^2), therefore, this is correct for large n.
If you can come with a more intuitive and simple proof, please edit this post and add a section ‘Alternate Proof’.