【HDU1538】A Puzzle for Pirates(经典的海盗问题)

【题目】

Description

A bunch of pirates have gotten their hands on a hoard of gold pieces and wish to divide the loot. They are democratic pirates in their own way, and it is their custom to make such divisions in the following manner: The fiercest pirate makes a proposal about the division, and everybody votes on it, including the proposer. If 50 percent or more are in favor, the proposal passes and is implemented forthwith. Otherwise the proposer is thrown overboard, and the procedure is repeated with the next fiercest pirate. 
All the pirates enjoy throwing one of their fellows overboard, but if given a choice they prefer cold, hard cash, the more the better. They dislike being thrown overboard themselves. All pirates are rational and know that the other pirates are also rational. Moreover, no two pirates are equally fierce, so there is a precise pecking order ― and it is known to them all. The gold pieces are indivisible, and arrangements to share pieces are not permitted, because no pirate trusts his fellows to stick to such an arrangement. It‘s every man for himself. Another thing about pirates is that they are realistic. They believe ‘a bird in the hand is worth two in the bush‘ which means they prefer something that is certain than take a risk to get more, where they might lose everything.

For convenience, number the pirates in order of meekness, so that the least fierce is number 1, the next least fierce number 2 and so on. The fiercest pirate thus gets the biggest number, and proposals proceed in the order from the biggest to the least.

The secret to analyzing all such games of strategy is to work backward from the end. The place to start is the point at which the game gets down to just two pirates, P1 and P2. Then add in pirate P3, P4, ... , one by one. The illustration shows the results when 3, 4 or 5 pirates try to divide 100 pieces of gold.

Your task is to predict how many gold pieces a given pirate will get.

Input

The input consists of a line specifying the number of testcases, followed by one line per case with 3 integer numbers n, m, p. n (1 ≤ n ≤ 10^4) is the number of pirates. m (1 ≤ m ≤ 10^7) is the number of gold pieces. p (1 ≤ p ≤ n) indicates a pirate where p = n indicates the fiercest one.

Output

The output for each case consists of a single integer which is the minimal number of gold pieces pirate p can get. For example, if pirate p can get 0 or 1 gold pieces, output ‘0‘. If pirate p will be thrown overboard, output ‘Thrown‘.

Sample Input

3
3 100 2
4 100 2
5 100 5

Sample Output

0
1
98

【题意】

  这是一个经典问题,有n个海盗,分m块金子,其中他们会按一定的顺序提出自己的分配方案,如果50%以上的人赞成,则方案通过,开始分金子,如果不通过,则把提出方案的扔到海里,下一个人继续。

【分析】

  自己先从小到大玩一遍这个游戏,就能找到一些规律。

  分够贿赂和不够贿赂两种情况。抓特点:一个人如果将要死,他会支持前面的人保证自己不死。如果能得到更多的钱,他会更加支持。如果得到同样的钱,他乐于把前面的人扔下水。对于

够钱贿赂的情况,他会给与自己同奇偶的人。这样票数也够,他也会支持你。(具体为什么思考一下就知道了)。对于不够钱贿赂,要考虑到人不希望死这个情况,找到规律,决策者总是

2*m+2^k(k为任意整数),可是他具体贿赂谁是不确定的,所以除了一些在前面的必死的人,其他人的ans都为0。

  具体看大神blog:http://blog.csdn.net/acm_cxlove/article/details/7853916

代码如下:

 1 #include<cstdio>
 2 #include<cstdlib>
 3 #include<cstring>
 4 #include<iostream>
 5 #include<algorithm>
 6 using namespace std;
 7 #define Maxn 1010
 8
 9 void ffind(int x,int y,int z)
10 {
11     if(x<=2*y+1)
12     {
13         if(z==x) printf("%d\n",y-(x-1)/2);
14         else if((x-z)%2==0) printf("1\n");
15         else printf("0\n");
16     }
17     else
18     {
19         int mx;
20         for(int i=0;(2*y)+(1<<i)<=x;i++) mx=2*y+(1<<i);
21         if(z>mx) printf("Thrown\n");
22         else printf("0\n");
23     }
24 }
25
26 int main()
27 {
28     int T;
29     scanf("%d",&T);
30     while(T--)
31     {
32         int n,m,p;
33         scanf("%d%d%d",&n,&m,&p);
34         ffind(n,m,p);
35     }
36     return 0;
37 }

[HDU1538]

2016-04-25 13:21:52

时间: 2024-08-05 07:42:01

【HDU1538】A Puzzle for Pirates(经典的海盗问题)的相关文章

