Dynamic Programming optimal broadcast - algorithm

I know how to solve this problem in an usual way, not using dynamic programming.
If you could be kind enough to explain to me the solution/give me a general idea/ pseudocode. Thanks a bunch.
The input consists of a sequence R = hR0, . . . ,Rni of non-negative integers, and an integer k. The number Ri represents the number of users requesting some particular piece of information at time i (say from a www server).
If the server broadcasts this information at some time t, the the requests of all the users who requested the information
strictly before time t are satisfied. The server can broadcast this information at most k times. The goal is to pick the k times to broadcast in order to minimize the total time (over all requests) that requests/users have to wait in order
to have their requests satisfied.
As an example, assume that the input was R = 3, 4, 0, 5, 2, 7 (so n = 6) and k = 3. Then one possible solution
(there is no claim that this is the optimal solution) would be to broadcast at times 2, 4, and 7 (note that it is obvious
that in every optimal schedule that there is a broadcast at time n + 1 if Rn 6= 0). The 3 requests at time 1 would
then have to wait 1 time unit. The 4 requests at time 2 would then have to wait 2 time units. The 5 requests at
time 4 would then have to wait 3 time units. The 2 requests at time 5 would then have to wait 2 time units. The
7 requests at time 6 would then have to wait 1 time units. Thus the total waiting time for this solution would be
3 × 1 + 4 × 2 + 5 × 3 + 2 × 2 + 7 × 1. .
I/O description. Input: n and k, separated by one space on the first line, then R on second line. Output: the
sequence of the k times.

Set the first broadcast at time i.
Solve the problem for R' = {Rj | j>=i} and k' = k-1
Of course, you will need to store all sub-solutions to make it a dynamic programming algorithm rather than plain recursion.
Note that you must begin with k-1 broadcast times, as the kth broadcast will always be after the last time with non-zero users.
The problem is with step 1. You can try every possible position (worst case time complexity will be n*k i think). I recommend you try this naive method first, test it on some data, and see if you can come up with a better way to find the position of the first broadcast.

Related

