PS/Baekjoon Online Judge

[백준 15120] Unloaded Die [Python]

kimyoungrok 2024. 9. 16. 01:55
728x90

문제

Consider a six-sided die, with sides labeled 1 through 6. We say the die is fair if each of its sides is equally likely to be face up after a roll. We say the die is loaded if it isn’t fair. For example, if the side marked 6 is twice as likely to come up as than any other side, we are dealing with a loaded die.

For any die, define the expected result of rolling the die to be equal to the average of the values of the sides, weighted by the probability of those sides coming up. For example, all six sides of a fair die are equally likely to come up, and thus the expected result of rolling it is (1 + 2 + 3 + 4 + 5 + 6)/6 = 3.5.

You are given a loaded die, and you would like to unload it to make it more closely resemble a fair die. To do so, you can erase the number on one of the sides, and replace it with a new number which does not need to be an integer or even positive. You want to do so in such a way that

  • The expected result of rolling the die is 3.5, just like a fair die.
  • The difference between the old label and the new label on the side you change is as small as possible.

입력

The input consists of a single line containing six space-separated nonnegative real numbers v1 . . . v6, where vi represents the probability that side i (currently labeled by the number i) is rolled.

It is guaranteed that the given numbers will sum to 1.

출력

Print, on a single line, the absolute value of the difference between the new label and old label, rounded and displayed to exactly three decimal places.


풀이

합이 1인 주사위의 각 면이 나올 확률에 대해 1 ~ 6 까지의 가중치를 부여한 후 합을 구할 때 3.5가 맞는지 확인하는 문제다.

만약 3.5가 아니라면, 3.5에서 총 합을 뺀 값에 대해 

차이가 가장 작도록 하기 위해 6개의 수 중 가장 큰 수로 나눈 값의 절대값을 출력하면 된다. 


소스코드

보기


출처

https://www.acmicpc.net/problem/15120

728x90