hdu-1538 A Puzzle for Pirates

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1538 题目类型: 模拟 题意概括: 有1~n,n个海盗,m块金子,第n个海盗要提出一个分金方案,如果有一半以上的人同意,就立刻分金,反之就将这个人扔下水里,问所有情况都是最优解的情况下,要给第p个海盗分多少金? 解题思路: 如果只有一个人,所以肯定是给自己. 如果有两个人,那么自己将所有的钱都给自己,那么自己也同意分配,就可以得到一半以上的人的同意. 如果有三个人,第一个人知道如果不同意三号,那么

HDU 1538 A Puzzle for Pirates 经典海盗分金币

题目:这是一个经典问题,有n个海盗,分m块金子,其中他们会按一定的顺序提出自己的分配方案,如果50%以上的人赞成,则方案通过,开始分金子,如果不通过,则把提出方案的扔到海里,下一个人继续. 首先我们讲一下海盗分金决策的三个标准:保命,拿更多的金子,杀人,优先级是递减的. 同时分为两个状态稳定状态和不稳定状态:如果当n和m的组合使得最先决策的人(编号为n)不会被丢下海, 即游戏会立即结束, 就称这个状态时"稳定的". 反之, 问题会退化为n-1和m的组合, 直到达到一个稳定状态, 所以乘

HDU 1538 A Puzzle for Pirates (海盗分金问题)

A Puzzle for Pirates Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 508    Accepted Submission(s): 167 Problem Description A bunch of pirates have gotten their hands on a hoard of gold pieces a

[Puzzle] 5 Pirates and 100 Gold Coins

https://wesleydeng.iteye.com/blog/1186457 https://www.geeksforgeeks.org/puzzle-5-finding-the-poisoned-wine/ 原文地址:https://www.cnblogs.com/lawrenceSeattle/p/10281548.html

博弈论类题目小结——转载

出处http://blog.csdn.net/ACM_cxlove?viewmode=contents    by---cxlove 首先当然要献上一些非常好的学习资料: 基础博弈的小结:http://blog.csdn.net/acm_cxlove/article/details/7854530 经典翻硬币游戏小结:http://blog.csdn.net/acm_cxlove/article/details/7854534 经典的删边游戏小结:http://blog.csdn.net/acm

【补题】组队训练第一场

本来想一次补完的(正常应该补两次的)但是晚上玩dota2和rpg去了然后--又堕落了啊. 好吧进入正题,题目按照从易到难的顺序(个人感觉)其他题目现在对我来说太难了,以后再补. A题 ZOJ 3878 水题,可以用map一个一个对应,比较麻烦就用了两个字符数组,要注意\和"要转义. 1 #include<cstdio> 2 #include<cstring> 3 #include<iostream> 4 #include<algorithm> 5

加勒比海盗1英文剧本

[完整版]加勒比海盗一全英文对白完整版 这是加勒比海盗一的全部英文对白,从开头到结束,目的是为了方便那些想用加勒比海盗练习听力的朋友,也可以作为对剧情的参考,加注了人名,部分无法考证名字的路人以MAN替代 <PIRATES OF THE CARIBBEAN>--THE CURSE OF THE BLACK PEARL [Elizabeth singing softly]:Drink up,me hearties,yo ho; We kidnap and ravage and don't giv

(转)一些经典的计算机书籍

以下列表中的计算机书籍(中文版)来自微博:@程序员的那些事 粉丝的推荐.按推荐次数,从高到低往下排.如果大家还有其他计算机相关的经典书籍推荐,请在评论中留言,或者在这条微博的评论中留言,我们将继续扩充这个列表.1. 算法导论(第2版)2. 代码大全(第2版)3. C++ Primer中文版(第4版)4. 设计模式:可复用面向对象软件的基础5. 浪潮之巅6. Java编程思想(第4版)7. Java核心技术 卷1:基础知识8. Java核心技术 卷2:高级特性9. 人月神话10. Linux内核编

经典海量jQuery插件

海量的jQuery插件帖,很经典,不知道什么时候开始流传,很早以前就收藏过,为了工作方便还是发了一份放在日志里面. 其中有些已经无法访问,或许是文件移除,或许是被封锁.大家分享的东西,没什么特别的可说的,唯有感谢无私分享的人们. 猫嗔提醒大家在使用的时候注意jQuery包的版本问题,最好是使用相同的版本,因为使用了高版本很有可能出现或多或少的问题.并且其中英文插件在转换成中文时注意编码,推荐utf-8. jQuery插件-文件上传(File upload)Ajax File Upload.jQU