Barrel
Time limit1sMemory limit128 MB
Simulate cubes floating or sinking in a barrel of water and compute the final water level using a binary search or iterative physics-based calculation.
- Level
Medium6 of 10
- Topics
- Binary search, Simulation, Math
- Solved
- No attempts yet
Problem
Some amount of water is poured into a barrel, then several cubes of different size and density are placed into the water. Finally, a lid is put on the barrel and pushed down until it rests on the rim of the barrel.

Write a program that computes the resulting water level in the barrel.
You may assume that:
- the density of water is 1.0,
- the effect of air can be neglected,
- every cube fits completely inside the barrel,
- the cubes do not rotate and do not touch one another.
Input
The first line contains three real numbers: the base area of the barrel , the height of the barrel , and the volume of water .
The second line contains the number of cubes .
Each of the following lines contains two real numbers describing one cube: the side length and the density .
Output
Print a single line containing the resulting water level, rounded to exactly four decimal places.