Splitting Game Levels
InterviewTime limit2sMemory limit512 MB
Partition n levels into k consecutive groups to minimize the expected total time of a random coin-draw process, and print it to six decimals.
- Level
Hard8 of 10
- Topics
- Dynamic programming, Divide and conquer, Probability, Prefix sum
- Solved
- No attempts yet
Problem
Areum plays a computer game with levels, numbered through .
The levels are split into groups. Each group consists of consecutive levels, every level belongs to exactly one group, and no group is empty.
The game repeats the following process.
- If every level is cleared, the game ends. Otherwise the system finds the first group that still has at least one uncleared level. Call this group .
- The system prepares an empty bag for coins. One coin represents one level, and several coins may represent the same level.
- For every already cleared level in group , the system puts coins representing level into the bag.
- Let be the first uncleared level in group . The system puts coins representing level into the bag.
- The system picks one coin from the bag uniformly at random and tells Areum the level that coin represents. Areum plays that level for one hour and always clears it. If the level was already cleared, the hour is still spent and nothing about the cleared levels changes.
The expected time until the game ends depends on how the levels are split. Given , and , write a program that computes the expected time for a split that makes it as small as possible.
Input
The first line contains and . (, )
The second line contains . ()
Output
Print the smallest expected time on one line, rounded to six digits after the decimal point. Pad with zeros so that six digits always follow the decimal point.
Hint
In the first example, putting level in one group and the remaining levels in the other group is optimal.
In the second example, splitting the levels into two groups of three is optimal.