Розбір
Let denote the expected time to escape the room to a safe place. This time is expressed as follows:
where is the time to reach a safe place if the -th door is chosen. It is clear that if . If , then to escape, one must first spend time to return to the room, and then spend time for another attempt to exit. Thus, in this case, .
As a result, we obtain a linear equation in terms of . This equation can be constructed and solved in time, where is the number of doors.
Example
Let’s consider the first test. Create an equation:
where from or .
Algorithm realization
We'll solve the linear equation as follows. All terms containing the multiplier will be moved to the left side, and their coefficients will be stored in the variable . All constants will be collected on the right side and stored in the variable . Initially, set and .
Read pairs of numbers and .
If , then increase the right side of the equation by .
If , a term will appear on the right side of the equation. In this case, we should add to the right side and subtract from the left side.
left = 1.0; right = 0.0; scanf("%d",&n); for(j = 0; j < n; j++) { scanf("%lf %lf",&x, &p); if (x < 0.0) { left -= p; right += p * (-x); } else right += x * p; }
We obtained the equation . If , then it is impossible to escape from the room (there is no door leading to a safe place). Otherwise, the sought time can be calculated as .
if (left > 0.0) { res = right / left;
Print the answer. The variable corresponds to the test number.
printf("Case %d: %.2lf\n",i,res); } else printf("Case %d: God! Save me\n",i);