Catch the Plane problem from ACM ICPC 2018 world finals [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 2 years ago.
Improve this question
It's about a few days that I am thinking about the algorithm of this problem. I've come up with different solutions but none of them got the right output. I was thinking about the directed acyclic graph but it seems that the passenger can do a round-trip, for example, from station 0 to 3, and then from 3 to 0, and then 0 to 1. I would appreciate if someone can describe the algorithm(not the code) of this problem. For making it easier to look up, I put the problem here, as well.
Your plane to the ICPC Finals departs in a short time, and the only
way to get to the airport is by bus. Unfortunately, some of the bus
drivers are considering going on strike, so you do not know whether
you can get to the airport on time. Your goal is to plan your journey
in such a way as to maximize the probability of catching your plane.
You have a detailed map of the city, which includes all the bus
stations. You are at station 0 and the airport is at station 1. You
also have a complete schedule of when each bus leaves its start
station and arrives at its destination station. Additionally, for each
bus you know the probability that it is actually going to run as
scheduled, as opposed to its driver going on strike and taking the bus
out of service. Assume all these events are independent. That is, the
probability of a given bus running as planned does not change if you
know whether any of the other buses run as planned. If you arrive
before the departure time of a bus, you can transfer to that bus. But
if you arrive exactly at the departure time, you will not have enough
time to get on the bus. You cannot verify ahead of time whether a
given bus will run as planned – you will find out only when you try to
get on the bus. So if two or more buses leave a station at the same
time, you can try to get on only one of them.
Input
The first line of input contains two integers m (1 ≤ m ≤ 10^6 ) and n (2 > ≤ n ≤ 10^6 ), denoting the number of buses and the number of stations in > the city. The next line contains one integer
k (1 ≤ k ≤ 10^18 ), denoting the time by which you must arrive at the
airport. Each of the next m lines describes one bus. Each line
contains integers a and b (0 ≤ a, b < n, a != b), denoting the start
and destination stations for the bus. Next are integers s and t (0 ≤ s
< t ≤ k), giving the departure time from station a and the arrival
time at station b. The last value on the line is p (0 ≤ p ≤ 1, with at
most 10 digits after the decimal point), which denotes the probability
that the bus will run as planned.
Output
Display the probability that you will catch your plane, assuming you
follow an optimal course of action. Your answer must be correct to
within an absolute error of 10^−6 .
Sample Input
8 4
1000
0 1 0 900 0.2
0 2 100 500 1.0
2 1 500 700 1.0
2 1 501 701 0.1
0 3 200 400 0.5
3 1 500 800 0.1
3 0 550 650 0.9
0 1 700 900 0.1
Sample Output
0.3124
Consider the arrival event associated with each scheduled trip.
If you participate in an arrival, i.e., if you actually successfully take the scheduled trip, then you end up at the station at the arrival time and you decide what to do from there.
For each arrival you can easily calculate the best probability of making your flight, if you already know the best probabilities for the arrivals you could possibly get to next.
Since the arrivals you could possibly get to next are all later, if you process all the possible arrivals in reverse chronological order, you can calculate the best probability for making your flight after each arrival, using only probabilities that you will already have calculated.
The process ends with the best probability for your initial arrival at station 0.
If you're clever in the implementation the whole thing takes O(N log N) time.

Greedy Algorithm: Assigning jobs to minimize cost

What is the best approach to take if I want to find the minimum total cost if I want to assign n jobs to a person in a sequence which have cost assigned to them? For eg. I have 2 jobs which have costs 4 and 5 respectively. Both jobs take 6 and 10 minutes respectively. So the finish time of the second job will be finish time of first job + time taken by this job. So the total cost will be finish time of each job multiplied by its cost.
If you have to assign n jobs to 1 person (or 1 machine) in scheduling literature terminology, you are looking to minimize weighted flow time. The problem is polynomially solvable.
The shortest weighted processing time sequence is optimal.
Sort and reindex jobs such that p_1/w_1 <= p_2/w_2 <= ... <= p_n/w_n,
where, p_i is the processing time of the ith job and w_i is its weight or cost.
Then, assign job 1 first, followed by 2 and so on until n.
If you look at what happens if you swap two adjacent values you will end up comparing terms like (A+c)m + (A+c+d)l and (A+d)l + (A+c+d)m, where A is the time consumed by earlier jobs, c and d are times, and l and m are costs. With some algebra and rearrangement you can see that the first version is smaller if c/m < d/l. So you could work out for each job the time taken by that job divided by its cost, and do first the jobs with smallest time per unit cost. - check: if you have a job that takes 10 years and has a cost of 1 cent, you want to do that last so that 10 year wait doesn't get multiplied by any other costs.

Finding minimum number of days

I got this question as a part of the interview and I am still unable to solve it.
It goes like this
A person has to complete N units of work; the nature of work is the same.
In order to get the hang of the work, he completes only one unit of work in the first day.
He wishes to celebrate the completion of work, so he decides to complete one unit of work in the last day.
Given that he is only allowed to complete x, x+1 or x-1 units of work in a day, where x is the units of work
completed on the previous day.
How many minimum days will he take to complete N units of work?
Sample Input:
6
-1
0
2
3
9
13
Here, line 1 represents the number of input test cases.
Sample Output:
0
0
2
3
5
7
Each number represents the minimum days required for each input in the sample input.
I tried doing it using the coin change approach but was not able to do so.
In 2k days, it's possible to do at most 2T(k) work (where T(k) is the k'th triangular number). In 2k+1 days, it's possible to do at most T(k+1)+T(k) at most work. That's because if there's an even (2k) number of days, the most work is 1+2+3+...+k + k+(k-1)+...3+2+1. Similarly, if there's an odd (2k+1) number of days, the most work is 1+2+3+...+k+(k+1)+k+...+3+2+1.
Given this pattern, it's possible to reduce the amount of work to any value (greater than 1) -- simply reduce the work done on the day with the most work, never picking the start or end day. This never invalidates the rule that the amount of work on one day is never more than 1 difference from an adjacent day.
Call this function F. That is:
F(2k) = 2T(k)
F(2k+1) = T(k)+T(k+1)
Recall that T(k) = k(k+1)/2, so the equations simplify:
F(2k) = k(k+1)
F(2k+1) = k(k+1)/2 + (k+1)(k+2)/2 = (k+1)^2
Armed with these observations, you can solve the original problem by finding the smallest number of days where it's possible to do at least N units of work. That is, the smallest d such that F(d) >= N.
You can, for example, use binary search to find d, or an optimal approach is to solve the equations. The minimal even solution has d/2 * (d/2 + 1) >= N which you can solve as a quadratic equation, and the minimal odd solution has (d+1)^2/4 >= N, which has a solution d = ceil(2sqrt(N)-1). Once you've found the minimal even and odd solutions, then you can pick the smaller of the two.
AS you want to have the minimum amounts of days you can just say yeah x+1, since if you want the minimum amount of days, BUT you have to consider that his last day x should be 1 so you have to break at a given point and go x-1, so now we have to determine the Breakpoint.
The Breakpoint is located in the middle of the days, since you start at 1 and want to end at 1.
For example you have to do 16 units so you distribute your days like:
Work done:
1234321.
7 days worked.
When you can't make an even Breakpoint like above repeat some values
5 = 1211
Samples:
2: 11
3: 111
9: 12321
13: 1234321
If you need to do exactly N units, not more, then you could use dynamic programming on the matrix a[x][y] where x is the amount of work done in the last day, y is the total amount of work, and a[x][y] is the the minimum number of days needed. You could use Dijkstra's algorithm to minimize a[1][N]. Begin with a[1][1]=1.

