概率最大骰子总和 Topcoder SRM 536 DIV1 第2题

题目描述

有n个骰子,每个骰子的面数不等,第i个骰子有d[i]面,1~d[i]朝上的概率均等。问掷出这n个骰子,把朝上的点求和,问概率最大的和是多少?如果概率相等,取和最小的。骰子面数的取值为1-10^9。


解题思路
取所有可能出现的点数最中间那个,概率一定是最大的。可能出现的点数为n ~ sum(d[i]),所以结果是(n+sum(d[i]))/2。但考虑到取最小的和,当前n-1个骰子的总点数还没 第n个骰子 出现在最中间的点数大时,答案为前n-1个骰子点数总和加1。

代码
 long long mostLikely(vector <int> dice)
 {
      long long ans = 0;
      long long res = 0;
      sort(dice.begin(), dice.end());
      for(int i=0; i<dice.size(); i++)
      {
          ans += dice[i];
          res = dice[i];
      }
      res = ans - res + 1;
      ans = (ans + dice.size()) / 2;
      return ans < res ? ans : res;
 }
 
附:题目原文
     Byteasar is playing a tabletop role-playing game with his friends. To determine the effectiveness of their heroes' actions the players use a rather unique set of dice. The i-th (0-based) die has dice[i] faces. They are numbered from 1 to dice[i] and the face number k shows k pips. When a die is cast, every face has equal probability to come out on top.

Byteasar's hero is now trying to unlock a safe containing treasure. In order to see if he succeeds, Byteasar has to guess a number M and then to roll all the dice. The safe will open if and only if M is precisely equal to the total number of pips on the topmost faces of all the dice. Obviously, the best strategy for Byteasar is to set M equal to the most probable outcome of the dice roll.

You are given the vector <int> dice. Return the value M such that the probability of the total number of pips being M is the highest possible. If there are multiple such values of M, return the smallest one.

你可能感兴趣的:(概率,topcoder)