Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 11 years ago.
Improve this question
https://liahen.ksp.sk/ provides some trainig problems for competitions. But they do not have solutions with them. So I would like to ask you how to solve this, since I am trying to solve it for hours and still nothing.
Problem:
We have some line of length L kilometers. Some person stands in the middle of this line. We have list with two numbers: x, y and z - y is the time in seconds when z crates will fall to the x-th kilometer of the road. Person can stay or move one km to right or left each second. To catch crates, person must be on place where they will fall exactly in the second they are intended to fall.
Point of the algorithm is to find a way to save maximal number of crates.
I'd do this as a dp problem: for each time, for each place you can stand, store the maximum number of crates you can have caught.
Runtime would be O(L * timesteps)
EDIT:
if L is very large compared to the number of drop points you can get away with storing only information at the drop points, saving a bit on the performance:
for each drop point store how far it is to the left and right neighbor, and a buffer indicating crates collected at this point at time t-i for i from 0 to the maximum distance to a neighbor.
at each time step, for each drop point, fetch the possible crates collected from each neighbors at time t-distance, and select the best value.
add the number of crates dropped at this point, if any.
This algorithm runs in O(droppoints*timesteps), but uses O(L) space.
You can solve this problem using dynamic programming.
Here's the basic recursive relationship that you need to solve.
saved[y][x] = max(saved[y-1][x-1],saved[y-1][x+1],saved[y-1][x])+crates[y][x]
crates[y][x] : the # crates that fall at time y at position x. You create this array from your input data.
saved[y][x] : the maximum number of crates that you have saved so far winding up at time y being in position x.
The equation is based on the fact that at time y-1 ( 1 time step before ), you can only be at position x+1 or x-1, or x (stayed in the same place), so look at both options and pick the one that gives you the most crates saved, and then add the crates saved since you are now at position x at time y ( i.e. crates[y][x] )
Say you start at x = 0, and time y = 0.
Then saved[0][x] = 0 ( assuming that crates start to fall at time y > 0 ), and then you can do DP using the above equation.
Your final answer is to find the maximum value of saved[y][x].
Related
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 2 years ago.
Improve this question
Suppose I have a image that looks like
x x x x
x x x x
x x x x
Dimensions might change, but just give a rough idea about this.
I am curious if there is an algorithm that will help me quickly check if the current point I am looking at is a corner / points on one of the 4 sides / within the square itself, as well as helping me check all points around the point I am currently looking at.
My current approach is like writing a few helper functions that separately check if the current coordinate is a corner / points on one of the 4 sides / within the square itself? And within each helper function, I use several loops to check all the neighbor points around the point I am currently looking at. But I feel like this approach is extremely ineffective, I believe there must exists a more advanced way to do this, can anyone help me if you have encountered this kind of question before?
Thanks.
Largely you are correct, but there should be no need to use loops. You can make your functions efficient by using some index calculations and using direct access to 1-dimensional array.
Imagine that your image is stored in a 1-dimensional array D. The image is of size (m,n). Hence the array will have a size of m x n. Each data point will have its ID as the index to the array D.
To access neighbors of ID = a, use the following offsets:
a-1, a+1 for left and right neighbors
a-m, a+m for bottom and top neighbors
a-m+1, a-m-1, a+m+1, a+m-1 for diagonal neighbors
After every offset you need to check for the following:
is the neighbor index out of bound for the array D?
does the neighbor index wrap around the x-bounds i.e. assert that
abs((neighbor_id % m)-(a%m)) <= 1 , else neighbor_id is not my neighbor.
Of course, the second test assumes that your image is large enough (perhaps m > 3).
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 5 years ago.
Improve this question
I have a floating point number x from [1, 500] that generates a binary y of 1 at some probability p. And I'm trying to find the x that can generate the most 1 or has highest p. I'm assuming there's only one maximum.
Is there a algorithm that can converge fast to the x with highest p while making sure it doesn't jump around too much after it's achieved for e.x. within 0.1% of the optimal x? Specifically, it would be great if it stabilizes when near < 0.1% of optimal x.
I know we can do this with simulated annealing but I don't think I should hard code temperature because I need to use the same algorithm when x could be from [1, 3000] or the p distribution is different.
This paper provides an for smart hill-climbing algorithm. The idea is basically you take n samples as starting points. The algorithm is as follows (it is simplified into one dimensional for your problem):
Take n sample points in the search space. In the paper, he uses Linear Hypercube Sampling since the dimensions of the data in the paper is assumed to be large. In your case, since it is one-dimensional, you can just use random sapling as usual.
For each sample points, gather points from its "local neighborhood" and find a best fit quadratic curve. Find the new maximum candidate from the quadratic curve. If the objective function of the new maximum candidate is actually higher than the previous one, update the sample point to the new maximum candidate. Repeat this step with smaller "local neighborhood" size for each iteration.
Use the best point from the sample points
Restart: repeat step 2 and 3, and then compare the maximums. If there is no improvement, stop. If there is improvement, repeat again.
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 6 years ago.
Improve this question
I have n (about 10^5) points on a hypersphere of dimension m (between 10^4 to 10^6).
I am going to make a bunch of queries of the form "given a point p, find the closest of the n points to p". I'll make about n of these queries.
(Not sure if the hypersphere fact helps at all.)
The simple naive algorithm to solve this is, for each query, to compare p to all other n points. Doing this n times ends up with a runtime of O(n^2 m), which is far too big for me to be able to compute.
Is there a more efficient algorithm I can use? If I could get it to O(nm) with some log factors that'd be great.
Probably not. Having many dimensions makes efficient indexing extremely hard. That is why people look for opportunities to reduce the number of dimensions to something manageable.
See https://en.wikipedia.org/wiki/Curse_of_dimensionality and https://en.wikipedia.org/wiki/Dimensionality_reduction for more.
Divide your space up into hypercubes -- call these cells -- with edge size chosen so that on average you'll have one point per cube. You'll want a map from hypercells to the set of points they contain.
Then, given a point, check its hypercell for other points. If it is empty, look at the adjacent hypercells (I'd recommend a literal hypercube of hypercells for simplicity rather than some approximation to a hypersphere built out of hypercells). Check that for other points. Keep repeating until you get a point. Assuming your points are randomly distributed, odds are high that you'll find a second point within 1-2 expansions.
Once you find a point, check all hypercells that could possibly contain a closer point. This is possible because the point you find may be in a corner, but there's some closer point outside of the hypercube containing all the hypercells you've inspected so far.
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
I have been stuck on this uva problem from a long time now.
Abridged problem statement : Given a set of sticks of various lengths, is it possible to join them end-to-end to form a square? There are a maximum of 20 sticks and each stick has a length less than 10000.
There are different solutions possible for this problem. One of them is a backtracking solution explained here. But there exists other dynamic programming solutions explained here, here and here with a better running time. But I can't understand what approach they are using. Please help me understand the dp algorithm.
If you are not familiar with dynamic programming over subsets, I suggest you read about it first. This link can help, but there may be better tutorials out there.
Back to the given problem, since M is no more than 20, the following 2M×M approach will probably work.
For each of the 2M subsets of the given sticks, we know the total length of the sticks in that subset. We also know the total length of all the given sticks and thus the length of the square side. We construct the square by laying sticks on its sides. Let us fix the order in which we construct our square: we start at the upper left corner and move along the square border in clockwise direction, laying sticks on the way and leaving no gaps. So, first we fully construct the upper side (from left to right), then the right side (top-down), then the lower one (right-to-left) and finally the left one (bottom-up). Whenever the distance to the next square corner in our traversal is L, we can't lay a stick of length greater than L; at least not until we reach that corner using other sticks. Now, the question is: can we order the sticks in such a way that the square can be constructed by our procedure?
There are M! different orders in which we can try to lay the sticks. But, if we lay sticks one-by-one, when choosing the next stick, all that we are concerned with is the set of sticks already laid, not their particular order. This observation leads us to considering only 2M subsets which is way smaller than M! orders.
Next we define subproblems of the problem defined before. For each subset of sticks, the question is: can we order the sticks in such a way that all of them can be laid sequentially by the rules of the above procedure? In other words, can we construct a "valid prefix" of the square traversal, as it is defined above?
We will say a subset of sticks is good if the answer to the above question is "yes", and bad otherwise. Sure, the empty subset is good. In the end, we are interested in whether the whole given set of sticks is also good. To find that out, we can process the subsets in natural order (for M=3, that would be 000, 001, 010, 011, 100, 101, 110, 111 where 1s correspond to sticks in the subset). For each new non-empty subset S, it is good if and only if some of its "immediate subsets" T (S without exactly one element - say a stick of length X) is good, and T can be extended by that stick of length X according to the rules of our construction procedure (that is, laying a stick of length X, we won't have to bend it around some corner).
What's left is implementation details. For each subset, either store or calculate the total length of the sticks in it and find the distance to the next corner L. If this subset is good, it can be extended only by sticks having two properties: (1) length no more than L and (2) not already in the subset.
Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
I've got two normal PDFs, given by μ1, μ2, σ1 and σ2. What I need is the integral over the product of these functions - the solution to the problem that if X occurred at μ1 with a certain probability expressed in σ1 and Y occurred at μ2 with a certain probability, what's the probability P(X=Y)?
x=linspace(-500,500,1000)
e1 = normpdf(x,mu1,sigma1)
e2 = normpdf(x,mu2,sigma2)
solution = sum(e1*e2)
To visualise, e1 is blue, e2 green, and e1*e2 is red (magnified by factor 100 for visualisation):
Is there however a more direct way of computing solution given mu1, mu2, sigma1 and sigma2?
Thanks!
You should be able to do the integral easily enough, but it does not mean what you think it means.
A mathematical normal distribution yields a randomly chosen real, which you could think of as containing an infinite number of random digits after the decimal point. The chance of any two numbers from such distributions being the same (even if they are from the same distribution) is zero.
A continuous probability density function p(x) like the normal distribution does not give, at p(x), the probability of the random number being x. Roughly speaking, it says that if you have a small interval of width delta-x at x then the probability of a random number being inside that interval is delta-x times p(x). For exact equality, you have to set delta-x to zero, so again you come out with probability zero.
To compute the interval (whatever it means) you might note that N(x;u,o) = exp(-(x-u)^2)/2o^2) neglecting terms that I can't be bothered to look up in http://en.wikipedia.org/wiki/Normal_distribution, and if you multiply two of these together you can add the stuff inside the exp(). If you do enough algebra you might end up with something that you can rewrite as another exponential with a quadratic inside, which will turn into another normal distribution, up to some factors which you can pull outside the integral sign.
A better way of approaching something like this problem would be to note that the difference of two normal distributions with mean M1 and M2 and variance V1 and V2 is a normal distribution with mean M1 - M2 and variance V1 + V2. Perhaps you could consider this distribution - you can easily work out that the probability that the difference of your two numbers is within any range that catches your fancy, for example between -0.0001 and +0.0001.