Problem Statement | |||||||||||||
| N friends (numbered from 0 to N-1) play a game called Mafia. The exact rules of the game are not important for this problem. What's important is that at some point in the game they will need to choose one player who will
lose and leave the game. It is known that some players have a definite opinion on who should lose. Their opinions are given in the vector <int> decisions, where each element corresponds to a single opinion and is the number of a player who should lose according to that opinion. All opinions in decisions belong to different players. If decisions contains less than N elements, then all other players do not have a definite opinion on who should lose. In order to determine who will lose, one or more rounds of voting will be conducted. In each round, there's a set of players for whom the players are allowed to vote. The players in this set are called "vulnerable". It's impossible to vote for players not in this set. Before the first round of voting, all N players are included in this set. All N players will vote in each round. The voting is held according to the following scheme:
| |||||||||||||
Definition | |||||||||||||
| |||||||||||||
Notes | |||||||||||||
| - | The exact numbers of people to whom the opinions in decisions belong are not relevant in this problem. | ||||||||||||
| - | It is possible that a player will decide to vote against himself (see example 0). It is also possible that a player will have to vote against himself (if he is one of "vulnerable" players who have the smallest number of votes in the current round). | ||||||||||||
| - | The returned value must have an absolute or relative error less than 1e-9. | ||||||||||||
Constraints | |||||||||||||
| - | N will be between 2 and 500, inclusive. | ||||||||||||
| - | decisions will contain between 1 and min(N, 50) elements, inclusive. | ||||||||||||
| - | Each element of decisions will be between 0 and N-1, inclusive. | ||||||||||||
Examples | |||||||||||||
| 0) | |||||||||||||
| |||||||||||||
| 1) | |||||||||||||
| |||||||||||||
| 2) | |||||||||||||
| |||||||||||||
| 3) | |||||||||||||
|
| |||||||||||||
【题解】
官网说的很详细。
对于最初局面有两种情况:
1、每个人都有有且仅有一票,则肯定无解。
2、有些人得到了两票或者更多,则那些最初得票最多的成为输家的概率更大。(显而易见)
所以答案=1/(最初的票最多的人的数量)
【代码】
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
using namespace std;
class MafiaGame
{
public:
double probabilityToLose(int, vector <int>);
};
double MafiaGame::probabilityToLose(int n, vector <int> decisions)
{
int i,Max,sum,c;
int a[505]={0};
for (i=0;i<decisions.size();i++)
a[decisions[i]]++;
Max=0;sum=0;
for (i=0;i<n;i++)
if (a[i]>Max)
{
sum=1;
Max=a[i];
}
else if (a[i]==Max)
{
sum++;
}
if (sum==decisions.size()) return 0;
c=sum;
while (1)
{
if (c==0) return 0;
if (c==1) return 1.0/sum;
c=n%c;
}
}
//Powered by [KawigiEdit] 2.0!
本文介绍了一种名为Mafia的游戏中的投票策略问题。在每轮投票中,玩家根据已有的意见进行投票,若无法决出输家则可能无限循环。文章提供了一个算法,用于计算每个玩家成为输家的最大概率。
663

被折叠的 条评论
为什么被折叠?



