SPOJ - Aggressive cows, what is the meaning of "largest minimum distance" terminology? - c

I'm trying to understand the problem of AGGRCOW - Aggressive cows in Spoj. But how this output has come, I don't get it. First I thought maybe the distance between them like for this input:
Input:
5 3
1 2 4 8 9
Ouput: 3
first we put, cow1 in position arr[0], then we put cow2 in arr[2]. We don't put cow2 in arr1 because then there will be no distance between them (2-1=1). Distance of cow2 and cow1 is 3 units now. So for that we put cow2 in arr[2]. And finally we put cow3 in arr[3] as distance between cow3 and cow2 is here 4. And then we compare this 3<4. This outputs 3.
But when I tried to apply same logic for this input:
Input:
6 3
2 3 4 5 8 9
Ouput: 3
In my logic it should be 2. As if we keep cow1 in arr[0] and cow2 in arr[2] then distance is 4-2=2. And it's minimum. But when I want to see what should be the actual value by googling I found out that it is 3. I dont understand how it is 3. Why not 2? I just want the explanation of this problem, not the code.

The problem is that you are given N stall locations, a0 through aN-1. You must fill C of those, but so that the separation between the filled stall numbers is as high as possible. The output is the separation fulfilled by all filled stalls. That is, if you output 3, it means your solution says "I can fill in the stalls so that the distance between any two filled stalls is at least 3".
If you think about it, you really are looking at the minimum distance between any two filled stalls, and you are trying to maximize it, without compromising the distance to any other filled stalls. Thus: largest minimum distance.
The input is of form
N C
a0 a1 ... aN-2 aN-1
The second example is
6 3
2 3 4 5 8 9
meaning there are 6 stalls: 2, 3, 4, 5, 8, and 9, and three of them need to be filled, maintaining maximum separation between stall numbers.
The optimum solution is to use stalls 2, 5, and 9. The separations are then 5-2 = 3 and 9-5 = 4, and the result is the smallest of them, 3.

In the case 6 3, 2 3 4 5 8 9, the cows could be put in 2, 5, and 8, yielding a minimum distance of 3. That is a larger minimum than 2, so 2 is not the largest minimum possible.

To prevent the cows from hurting each other, FJ wants to assign the cows to the stalls, such that the minimum distance between any two of them is as large as possible.
FJ has to put the cows as far apart from each other as possible - the distance between two cows/stall locations in that setup is the largest minimum distance.
In the given stall locations and given number of cows, you've to choose the stall locations such that all the cows would be accommodated in the stalls which are most distant from each other (minimum distance is the distance between two stall locations where you assign given cows). The distance between two of the nearest stalls in that setup is the largest minimum distance.

Related

Maximum average distance between two numbers across multiple arrays

Let's say you have k arrays of size N, each containing unique values from 1 to N.
How would you find the two numbers that are on average the furthest away from each other?
For example, given the arrays:
[1,4,2,3]
[4,2,3,1]
[2,3,4,1]
Then the answer would be item 1 and 2, because they are of distance 2 apart in the first two arrays, and 3 numbers apart in the last one.
I am aware of an O(kN^2) solution (by measuring the distance between each pair of numbers for each of the k arrays), but is there a better solution?
I want to implement such an algorithm in C++, but any description of a solution would be helpful.
After a linear-time transformation indexing the numbers, this problem boils down to computing the diameter of a set of points with respect to L1 distance. Unfortunately this problem is subject to the curse of dimensionality.
Given
1 2 3 4
1: [1,4,2,3]
2: [4,2,3,1]
3: [2,3,4,1]
we compute
1 2 3
1: [1,4,4]
2: [3,2,1]
3: [4,3,2]
4: [2,1,3]
and then the L1 distance between 1 and 2 is |1-3| + |4-2| + |4-1| = 8, which is their average distance (in problem terms) times k = 3.
That being said, you can apply an approximate nearest neighbor algorithm using the input above as the database and the image of each point in the database under N+1-v as a query.
I've a suggestion for the best case. You can follow an heuristical approach.
For instance, You know that if N=4, N-1=3 will be the maximum distance and 1 will be the minimum. The mean distance is 10/6=1,66667 (sums of distances among pairs within array / number of pairs within an array).
Then, you know that if two numbers are on the edges for k/2 arrays (most of the times), it is already on the average top (>= 2 of distance), even if they're just 1 distance apart in the other k/2 arrays. That could be a solution for a best case in O(2k) = O(k).

HeIp understanding Fibonacci Search

