Funny pirate gold sub-questions, do not learn algorithms do not deserve to be a pirate, a

In recent days to see a very interesting game associated Something about to share with you today, and will explain in detail the optimal solution to a final question, and I'm okay by this question have said something about recursion.

Problem Description

There are five pirates, won the 100 gold coins, so they want to discuss a method to distribute gold coins. Deliberations as follows:

5 turns made by a pirate allocation scheme, the following rules

1, if more than half of the pirates (including the author) agreed that the program is distributed according to the program.

2, if the number of the program (including the author) agree to less than half, the author is to be thrown into the sea to feed the fish, the rest of the pirates continue to negotiate distribution.

3, the pirates are absolutely rational to own gold coins as possible to obtain a multi-purpose. But in the case of equal income, it would tend to presenter overboard.

Q: The first pirate should propose how the distribution plan, in order to ensure that they are neither being thrown into the sea, but also make maximize their own interests?

Solve the problem

Do some assumptions and reminders

For convenience of later description, we assume that the order of rotation scheme is proposed: Pirates 5, Pirates 4, Pirates 3, Pirates 2, Viking 1; that is, initially proposed allocation scheme 5 by the pirate, the pirate at the last 1

In addition, we must pay attention to the last condition, each pirate is absolutely rational and in equal earnings, would tend to presenter thrown into the sea .

In front of the high-energy, start crap, you play your various suspect

Well, now if you are Viking 5, how would you allocate the gold in order to make available as much as possible, and will not be thrown into the sea to feed the fish it?

To tell the truth, first saw this question, a bit impossible to start, mind Tete What a mess, because do not know how to prove my allocation scheme that allows more than half of the pirates must support me , or evenly distributed? Or I them a little bit less? Or I them a little bit less (this will not immediately be thrown sea)?

You can own a few minutes to think, oh, I want to see if you can come out?

In fact, let others agree to our thoughts, we have to know the enemy confidant , to win every battle. In other words, the pirates to give 5 distribution plan must be based on Pirates 4 of allocation scheme to; that is, 5 pirates have to assume that they have been thrown into the sea, then, Pirates 4 will be how to allocate it? Then allocation scheme according to Pirates 4, Pirates 5 in order to give his distribution plan.

Similarly, pirates have based allocation method 4 3 pirate, the pirate 3 based pirates have 2, so

I do not understand? It does not matter, here I give an example that you immediately understand

Layer by layer break

1. Only two pirates

Now, we assume that only two Pirates: Pirates 1 and Viking 2, this time you should be aware of the allocation result, right?

Obviously, no matter what program proposed Pirates 2, Pirates 1 will be directly rejected, so the pirates could get 1 all the coins, that is, when only two Pirates, Pirates 2 no matter how pleasing Viking 1, the final results are is feeding the fish to the sea, the distribution was as follows

2, only three cases of piracy

This time suddenly jumped out of a Pirate 3, spoils also participate in this event, this time the pirates how to allocate 3?

In fact, very simple, just 3 Viking 1 and Viking glimpse listened dialogue Pirates 2, knowing if they have been thrown into the sea, then Pirates 2 must also be thrown into the sea, the pirate 3 know yourself no matter what method is proposed, pirates 2 must agree , so pirates 3 can make the following allocation:

Pirates 3: 100 gold

Pirates 2: 0 gold
Pirates 1: 0 gold.

That is, as long as Pirates 3 Pirates 2 support and form 2: 1 situation, you can pirate 3 Wynn, without taking into account the pirate 1 support. Therefore, the final allocation result is as follows

One might say that we do not have to allocate a little advantage with pirate 2? For example assigned to a pirate 2 gold, 3 condition there is a rule: In case of equal income, the pirates would tend to the author into the sea , and the answers are not required, although pirate 2 is not assigned to the gold, but he did not It was thrown into the sea, which is the biggest advantage of

看到这里,你是不是也知道如果是 4 个海盗或者 5 个海盗,你也会分配了?我相信你大概率知道怎么分配了,不过我还是要讲一下,因为后面随着人数的增加,也并没有你想的那么简单,并且后面还会和递归算法串讲一下。

3、只有4个海盗的情况

这个时候又突然蹦出个海盗4,并且海盗4是已经知道了海盗3的分配方案了,这个时候海盗4只需要获得其中两个人的支持即可。

如何获得其中两个人的支持?

这很容易,拿点钱给海盗1和海盗2就可以了,海盗4可以提出如下分配方案

海盗4:98个

海盗3:0个

海盗2:1个

海盗1:1个

注意,在收益相等的情况下,海盗们会倾向把提出者扔到海里,所以海盗4必须在海盗3的基础上,多给海盗1和海盗2一个金币,这个时候海盗1和海盗2一定会支持海盗4,此时的局面是 3:1(支持:反对的人数),因此只有4个人的情况下,分配方案为:

