Catch the Plane problem from ACM ICPC 2018 world finals [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 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.

Related

Sampling packets [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 1 year ago.
Improve this question
I am programming a hardware packet processor, and I am trying to sample packets. My goal is to keep one out of 10 packets. However, I am aware that one cannot just keep every 10th packet as that would not be a correct sampling method.
I use a random number generator, although the number is always between 0 and (2^n) - 1. That is, if the random number is 4 bits in size, the random number generator will generate a number between 0 and 15.
My first approach was to generate a number of size 10 or 11 bits. Say a 10-bit number n would go between 0 and 1023. I was planning to modulo 10 the n random number and pick one out of the 10 numbers. Unfortunately, this is not possible as my hardware packet processor does not contemplate modulo operations without knowing n at compile time.
My second option was to make a simple if:
// between 0 and 1023. random() retrieves a random number with uniform distribution
int<10> n = random();
if n < 102{
keep_packet()
} else{
drop_packet()
}
I wonder if this last method is indeed a correct sampling method and as correct as doing n modulo 10.
It depends on what you mean by "correct".
Assuming n = random() outputs independent uniform random integers in the interval [0, 1023), then checking whether n < 102 will succeed 102/1024 of the time. 102 out of 1024 values will be accepted and the rest rejected. Does it correctly succeed 10% of the time? No. Does it correctly succeed 102/1024 of the time? Yes.
Likewise with the same assumption, checking whether n % 10 == 9 will succeed 102/1024 of the time. 102 out of 1024 values will be accepted (namely those that end in nine) and the rest rejected. Does it correctly succeed 10% of the time? No. Does it correctly succeed 102/1024 of the time? Yes.
There is a third way to proceed: rejection sampling. Keep generating n = random() until n < 1020, then check whether n < 102. This will correctly succeed 10% of the time. The tradeoff, though, is that there is a 4/1024 chance of requiring more than one random number, a (4/1024)^2 chance of requiring more than two, and so on. In fact, rejection sampling will run forever in the worst case (as is the case in general), and stopping the method after K rejections will introduce bias. However, you can make the bias as small as you like by choosing K appropriately (it may or may not be acceptable for you for a hardware device to "freeze").
See also:
Frugal conversion of uniformly distributed random numbers from one range to another
How to generate a random integer in the range [0,n] from a stream of random bits without wasting bits?

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.

Stuck on graph algorithm task

I'm currently trying to solve an algorithm problem from last year's Polish Collegiate Championships which reads as follows:
The Lord Mayor of Bytetown plans to locate a number of radar speed
cameras in the city. There are n intersections in Bytetown numbered
from 1 to n, and n-1 two way street segments. Each of these street
segments stretches between two intersections. The street network
allows getting from each intersection to any other.
The speed cameras are to be located at the intersections (maximum one
per intersection), wherein The Lord Mayor wants to maximise the number
of speed cameras. However, in order not to aggravate Byteland
motorists too much, he decided that on every route running across
Bytetown roads that does not pass through any intersection twice there
can be maximum k speed cameras (including those on endpoints of the
route). Your task is to write a program which will determine where the
speed cameras should be located.
Input
The first line of input contains two integers n and k (1 <= n, k <=
1000000): the number of intersections in Bytetown and maximum number
of speed cameras which can be set up on an individual route. The lines
that follow describe Bytetown street network: the i-th line contains
two integers a_i and b_i (1 <= a_i, b_i <= n), meaning that there is a
two-way street segment which joins two intersections numbered a_i and
b_i.
Output
The first output line should produce m: the number describing the
maximum number of speed cameras, that can be set up in Byteland. The
second line should produce a sequence of m numbers describing the
intersections where the speed cameras should be constructed. Should
there be many solutions, your program may output any one of them.
Example
For the following input data:
5 2
1 3
2 3
3 4
4 5
one of the correct results is:
3 1 2 4
So judging by how many teams solved it, I'm guessing it can't be too hard but still, I got stuck almost immediately with no idea as to how to move on. Since we know that "on every route running across Bytetown roads that does not pass through any intersection twice there can be maximum k speed camera", I guess we first have to somehow dissect the graphs into components being possible routes around the town. This alone seems like a really hard thing to do cause supposing there's an intersection with four motorways coming out of it, it already creates three possible directions for every enter point, thus making 12 routes. Not to mention how the situation complicates when there's more such four-handed intersections.
Maybe I'm approaching the task from the wrong angle? Could you please help?
It seems greedy works here
while k >= 2
mark all leaves of the tree and remove them
k = k - 2;
if ( k == 1 )
mark any 1 of remaining vertices

Dynamic Programming optimal broadcast

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.

fully connection algorithm

I have encoutered an algorithm question:
Fully Connection
Given n cities which spreads along a line, let Xi be the position of city i and Pi be its population.
Now we begin to lay cables between every two of the cities based on their distance and population. Given two cities i and j, the cost to lay cable between them is |Xi-Xj|*max(Pi,Pj). How much does it cost to lay all the cables?
For example, given:
i Xi Pi
- -- --
1 1 4
2 2 5
3 3 6
Then the total cost can be calculated as:
i j |Xi-Xj| max(Pi, Pj) Segment Cost
- - ------ ----------- ------------
1 2 1 5 5
2 3 1 6 6
1 3 2 6 12
So that the total cost is 5+6+12 = 23.
While this can clearly be done in O(n2) time, can it be done in asymptotically less time?
I can think of faster solution. If I am not wrong it goes to O(n*logn). Now let's first sort all the cities according to Pi. This is O(n* log n). Then we start processing the cities in increasing order of Pi. the reason being - you always know you have max (Pi, Pj) = Pi in this case. We only want to add all the segments that come from relations with Pi. Those that will connect with larger indexes will be counted when they will be processed.
Now the thing I was able to think of was to use several index trees in order to reduce the complexity of the algorithm. First index tree is counting the number of nodes and can process queries of the kind: how many nodes are to the right of xi in logarithmic time. Lets call this number NR. The second index tree can process queries of the kind: what is the sum of distances from all the points to the right of a given x. The distances are counted towards a fixed point that is guaranteed to be to the right of the rightmost point, lets call its x XR.Lets call this number SUMD. Then the sum of the distances to all points to the right of our point can be found that way: NR * dist(Xi, XR) - SUMD. Then all these contribute (NR * dist(Xi, XR) - SUMD) *Pi to the result. The same for the left points and you get the answer. After you process the ith point you add it to the index trees and can go on.
Edit: Here is one article about Biary index trees: http://community.topcoder.com/tc?module=Static&d1=tutorials&d2=binaryIndexedTrees
This is the direct connections problem from codesprint 2.
They will be posting worked solutions to all problems within a week on their website.
(They have said "Now that the contest is over, we're totally cool with everyone discussing their solutions to the problems.")

Resources