0

Ice-Cream: The beach stretches along the seacoast like a narrow strip. At some points of the beach the ice cream stalls are located. One day not all the ice cream sellers come to work. Distribute the sellers among the ice-cream stalls so that the minimum distance between them is as much as possible. So they will interfere less with each other.

Input: The first line contains the number of stalls n (2 < n < 10001) and the number of ice cream sellers k (1 < k < n) at work. The second line contains n positive integers in increasing order - the coordinates of the stalls (the coordinates are not greater than 109).

Output: Print one number - the minimum distance between the adjacent stalls in the optimal arrangement.

Input example:

5 3
1 2 3 100 1000

Output example: 99

This is what I have come up with so far. It is working not fast enough and I need other idea.

import java.util.Scanner;

public class Main 
{
    public static void main(String[] args)
    {
        Scanner s = new Scanner(System.in);
        int[] n = new int[s.nextInt()]; 


  int   k = s.nextInt();
        for(int i = 0; i < n.length; i++)
        {n[i] = s.nextInt();}

    int c = -1;
    int[] cA = new int[k + 3]; //control array
    for(int j = 1; j < cA.length; j++)
    {cA[j] = j - 1;}
    cA[k+1] = n.length;
    cA[k+2] = 0;
    while(true) 
    {
        int currentCoordinate = -1, previousCoordinate = -1, minDist = -1;
        for(int i = 1; i <= k; i++)
        {
            if(currentCoordinate == -1)
            {currentCoordinate = n[cA[i]];}
            else
            {
                previousCoordinate = currentCoordinate;
                currentCoordinate = n[cA[i]];
                int currentDistance = currentCoordinate - previousCoordinate;
                if(minDist == -1 || minDist > currentDistance)
                {minDist = currentDistance;}
            }
        }
        if(minDist > c)
        {c = minDist;}

        int j = 1;
        while(cA[j] + 1 == cA[j + 1]) 
        {cA[j] = j - 1; j++;}

        if(j > k)
        {break;}
        cA[j] = cA[j] + 1;
    }
    System.out.println(c);
}        

}

Danh
  • 5,916
  • 7
  • 30
  • 45
  • (the coordinates are not greater than 10^9) –  Nov 06 '16 at 11:45
  • 7
    Welcome to StackOverflow. Please note that SO is not a code writing service to complete your homework)whatever. Please show your attempt solving the question when asking. – SOFe Nov 06 '16 at 11:46
  • coordinates are not greater than 10^9, https://www.e-olymp.com/en/problems/4035 – 11thdimension Nov 08 '16 at 21:36

1 Answers1

0

This problem is solvable using binary search. First assume the answer is x. It means the minimum distance between two stalls is x. A greedy approach can verify this assumption. It is obvious that in the best configuration we have to use the leftmost stall (Can be easily proven by contradiction). Now traverse the points from left to right until the distance between the leftmost point and the rightmost point is less than x. Upon reaching to the first point (pi) that its distance to the leftmost point is bigger than x increment your counter. From now on your leftmost is px. Repeat this process until you reach the end of the points. Now if your counter is bigger than k it means that you can increase the value of x and vice versa.

So you can binary search to find the minimum value for x. This approach is in O(nLogn).

Saeid
  • 4,147
  • 7
  • 27
  • 43
  • Similar problems: [link](http://stackoverflow.com/questions/40189551/arrange-n-items-in-k-nonempty-groups-such-that-the-difference-between-the-minimu/40205972#40205972), [link](http://stackoverflow.com/questions/39673898/divide-array-into-k-contiguos-partitions-such-that-sum-of-maximum-partition-is-m/39675098#39675098) – Saeid Nov 08 '16 at 23:42