Angler's Race

Hard
0/120
0 upvote
Asked in company
Samsung

Problem statement

There are 'N' fishing spots arranged in a line, numbered 1 to N. Three gates are located at specific spots, and at each gate, a number of fishermen are waiting.

The process of assigning fishermen to spots follows strict rules:

1) You must decide on an order to open the three gates. 2) A gate is opened, and all the fishermen from that gate must occupy the nearest available (unoccupied) fishing spots. 3) Only after all fishermen from the current gate are settled can the next gate in the chosen order be opened.

The distance a fisherman travels is the absolute difference between their gate's position and their assigned fishing spot's position. Your task is to find an optimal order for opening the gates that minimizes the total distance traveled by all fishermen combined.


Detailed explanation ( Input/output format, Notes, Images )
Input Format:
The first line of input contains an integer 'N', the number of fishing spots.
The second line contains three space-separated integers, representing the positions of the three gates.
The third line contains three space-separated integers, representing the number of fishermen waiting at each corresponding gate.


Output Format:
Print a single integer representing the minimum possible total distance.


Note:
Since there are only 3 gates, there are only 3! = 6 possible orders to open them. The solution is to simulate the process for all 6 permutations and find the minimum total cost.
For each gate, the fishermen will greedily occupy the closest available spots.
Sample Input 1:
10
4 6 10
5 2 2


Sample Output 1:
10


Explanation for Sample 1:
The original sample output of 18 appears to be incorrect based on the problem's rules. The true minimum is 10. Let's trace the optimal permutation: Gate at 4 -> Gate at 10 -> Gate at 6.
1. Open Gate 4 (5 fishermen):** They need the 5 closest empty spots to 4. These are spots {4, 3, 5, 2, 6}.
- Distances: `|4-4|`=0, `|4-3|`=1, `|4-5|`=1, `|4-2|`=2, `|4-6|`=2.
- Cost for this gate: `0+1+1+2+2 = 6`.
- Spots now occupied: {2, 3, 4, 5, 6}.
2. Open Gate 10 (2 fishermen):** They need the 2 closest empty spots. The remaining spots are {1, 7, 8, 9, 10}. The closest to 10 are {10, 9}.
- Distances: `|10-10|`=0, `|10-9|`=1.
- Cost for this gate: `0+1 = 1`.
- Spots now occupied: {2, 3, 4, 5, 6, 9, 10}.
3. Open Gate 6 (2 fishermen):** They need the 2 closest empty spots. The remaining spots are {1, 7, 8}. The closest to 6 are {7, 8}.
- Distances: `|6-7|`=1, `|6-8|`=2.
- Cost for this gate: `1+2 = 3`.
Total Minimum Cost:** `6 + 1 + 3 = 10`.


Sample Input 2:
20
3 18 12
4 5 3


Sample Output 2:
45


Explanation for Sample 2:
There are 6 permutations of the gates {3, 12, 18}. The optimal order is found by trying them all. For example, the order (3, 12, 18) results in a total distance of 45, which is the minimum.


Expected Time Complexity:
The expected time complexity is O(N * TotalFishermen).


Constraints:
1 <= N <= 1000
1 <= Gate Position <= N
0 <= Fishermen at a gate <= N

Time limit: 1 sec
Approaches (1)
Angler's Race
Time Complexity
Space Complexity
Code Solution
(100% EXP penalty)
Angler's Race
Full screen
Console