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)
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 does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 3 years ago.
Improve this question
Given two very large numbers a and b where a < b, the problem is to find the following sum:
a + (a + 1) + (a + 2) + ... + (b - 2) + (b - 1) + b
The numbers a and b can be very very large (can contain millions of digits).
As these are very large numbers, programming languages cannot find the sum using integer data types.
So, the only option is to use strings.
What is the most efficient way to do so?
programming languages cannot find the sum using integer data types. So, the only option is to use strings.
This is false. You can't represent a million-digit integer with a 32-bit or 64-bit int, sure, but it doesn't follow that the best option then is to use strings. You would use a library for dealing with large numbers, see "arbitrary precision arithmetic" -- which lets you represent numbers as large as available memory allows.
In the case of this sum, if m = b-a, you want a*(m+1) + sum(1 ...m) . The sum can be calculated with one multiplication via Gauss's formula. That multiplication can be done in O(n^1.47) where n is the number of digits, the various addition operations are O(n), and I assume division by 2 can be done fast, so the whole time complexity is the time complexity of the multiplications.
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.
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
Here is an algorithm for the HIRE-ASSISTANT problem.
HIRE-ASSISTANT(n)
best <- 0
for i <- 1 to n do
if candidate[i] is better than candidate[best]
best <- i
hire candidate i
Now some observations:
1.Candidate 1 is always hired.
2.The best candidate,i.e., the one whose rank is n, is always hired.
3.If the best candidate is candidate 1, then that is the only candidate hired.
Now the problem is what is the probability of hiring twice?
My approach:
Now before nth rank candidate, I can interview any number of candidates as I want to but the order of their rank is fixed.Therefore for i candidates being interviewed before nth rank candidate=C(n-1,i)*(n-i-1)! total cases are possible.So varying i=1 from n-1 and summing and dividing by total possibilities that are n! I calculate the answer, but it does not match with the standard answer so I need help in finding what is wrong?
The probability to hire At least twice is (n-1)/n.
Assuming you have a random permutation of the candidates, you hire only one candidate if and only if the first candidate is also the best. The probability for it to happen is 1/n. Thus, the probability that it won't happen (and you will hire twice or more) is 1-1/n = (n-1)/n
To hire exactly twice:
Choose a location (not first) for the 'best': n-1 possibilities. Let the place be i
For each such place, choose i-1 candidates to be placed before the best: Choose(n-1,i-1)
The first from these i-1 candidates is the best out of them. Need to permute the rest: (i-2)!
In addition, still need to permute for (n-i-1) candidates after the 'best': (n-i-1)!.
This gives us the total number of 'valid' permutations that exactly two candidates are hired are:
f(n) = Sum[ Choose(n-1,i-1)*(i-2)!*(n-i-1)! | for i=2,...,n]
And the probability is simply P=f(n)/n!
Actually, in the 4th bullet we need to permute (n-i) candidates so the final answer reduces to P = 1/n*H_(n-1) where H_(n-1) is the (n-1)^th harmonic number.
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.