Dynamic Programming: Tennis Balls, Lockers, and Keys [closed] - algorithm

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I am having trouble finding the Optimal Dynamic Programming solution to a problem and would greatly appreciate any help. The optimal solution is O(T^2+M); I can only find a solution which is O(NM^2).
The problem is:
There are N lockers that are labeled 1,2...N. Each locker is locked, but can be opened using it's unique key. Copies of the key to each locker are in its adjacent lockers; i.e. a copy of the key to locker i is placed in locker i+1 and i-1 (the key to locker 1 is only in locker 2 and the key to locker N is only in locker N-1)
T tennis balls are inside T distinct lockers (and you know which lockers they are in). You are given keys to M of the lockers and your goal is to collect all of the tennis balls by opening the least number of lockers.
My only hint given is:
Hint: Can you efficiently decide whether there exist at least one tennis ball between the ith and jth locker?

First place a dummy cell at N + 1 with a key and no tennis ball.
Now start at the second key, and proceed until the last key (which, you should note, is a dummy key).
At each iteration, calculate the optimal solution to the tennis balls to the left of the current key (inclusive) for two cases: a. the current key is used, and b. the current key is not used. The optimal solution should also record the rightmost key actually used.
If the current key is not used, then use the previous best two solutions to update the cost, using the rightmost key actually used for covering the balls to the left of the current key (inclusive). If the current key is used, then for each of the balls to the left of the current key (inclusive), calculate whether it pays to open from this key or the previously used key (it depends on the midpoint between the keys).
The overall solution is the one that is at the dummy N + 1 cell, and doesn't use the key in it.

Related

Locker Room Algorithm [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
Note: Just to give a heads up, this is not an assignment for my school since I myself don't even know which school wrote this question. Hopefully no misunderstanding occurred.
I found this interesting questions about locker room:
The locker room of the Rec has N lockers that are labeled 1, 2, . . ., N .
Each locker is locked, but can be opened using its unique key.
Copies of the key to each locker are in its adjacent lockers; i.e. a
copy of the key to locker i is placed in locker i + 1 and i − 1 (the
key to locker 1 is only in locker 2 and the key to locker N is only in
locker N − 1).
T tennis balls are inside T distinct lockers (and you
know which of the lockers they are in). You are given keys to M of
the lockers and your goal is to collect all of the tennis balls by
opening the least number of lockers.
For pictures you can see it directly to the file here.
I have to give this questions to some freshman but I wanted to make sure first that I myself already have the correct answer beforehand.
What I'm thinking is that:
Need to check the ball one by one. So for each ball (ignore the other balls), each key have to visit by traversing to the designated ball. For each key, calculate the steps it takes to visit the ball. The smallest result is stored in an variable called "total steps".
Do this exact thing to the next ball and when I get the minimum steps for this current ball. I add this value to "total steps".
Special condition applied if there is a ball above the key, then key start traversing from i + 1 and i - 1.
My question is: am I right? I don't want to share the wrong algorithm to others since it's unprofessional. Looking forward to any comments, suggestions and inputs.
Your algorithm will not result in minimum number of steps. You can not consider balls independently. Lets consider the following case: You have only one key for locker number 1 and balls are in lockers 12, 10, 8, 6, 4, 2. If You consider the balls in the order I have given you will end up with total steps equal to 11 + 9 + 7 + 5 + 3 + 1, while you can solve the problem in a single pass of 11 steps. You should not ignore the boxes visited on the previous steps.
Here is a technique you can use:
Consider each locker (in which there is a ball, of which you have a key and which have none) to be a node in the graph. The nodes will be of two types:
A) Lockers having balls
B) Lockers of which you have keys.
C) Lockers which have none
Consider all lockers of type-A to be closed and type-B to be open.
From all open lockers, find paths to all closed lockers (from locker type-B) and open the locker in A with min path length. Also, open all lockers of type C which fall in this min-path and move them from category-C to B.
Repeat above step till all lockers in A are opened. Sum of all the min-path lengths that you have come across will be your answer.

Dynamic Programming for sub sum elements in a matrix [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
Given a chess board with 4 rows and n columns. On every cell there's an integer. Given 2n discs that part of them, or all of them you can put on a different cell on the board so the total sum is maximal. The only limitation is that 2 discs can't be next to each other vertically or horizontally. How to place the best combination of discs on the board in O(n) using DP ?
1st, we cannot possibly use more than 2*n disk as any column could contain at maximum 2 disks.
let's say d[i][mask] - maximum amount obtained after filling columns from 1 to i with disks so that the last column is filled as mask ( mask could be 0000, 0001, 0010, 0100, 0101, 1000, 1001 or 1010 where 1 means disk was placed and 0 means it wasn't)
So d[i][mask1] = max of (d[i-1][mask2] + number obtained from applying mask1 on i-th column), where mask2 could be any mask which doensn't contradict with mask1
Edit 1: You do not need to change anything. When you are on i-th step on certain mask, it only depends on the answers for masks of i-1. And you already have all of them. You just try to update the current d[i][mask] from those which are valid. As I already said d[i][mask] - stores the maximum value which could be obtained filling columns from 1 to i, optimally, so that the last column has disks in form of this mask (doesn't matter masks before the i-th column were filled, because they won't affect next column)

