#P5045. Water Transfer and Spider Rescue
Water Transfer and Spider Rescue
Water Transfer and Spider Rescue
There are two vertical pipes – a left one and a right one – each with a closed bottom and an open top. All pipes have a uniform cross–section so that when water is injected into one pipe alone, its water level rises at a rate of 1 cm per second. A water source is attached above the first (left) pipe. Water is supplied in three stages:
- From second 0 to second 2 the water is injected into the left pipe at a rate which causes a 1 cm/s rise.
- From second 3 to second 5 the water is injected into the right pipe at the same rate (if injected into one pipe only, the water level in that pipe would rise 1 cm per second).
- From second 6 to second 9 the water is injected simultaneously into both pipes. However, because the flow is now split equally the effective water‐level rising rate in each isolated pipe is only 0.5 cm per second.
The two pipes may be connected by a number of horizontal connectors. Each connector is located at a fixed vertical position and can instantly transfer water from one pipe to the other once the water level in either pipe reaches the connector’s height. Once at least one connector is activated, the entire injections of both pipes add up in every connected pipe.
In other words, if no connector is activated then the water level in each pipe is determined solely by the water injected directly into that pipe. But as soon as the water level in either pipe becomes greater than or equal to the smallest connector height, all water (from both pipes) flows into both pipes so that the effective water level in each pipe is the sum of the water injected into the left pipe and the water injected into the right pipe.
A spider named Willy is hanging inside one of the pipes at a certain height. (To model a slight margin, the spider’s effective position is a tiny bit above the given number – so the water must exceed the given value to "touch" the spider.)
Your task is: Given the positions (in cm) of the connectors (if any) and the location of Willy (which pipe and at which height in cm), determine the minimum time (in seconds) when the water level in that pipe becomes strictly greater than Willy’s height.
Note: If there are no connectors (i.e. no connection between the two pipes) then each pipe remains independent. When there is at least one connector, consider only its smallest height – if the water level in either pipe reaches that connector then both pipes instantly share water so that the effective water level in each becomes the sum of the water injected into both pipes.
For example, if a connector is located at 2 cm and Willy is in the left pipe at 4 cm, then although the left pipe alone would only reach 2 cm by second 2 and would have no injection between seconds 3 and 5, the water injected into the right pipe during seconds 3–5 (which would raise its own level to 3 cm if isolated) will be transferred, so that by second 5 the left pipe’s effective water level becomes 2+3 = 5 cm – therefore, the water touches Willy at second 5.
inputFormat
The input is given as follows:
- The first line contains an integer
m
(m ≥ 0) – the number of connectors between the two pipes. - If m > 0, the second line contains m space–separated numbers representing the vertical positions (in cm) of the connectors. (You may assume these values are given in strictly increasing order.) If m = 0, this line is omitted.
- The next line contains a character and a number separated by a space. The character is either
L
orR
, indicating whether Willy is in the left or right pipe; the number (a positive real number) is Willy’s height position in cm.
Time is measured in seconds. The water injection schedule is fixed as follows:
- Seconds 0–2: water is injected into the left pipe (1 cm/s increase if isolated).
- Seconds 3–5: water is injected into the right pipe (1 cm/s increase if isolated).
- Seconds 6–9: water is injected simultaneously into both pipes (0.5 cm/s increase in each if isolated).
When the pipes are not yet connected by a connector (i.e. the water level in both pipes is less than the smallest connector height), the water level in each pipe equals the water injected into that pipe. Once either pipe reaches a water level ≥ (equal to or greater than) the smallest connector height, the pipes instantly become connected – thereafter the effective water level in each pipe equals the sum of the water injected into both pipes.
You may assume that the water stops being injected after second 9.
outputFormat
Output a real number (in seconds) which is the minimum time when the water level in Willy’s pipe becomes strictly greater than Willy’s height. Print the answer with at least 6 decimal places.
sample
0
L 1
1.000000