On the internet I only find code for the algorithm but I need understand in form of text first because I have trouble understand things from code only. And other description of the algorithm are very complicated for me (on Wikipedia and other sites).
Here is what I understand for far:
Let say we want search in array the element 10:
Index i 0 1 2 3 4
2 3 4 10 40
Some fibonacci number here:
Index j 0 1 2 3 4 5 6 7 8 9
0 1 1 2 3 5 8 13 21 34
First thing we do is find fibonacci number that is greater-equal to array length. Array length is 4 so we need take fibonacci number 5 that is in index position j=5.
But where we divide the array now and how continue? I really don't understand it.. Please help understand for exam...
The algorithm goes in the following way:
The length of the array is 5, so the fibonacci number which is greater than or equal to 5 is 5. The two numbers which are preceding in the Fibonacci sequence are 2 [n-2] and 3 [n-1] - (2, 3, 5).
So, arr[n-2] i.e. arr[2] is compared with the number to be searched which is 10.
If the element is smaller than the number, then the sequence is shifted 1 time to the left. Also, the previous index is saved for next iteration to give an offset for the index. In this case, since 4 is smaller, n-2 becomes 1 (1, 2, 3). arr[1 + 2(prev)] = arr[3] = 10. So, the index of the number is 3.
If the element is larger, the sequence is shifted 2 times to the left.
Always the min(n-2+offset,n)th element is compared with number to get the matching result.

Algorithm to divide array of length n containing numbers from 1 to n (no repetition) into two equal sum

You are giving array of length N and numbers in the array contain 1 to N no repetition. You need to check if the array can be divided into to list of equal sum.
I know it can be solved using subset sum problem whose time complexity is.
Is there an algorithm so that I can reduce the time complexity?
As per your requirements, we conclude the array will always contain numbers 1 to N.
So if Array.Sum()==Even the answer is YES, otherwise NO.
Since the sum of elements from 1 to n equals n*(n+1)/2, you have to check if n*(n+1) is a multiple of 4, which is equivalent to checking if n is a multiple of 4, or if n+1 is a multiple of 4. The complexity of it is O(1).
If this condition is met, the two subsets are :
if n is a multiple of 4: sum up the odd numbers of first half with even numbers of second half on one hand, and even numbers of first half with odd of second half on the other.
For instance, 1 3 5 8 10 12 , and 2 4 6 7 9 11.
if n = 3 modulo 4 : almost the same thing, just split the first 3 between 1 and 2 on one hand, 3 on the other, you have a remaining serie which has a size multiple of 4.
For instance : 1 2 4 7 , and 3 5 6 ; or if you prefer, 3 4 7, and 1 2 5 6.

Algorithm - Find maximum sum of two numbers in unsorted array that have a minimum distance

I'm trying to find an algorithm that finds the maximum sum of two numbers that have a minimum distance D between them.
For example, lets say we have this array of 8 numbers and the minimum distance for a sum is 2:
9 4 6 2 8 7 5 6
9 can be paired with 2, 8, 7, 5, 6
4 can be paired with 8, 7, 5, 6
6 can be paired with 7, 5, 6
2 can be paired with 9 (from the left side), 5, 6
8 can be paired with 9, 4 and 6
etc..
From this array it is obvious that the maximum possible sum is 9+8 = 17
Does anyone know of an efficient algorithm to do this, or has any idea?
I have tried an algorithm which finds the max number, compares it with every possible else, and then checks every value that does not have a minimum distance from it with every possible else...but this is very slow when the array consists of many numbers and the especially when the minimum distance is large.
Note: All numbers are positive
Thanks.
For each position in the array, find and store the maximum of the elements up to that position. This can be done in O(n) for all positions by updating the maximum in each step.
By scanning right-to-left in the same manner, find and store for each position the maximum of all elements from that position to the end.
Now for each element, array[pos] + max(max_up_to[pos-D], max_from[pos+D]) will give you the highest sum that can be generated with that element. So another O(n) pass gives you the maximum over all elements.
Total: O(n) time, O(n) space.
In fact, you don't even need the additional space: The max_from array isn't needed because it's enough to evaluate array[pos] + max_up_to[pos-D] (since each sum would otherwise be generated twice). And the max_up_to values can be generated on-the-fly as you're iterating over the array.

Given 2 positions (x1,y1) and (x2,y2) print the sum of all elements within the area of rectangle in O(1) time

A 2D matrix is given to you. Now user will give 2 positions (x1,y1) and (x2,y2),
which is basically the upper left and lower right coordinate of a rectangle formed within the matrix.
You have to print sum of all the elements within the area of rectangle in O(1) running time.
Now you can do any pre computation with the matrix.
But when it is done you should answer all your queries in constant time.
Example : consider this 2D matrix
1 3 5 1 8
8 3 5 3 7
6 3 9 6 0
Now consider 2 points given by user (0, 2) and (2, 4).
Your solution should print: 44.
i.e., the enclosed area is
5 1 8
5 3 7
9 6 0
Since your question seems to be related to homeworks, i am just posting a clue... It is a formula that may inspire you :
What does this sum of terms represent ?
Rewrite your problem using mathematical tools, indexes like i and j ...
maybe this can also help:
courtosy of: http://www.techiedelight.com/calculate-sum-elements-sub-matrix-constant-time/

Resources