What's the minimal column sums difference? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
Imagine you are given a matrix of positive integer numbers (maximum 25*15, value of number does not exceed 3000000). When you do column sums and pick the smallest and the largest one, the difference between them must be the smallest possible.
You can swap numbers in every row (permute rows), not in column, how many times you want.
How would you solve this task?
I'm not asking for your code but your ideas.
Thanks in advance
I would make an attempt to solve the problem using Simulated Annealing. Here is a sketch of the plan:
Let the distance to optimize the difference between the max and min column sums.
Set the goal to be 0 (i.e., try to reach as close as possible to a matrix with no difference between sums)
Initialize the problem by calculating the array of sums of all columns to their current value.
Let a neighbor of the current matrix be the matrix that results from swapping two entries in the same row of the matrix.
Represent neighbors by their row index and two swapping column indexes.
When accepting a neighbor, do not compute all sums again. Just adjust the array of sums in the columns that have been swapped and by the difference of the swap (which you can deduce from the swapped row index)
Step 6 is essential for the sake of performance (large matrices).
The bad news is that this problem without the limits is NP-hard, and exact dynamic programming at scale seems out of the question. I think that my first approach would be large-neighborhood local search: repeatedly choose a random submatrix (rows and columns) small enough to be amenable to brute force and choose the optimal permutations while leaving the rest of the matrix undisturbed.

Powers of a half that sum to one [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Call every subunitary ratio with its denominator a power of 2 a perplex.
Number 1 can be written in many ways as a sum of perplexes.
Call every sum of perplexes a zeta.
Two zetas are distinct if and only if one of the zeta has as least one perplex that the other does not have. In the image shown above, the last two zetas are considered to be the same.
Find all the numbers of ways 1 can be written as a zeta with N perplexes. Because this number can be big, calculate it modulo 100003.
Please don't post the code, but rather the algorithm. Be as precise as you can.
This problem was given at a contest and the official solution, written in the Romanian language, has been uploaded at https://www.dropbox.com/s/ulvp9of5b3bfgm0/1112_descr_P2_fractii2.docx?dl=0 , as a docx file. (you can use google translate)
I do not understand what the author of the solution meant to say there.
Well, this reminds me of BFS algorithms(Breadth first search), where you radiate out from a single point to find multiple solutions w/ different permutations.
Here you can use recursion, and set the base case as when N perplexes have been reached in that 1 call stack of the recursive function.
So you can say:
function(int N <-- perplexes, ArrayList<Double> currentNumbers, double dividedNum)
if N == 0, then you're done - enter the currentNumbers array into a hashtable
clone the currentNumbers ArrayList as cloneNumbers
remove dividedNum from cloneNumbers and add 2 dividedNum/2
iterate through index of cloneNumbers
for every number x in cloneNumbers, call function(N--, cloneNumbers, x)
This is a rough, very inefficient but short way to do it. There's obviously a lot of ways you can prune the algorithm(reduce the amount of duplicates going into the hashtable, prevent cloning as much as possible, etc), but because this shows the absolute permutation of every number, and then enters that sequence into a hashtable, the hashtable will use its equals() comparison to see that the sequence already exists(such as your last 2 zetas), and reject the duplicate. That way, you'll be left with the answer you want.
The efficiency of the current algorithm: O(|E|^(N)), where |E| is the absolute number of numbers you can have inside of the array at the end of all insertions, and N is the number of insertions(or as you said, # of perplexes). Obviously this isn't the most optimal speed, but it does definitely work.
Hope this helps!

How many combinations are there to put n chapters into k(<n)volumes? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Also, how can algorithmicly generate them within polynomial of n? Pseudo-code is ok.
This problem comes down to selecting k-1 borders, which divide n elements in k parts. As such, k-1 positions need to be selected out of n+k-1 positions, resulting in
possibilities. As an example, let's say we need to divide four sweets among three children. In this case, a first possibility would be to put the two borders on the first two positions, leaving the four sweets in positions 3-6:
As such, the first two children get no candy, while the third child gets all four. Another possibility would be to put the first border at position 2 and the second border at position 4:
Now the first two children get one candy each (positions 1 and 3), while the third child gets two (positions 5-6). Iterating over all positions results in a total of 15 possibilities.
Note that the answer is different when all volumes need to contain at least one chapter. In this case, k items are already given to one volume each, leaving n-k chapters. As such, there are
possibilities. Note that the condition k<n is important in this case.

Resources