TopCoder problem "DartThrow" used in TCHS SRM 19 (Division I Level One)



Problem Statement

    

Pistol Pete, the neighborhood game dealer, has offered to let you play his newest game. Pete's going to let you throw a dart at his wall containing several targets. Depending on what target you hit, you will win a fabulous prize!

You've been practicing a game like this lately, and you figure it's right up your alley. You will hit the target you aim at with prob percent probability. When you miss the target you aim at, you will hit either of the adjacent targets with equal probability. Since the targets are arranged in a circular fashion, if you miss target 0, then you will either hit target 1 or target n-1, where n is the total number of targets. See example 0 for clarification.

Pete has given you a int[] payout, in which the i-th element corresponds to the payout of hitting target i. You will also be given prob, the percent probability your dart will hit the target you aim for. The total number of targets is equal to the number of elements in payout. Given this information, return a double with the average payout that you will get from Pete if you play optimally.

 

Definition

    
Class:DartThrow
Method:bestScore
Parameters:int[], int
Returns:double
Method signature:double bestScore(int[] payout, int prob)
(be sure your method is public)
    
 

Notes

-A return value with either an absolute or relative error of less than 1.0e-9 is considered correct.
 

Constraints

-payout will contain between 3 and 50 elements, inclusive.
-Each element of payout will be between 0 and 100, inclusive.
-prob will be between 0 and 100, inclusive.
 

Examples

0)
    
{10,40,50}
80
Returns: 45.0
In this case, you have three options:
  • Aim at 10: Score = 10(0.8) + 50(0.1) + 40(0.1) = 17.0
  • Aim at 40: Score = 40(0.8) + 10(0.1) + 50(0.1) = 38.0
  • Aim at 50: Score = 50(0.8) + 40(0.1) + 10(0.1) = 45.0
Of the three options, the best score is 45, and so we return it.
1)
    
{10,40,50}
61
Returns: 40.25
This time, you don't throw as well. In this case, it is best to aim for the 50, scoring 50(0.61) + 40(0.195) + 10(0.195) = 40.25.
2)
    
{20,1,18,4,13,6,10,15,2,17,3,19,7,16,8,11,14,9,12,5}
60
Returns: 13.4
A standard dartboard.
3)
    
{20,1,18,4,13,6,10,15,2,17,3,19,7,16,8,11,14,9,12,5}
20
Returns: 15.4
Sometimes missing can be a good thing.
4)
    
{16,99,96,26,71,9,89,43,11,41,58,84,27,8,17,54,26,36,87}
66
Returns: 84.61

Problem url:

http://www.topcoder.com/stat?c=problem_statement&pm=6786

Problem stats url:

http://www.topcoder.com/tc?module=ProblemDetail&rd=10071&pm=6786

Writer:

connect4

Testers:

PabloGilberto , brett1479 , Olexiy

Problem categories:

Math