有人可能会问,为啥要拉拢贿赂海盗1和海盗2,咱能不能尝试贿赂下海盗3?

答是咱贿赂不起,如果你有这样的想法,只能说明你不是一个合格的海盗!

4、只有5个海盗的情况

如果有5个海盗,其实海盗5和海盗4一样,只需要拉拢两个人就可以了,那要拉拢谁呢?

这也不难,首先必须得贿赂海盗3,给他一个金币就可以了,其次我们在海盗2或者海盗1之中拉拢一个人即可,想要拉拢哪一个,随你开心,所以海盗5可以提出如下方案:

到这里,就已经分配完毕了,是不是觉得很不可思议?原本还怕自己无论提出啥方案,都会被扔进海里,结果是如此出人意料。以后和别人分赃,是时候拿出这个规则了

问题的核心

我觉得这个问题,非常像我们平时学算法中的递归,例如对于递归方式:f(n) = f(n - 1) + f(n - 2),并且给出初始条件 f(1) = f(2) = 1。

那么如果一开始要你算 f(n),你也是无从下手的,f(n) 必须基于 f(n - 1) 和 f(n - 2),类比于这个海盗问题的话,

1、n 相当于海盗的个数。

2、只有两个海盗时,我们可以非常容易着给出方案,相当于初始条件 f(1) = f(2) = 1

3、f(n) = f(n - 1) + f(n - 2) 相当于海盗之间定下的规则

所以呢,有时候遇到这种看似很复杂的博弈问题,不妨先从问题的规模尽量小处理起,后面在逐一增加问题的规模。

不妨来个拓展

如果又突然冒出了一个海盗呢?也就是在一共有 6 个海盗的情况下,该如何处理呢?

有没有觉得,从 5 个到 6 个,是一个分水岭?因为从 5 个开始,就有多种分配方案,这个时候就更加考验你的逻辑了。

不过,对于 6 个,我姑且给大家分析一下,当然,只是我认为是这样,其实我看过别人的也有不同的版本。下面我来分析下海盗6可以给出的策略:

首先,我们必须拉拢 3 个人,显然,我们是不可能会拉拢海盗5的,因为咱拉不起。因为我们会从海盗1 ~ 海盗4中考虑。

1、首先我们必须拉拢海盗4,因为他最容易贿赂,给他 1 个金币即可。

2、接着,我们拉拢海盗3,给他两个金币即可

此时,我们已经拉拢了海盗3和海盗4,接下来我们需要在海盗1和海盗2中选一个即可,那么问题来了,该给海盗1和海盗2他们多少,他们才愿意同意你的方案?

显然,如果我们给海盗1分配 3 个金币,海盗2分配 0 个,显然海盗1一定会同意。

但是,真的需要给海盗1分配 3 个吗?如果我给他 2 个金币,他会同意吗?

答是会的,为什么呢?因为在海盗5的方案中,要么是海盗1获得2个,要么是海盗2获得2个,所以对于海盗1来说,海盗5会不会贿赂他,存在不确定性,因此作为一个理智的海盗,海盗1是会同意海盗6给他2个金币的方案的。

因此海盗6可以提出如下方案

事实上,也可以从概率上来证明,在海盗5的方案中,由于海盗1和海盗2存在不确定性,我们可以进行折算,折算成海盗1和海盗2各自获得一个金币,所以我们给他两个金币,他必须得同意

当然,如果海盗6要给海盗2分配2个金币,然后给海盗1分配 0 个金币,也是可以的。**总的来说就是,我们可以从海盗1,海盗2,海盗3中选出两个人,然后每人给他两个金币即可以,这样的组合有三种,因此海盗6可以由如下 3 种方案:

分析到这里,就已经结束了,如果又蹦出一个海盗呢?也就是说一共有 7 个海盗呢?

剩下的就交给你了,鉴于篇幅,我就不继续分析了。

最后

这么多年以来,今年的春节,估计是最特殊的春节了,想必大家都在家里呆着,今天这道题也是我花了整整一个上午写的,希望能够让你有所收获,或者能够可以给给解解闷,我们下期再见!

老铁,要不点个赞再走可好?么么哒

1、给俺点个赞呗,可以让更多的人看到这篇文章,顺便激励下我,嘻嘻。

2、老铁们,关注我的原创微信公众号「帅地玩编程」,专注于写算法 + 计算机基础知识(计算机网络+ 操作系统+数据库+Linux)。

保存让你看完有所收获,不信你打我。后台回复『电子书』送你一份精选电子书大礼包,包含各类技能的优质电子书。

作者简洁

作者:大家好,我是帅地,从大学、校招一路走来,深知算法计算机基础知识的重要性,所以申请了一个微星公众号『帅地玩编程』,专业于写这些底层知识,提升我们的内功,帅地期待你的关注,和我一起学习。 转载说明:未获得授权,禁止转载

Guess you like

Origin www.cnblogs.com/kubidemanong/p/12238045.html