海盗分金问题:有五个海盗,劫掠了100两金子,需要分 赃。办法是抓阄,盗亦有道。抓到第一个阄的人,可以先提出一个分 配方案,如果他的方案被一半以上的人同意, 就照他的方案分金子,否则,第一个人就要 被杀掉。余下的人也照此办理。我们的问题是:如果你是第一个人,你 会提出怎样的分配方案?为了分析问题更确定,我们假定每个人 都是追求自己利益极大化的人。
with a as
(
select cast(help_topic_id as SIGNED) as n from mysql.help_topic where help_topic_id <101 order by help_topic_id desc
)
, max1 as
(
select max(n) as max1 from a
)
,max2 as
(
select
p2.n as max2,p1.n as max1 from a p1,a p2
where p1.n + p2.n = 100
and p1.n >=(select max1 from max1)
)
,max3 as
(
select/*+ JOIN_ORDER(P3,P2,P1)
JOIN_SUFFIX(P1)
NO_BNL() */
p3.n as max3,p2.n as max2,p1.n as max1 from a p1,a p2,a p3,max2
where p1.n+p2.n+p3.n = 100
and p3.n +p2.n <= 100
and sign(p2.n- max2) + sign(p1.n - max1) >=0
limit 1
)
,max4 as
(
select
/*+ JOIN_ORDER(p4,P3,P2,P1)
JOIN_SUFFIX(P1)
NO_BNL() */
p4.n as max4,p3.n as max3,p2.n as max2,p1.n as max1
from a p1,a p2 ,a p3,a p4,max3
where p1.n+p2.n+p3.n + p4.n = 100
and p4.n + p3.n +p2.n <= 100
and sign(p3.n - max3) + sign(p2.n - max2) + sign(p1.n - max1) >0
limit 1
)
,max5 as
(
select
/*+ JOIN_ORDER(p5,p4,P3,P2,P1)
JOIN_SUFFIX(P1)
NO_BNL() */
p5.n as max5,p4.n as max4,p3.n as max3,p2.n as max2,p1.n as max1
from a p1,a p2 ,a p3,a p4,a p5,max4
where p1.n+p2.n+p3.n + p4.n + p5.n = 100
and p5.n + p4.n + p3.n +p2.n <=100
and sign(p4.n - max4) + sign(p3.n - max3) + sign(p2.n - max2) + sign(p1.n - max1) >=0
limit 1
)
SELECT * FROM MAX5
max5 max4 max3 max2 max1
97 0 1 0 2