Is there better way than a Dijkstra algorithm for finding fastest path that do not exceed specified cost

I'm having a problem with finding fastest path that do not exceed specified cost.
There's similar question to this one, however there's a big difference between them.
Here, the only records that can appear in the data are the ones, that lead from a lower point to a higher point (eg. 1 -> 3 might appear but 3 -> 1 might not) (see below). Without knowing that, I'd use Dijkstra. That additional information, might let it do in a time faster than Dijkstras algorithm.
What do you think about it?
Let's say I've got specified maximum cost, and 4 records.
// specified cost
10
// end point
5
//(start point) (finish point) (time) (cost)
2 5 50 5
3 5 20 9
1 2 30 5
1 3 30 7
// all the records lead from a lower to a higher point no.
I have to decide, whether It's possible to get from point (1) to (5) (its impossible when theres no path that costs <= than we've got or when theres no connection between 1-5) and if so, what would be the fastest way to get in there.
The output for such data would be:
80 // fastest time
3 1 // number of points that (1 -> 2) -> (2 -> 5)
Keep in mind, that if there's a record saying you can move 1->2
1 2 30 5
It doesnt allow you to move 2<-1.
For each node at depth n, the minimum cost of path to it is n/2 * (minimal first edge at the path + minimal edge connecting to the node) - sum of arithmetic series. If this computation exceeds the required maximum, no need to check the nodes that follow. Cut these nodes off and apply Dijkstra on the rest.

Determining running time of an algorithm to compare two arrays

I want to know how it is possible to determine the run time of an algorithm written in pseudocode so that I can familiarize myself with run time. So for example, how do you know what the run time of an algorithm that will compare 2 arrays to determine if they are not the same?
Array 1 = [1, 5, 3, 2, 10, 12] Array 2 = [3, 2, 1, 5, 10, 12]
So these two arrays are not the same since they are ordered differently.
My pseudocode is:
1) set current pointer to first number in first array
2) set second pointer to first number in second array
3) while ( current pointer != " ") compare with same position element in other array
4) if (current pointer == second pointer)
move current pointer to next number
move second pointer to next number
5) else (output that arrays are not the same)
end loop
So I am assuming first off my code is correct. I know step 4 executes only once since it only takes 1 match to display arrays are not the same. So step 4 takes only constant time (1). I know step 1 and 2 only execute once also.
so far I know run time is 3 + ? (? being the run time of loop itself)
Now I am lost on the loop part. Does the loop run n times (n being number of numbers in the array?), since worst case might be every single number gets matched? Am I thinking of run time in the right way?
If someone can help with this, I'll appreciate it.
Thanks!
What you are asking about is called the time-complexity of your algorithm. We talk about the time complexity of algorithms using so called Big-O notation.
Big-O notation is a method for talking about the approximate number of steps our algorithms take relative to the size of the algorithms input, in the worst possible case for an input of that size.
Your algorithm runs in O(n) time (pronounced "big-oh of n" or "order n" or sometimes we just say "linear time").
You already know that steps 1,2, and 4 all run in a constant number of steps relative to the size of the array. We say that those steps run in O(1) time ("constant time").
So let's consider step 3:
If there are n elements in the array, then step 3 needs to do n comparisons in the worst case. So we say that step 3 takes O(n) time.
Since the algorithm takes O(n) time on step 3, and all other steps are faster, we say that the total time complexity of your algorithm is O(n).
When we write O(f), where f is some function, we mean that the algorithm runs within some constant factor of f for large values.
Take your algorithm for example. For large values of n (say n = 1000), the algorithm doesn't take exactly n steps. Suppose that a comparison takes 5 instructions to complete in your algorithm, on your machine of choice. (It could be any constant number, I'm just choosing 5 for example.) And suppose that steps 1, 2, 4 all take some constant number of steps each, totalling 10 instructions for all three of those steps.
Then for n = 1000 your algorithm would take:
Steps 1 + 2 + 4 = 10 instructions. Step 3 = 5*1000 = 5000 instructions.
This is a total of 5010 instructions. This is about 5*n instructions, which is a constant factor of n, which is why we say it is O(n).
For very large n, the 10 in f = 5*n + 10 becomes more and more insignificant, as does the 5. For this reason, we simply reduce the function to f is within a constant factor of n for large n by saying f is in O(n).
In this way it's easy to describe the idea that a quadratic function like f1 = n^2 + 2 is always larger than any linear function like f2 = 10000*n + 50000 when n is large enough, by simply writing f1 as O(n) and f2 as O(n^2).
You are correct. The running time is O(n) where n is the number of elements in the arrays. Each time you add 1 element to the arrays, you would have to execute the loop 1 more time in the worst case.

Resources