#P3824. Maximum Swimming Pool Area Probability
Maximum Swimming Pool Area Probability
Maximum Swimming Pool Area Probability
In a rectangular sea grid of dimensions (meters), each unit square (of size ) is independently safe with probability and dangerous with probability . The bottom side of the grid corresponds to a private beach. A swimming pool is defined as a subgrid (i.e. a rectangle) that (1) is entirely safe, (2) is a contiguous block of unit squares, and (3) has its bottom side flush with the bottom of the grid (i.e. it is adjacent to the beach).
For any configuration of safe/dangerous cells, let the achievable swimming pool areas be determined by selecting any contiguous block of columns and considering the number of consecutive safe cells starting from the bottom in each chosen column; the swimming pool area is the product of the number of columns and the minimum number of consecutive safe cells (from the bottom) among those columns. Among all valid choices, the one with the maximum area will be chosen. For example, when , if the bottom rows have a pattern such that one may choose a rectangle along the bottom row (area ) or a rectangle in a certain column (area ), the chosen swimming pool is the one with area because it is maximum.
Assume that in each column, the number of consecutive safe cells from the bottom (i.e. the column height) is a random variable with the following distribution: for ,[
P(h=r)=q^r(1-q),]and[
P(h=1001)=q^{1001}.]These random variables are independent across the columns. Hence, the maximum safe rectangle (which touches the beach) has an area equal to the maximum over all contiguous intervals of columns of ( L \times \min_{\text{in the interval}}, h_i ), where is the length (number of columns) of the interval.
Your task is: given , , and an integer , compute the probability that the maximum swimming pool area is exactly . That is, if we denote by the maximum area over all valid (bottom‐touching) subgrids, you need to compute
[
P(M = K).]
Note: All formulas must be typeset in (\LaTeX) format. You may assume that is small (for example, ) so that an algorithm with state‐space dynamic programming based on the classical largest rectangle in a histogram (using a stack) can be used. In this approach, one processes the columns one by one. For a new column with value , the algorithm first pops from the stack until the top of the stack has a value not greater than , updating the current maximum rectangle area using the formula
[
\text{area} = h \times (i - j),]
where is the height popped and is the index stored with it. After processing all columns, the stack is flushed to update the final maximum area. The answer is then given by the probability (evaluated over the randomness of the ’s) that this maximum area equals exactly .
inputFormat
The input consists of a single line containing three values:
- An integer $N$ ($1 \le N \le 5$) representing the number of columns of the grid.
- A floating‐point number $q$ ($0 \le q \le 1$) representing the probability that a given unit square is safe.
- An integer $K$ ($0 \le K \le 1001\cdot N$) representing the target swimming pool area.
All numbers are separated by spaces.
outputFormat
Output a single floating–point number: the probability that the maximum swimming pool area (as defined above) is exactly . The answer will be accepted if it is correct within a relative or absolute error of .
sample
5 0.5 4
0.####