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 had an assignment on dynamic programming due last night, but I had to turn it in unfinished because i could not understand how to solve the last problem:
The state wants to monitor traffic on a highway n miles long. It costs ci to install a monitoring device on mile i of the highway. The maximum distance between monitoring devices should not be more than d miles. That is, if there is a monitoring device on mile i, then there must be one monitoring device from mile i + 1 to mile i + d (or it is the case that i + d > n). The state wants a plan that will minimize the cost. Assume there is an array C[1..n] of costs.
Let vk be the cost of the best solution assuming a k mile highway and assuming a monitoring device on mile k. Given C and d, if the values of v1 through vk-1 are known, show how to determine the value of vk. You can write this mathematically, or provide pseudocode in the style of the book. Note you need to take into account all possible values of k from k = 1 to k = n.
I'm sure a problem similar to this will appear on the exam coming up and I'd like to at least know where to begin solving this, so any assistance is appreciated.
Let's define DP[i] as the minimum cost of installing a monitor at station i and some other indexes which are less than i (such that each consecutive station is less than or equal to distance d)
Now the answer to our problem would be
min(DP[n - d + 1], ...DP[n - 2], DP[n - 1], DP[n])
That is the minimum cost of having the last monitor on last d indexes.
Now, the recurrence relation for the dynamic programming can be easily seen as :
DP[i] = min(DP[i - 1], DP[i - 2], ... DP[i - d]) + C[i]
If we want to install a monitor on ith index, we install it by cost C[i], and we must also ensure that we have a monitor in previous d indexes. So, we take the minimum of installing the second last monitor on it's previous d indexes.
If you code the recurrence by naive method it looks O(n * d), but by using the sliding window minimum algorithm using a doubly ended queue, you can reduce the time complexity to asymptotically O(n).
As this is an assignment problem, I won't write in detail. You should be able to follow up from this point.
Related
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 1 year ago.
Improve this question
Can you help me with problem? Given N <= 10^5 pairs of points, suppose they are written in
array A, A[i][0] <= A[i][1]. Also, given M <= 10^5 pairs of segments, i-th pair given in form L_1[i], R_1[i], L_2[i], R_2[i]. For each pair of segments I need to find number of pairs from array A, such that for each pair (A[z][0], A[z][1]) it should be L_1[i] <= A[z][0] <= R_1[i] <= L_2[i] <= A[z][1] <= R_2[i].
I think here we can use scan line algorithm, but I don't know how to fit in time and memory. My idea works in N * M * log(N).
If you map A[i] to a point (A[i][0], A[i][1]) on 2d-plane, then for each segment, basically you're just counting the number of points inside the rectangle whose left-bottom corner is (L_1[i], L_2[i]) and right-top corner is (R_1[i], R_2[i]). Counting the points on 2d-plane is a classic question which could be solved in O(n logn). Here are some possible implementations:
Notice that number of points in a rectangle P(l,b,r,t) could be interpreted as P(0,0,r,t)-P(0,0,l-1,t)-P(0,0,r,b-1)+P(0,0,l-1,b-1), so the problem can be simplified to calculating P(0,0,?,?). This could be done easily if we maintain a fenwick tree during the process which basically resembles scan line algorithm.
Build a persistent segment tree for each x-coordinate (in time O(n logn)) and calculate the answers for segments (in time O(m logn)).
Build a kd-tree and answer each query in O(sqrt(n)) time. This is not efficient but could be useful when you want to insert points and count points online.
Sorry for my poor English. Feel free to point out my typos and mistakes.
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
Let us assume there is highway
There are N potholes at points [p1, p2, p3...., pn]
There are equal number of service crew at points [s1, s2, s3...., sn]
One service crew can repair one pothole only
The cost to send a crew at point pX to repair a pothole at point sX is |pX-sX|
How would you find the minimum cost to repair the road?
Ex:
Potholes are at [3, 5, 7]
Service Crew are stationed at [1, 3, 5]
Few possible combinations are:
1->3, 3->5, 5->7 (Cost = 6)
1->5, 3->7, 5->3 (Cost = 10)
Share/Explain the algorithm you'd use to solve this problem?
What is the time & space complexity of your algorithm?
In this example it should be 6, no matter what!
This is the problem of finding maximum matching in a fully connected bipartite graph.
If the problem has same number of potholes and crews, it's a perfect matching.
Intention would be to find out maximum matching between potholes P and crews C.
Assumption: A pothole can be serviced by any crew.
Matching condition: |P(i) - C(j)| is minimum.
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
There are N teams in a software company. The ith team has Bi employees in it and a total budget of Ai units of money. Each team has to divide their budget within employees equally. But for some teams it's not possible. Therefore the company has to perform revisions in their teams' budget. In one revision, to revise the budget of ith team, the budget of first i teams has to be increased by 1. Your task is to find the minimum number of revisions needed so that for each team, equal distribution of their budget among the employees are possible.
Sample case: (A1 B1), (A2 B2), (A3 B3) : (1 1), (3 7), (5 4).
Solution is 4. Initial budget (1,3,5) -> (2,4,5) -> (5,7,8)
The given problem can be solved by a constructive solution. To start off, let's inci denote the amount to be incremented for every Ai such that the sum (Ai + inci) can be distributed equally within Bi employees. The two observations to be made here are:
(Ai + inci) mod Bi should be equal to 0 for the amount to be distributed equally
inci <= inci-1, since we're always incrementing the budget in a prefix fashion starting from index 1 and hence it's impossible to have inci > inci-1 since inci-1 will always be incremented before inci
Using these two observations, we can start iterating over the given array from right-to-left and at every step we determine the value for inci.
The value of inci should be (c * Bi - Ai), where c is a smallest integral value such that c * Bi >= Ai and (c * Bi - Ai) >= inci+1 the value for which can be calculated in O(1), since we can reform the equation to estimate the value of c, -
c >= (Ai / Bi)
c >= (Ai + inci+1) / Bi
Hence c = max(ceil(Ai / Bi), ceil((Ai + inci+1) / Bi)
Hence the overall complexity of the given solution is O(n) and the final solution is the value of inc1 (assuming 1-indexing)
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 does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Suppose you have to drive from Islamabad to Lahore. At the start, your gas tank is full. Your gas tank, when full, holds enough gas to travel m miles, and you have a map that gives distances between gas stations along the route. Let d1 < d2 < … < dn be the locations of all the gas stations along the route, where di is the distance from Islamabad to the gas station. The distance between neighboring gas stations is at most m miles. Also, the distance between the last gas station and Lahore is at most m miles.
Your goal is to make as few gas stops as possible along the way. Give a greedy algorithm (in pseudo-code form) to determine at which gas stations you should stop.
Is your solution optimal? What is the time complexity of your solution?
This algorithm begins at Islamabad, and repeatedly tries to drive as far as possible without running out of gas.
current_distance = 0
current_stop = 0
stops = []
while current != lahore:
next_stop = 0
while distance(next_stop) - current_distance <= m:
next_stop = next_stop + 1
next_stop = next_stop - 1
current_stop = next_stop
current_distance = distance(current_stop)
add next_stop to stops
return stops
This is an optimal solution. To see this, we note that any sequence of stops that took less stops then the greedy algorithm would have to 'pass' the greedy algorithm at some point along the route.
Using induction, we can see that if the greedy algorithm is the farthest it can be after the first stop, and after the nth stop it is the farthest it could be given stop n - 1, then the greedy algorithm must be the farthest it can be for all stops along the route.
Although this algorithm has complexity O(n) and returns an optimal solution computationally, the route it returns may not be a very 'even' or 'smooth' route. In order to produce routes for actual use by people, more routes that space their stops more evenly would want to be considered.