機率最大骰子總和 Topcoder SRM 536 DIV1 第2題

題目描述spa

有n個骰子,每一個骰子的面數不等,第i個骰子有d[i]面,1~d[i]朝上的機率均等。問擲出這n個骰子,把朝上的點求和,問機率最大的和是多少?若是機率相等,取和最小的。骰子面數的取值爲1-10^9。ip


解題思路
取全部可能出現的點數最中間那個,機率必定是最大的。可能出現的點數爲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.
相關文章
相關標籤/搜索