【POJ】A New Stone Game(博弈论)
http://poj.org/problem?id=1740
题目大意就是,对于n堆石子,每堆若干个,两人轮流操作,每次操作分两步,第一步从某堆中去掉至少一个,第二步(可省略)把该堆剩余石子的一部分分给其它的某些堆。最后谁无子可取即输。
看了题解感觉太神了。
首先我们来分析:
当只有一堆时,先手必胜,直接一次取完即可
当有两堆时,分两种情况,一是两堆相同时,先手必输,因为无论先手怎样取,后手总有办法再次平衡两堆;而是两堆不同时,先手必胜,因为先手总有办法先平衡两堆,然后就像一的情况。
当有三堆时,先手总有办法平衡为两堆。(将最多的那堆部分,然后将剩余的补满另外两堆最少的)
当有偶数堆并且从小到大排序后两两相等(即1、2堆相等,3、4堆相等,5、6堆相等...),先手必输,因为无论先手怎样取,后手总能将堆数平衡为偶数堆且两两相等。
当有奇数堆时,先手必胜,因为将所有堆从小到大排序后,取最大的一堆并一定能够补满一一对应的堆平衡,成为第四种情况(将前边的堆的高度差映射到y轴,可以发现总和一定小于,注意是小于最高的那堆,所以一定能够平衡这些一一对应的堆)
当有偶数堆时,从小到大排序后有一一对应的堆不平衡时,先手必胜,因为先手总能先将这些偶数堆平衡,成为第四种情况,(最大的堆去掉至少一个后,将剩余石头与最小堆的高度差的石头移动到其它堆平衡,而且一定能够平衡!同上一种情况,映射到y轴即可)
所以我们只要判断当偶数堆时是否一一对应的石头一定平衡,如果是,那么先手必输,如果不是,先手必胜。
#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
#define rep(i, n) for(int i=0; i<(n); ++i)
#define for1(i,a,n) for(int i=(a);i<=(n);++i)
#define for2(i,a,n) for(int i=(a);i<(n);++i)
#define for3(i,a,n) for(int i=(a);i>=(n);--i)
#define for4(i,a,n) for(int i=(a);i>(n);--i)
#define CC(i,a) memset(i,a,sizeof(i))
#define read(a) a=getint()
#define print(a) printf("%d", a)
#define dbg(x) cout << #x << " = " << x << endl
#define printarr(a, n, m) rep(aaa, n) { rep(bbb, m) cout << a[aaa][bbb]; cout << endl; }
inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<'0'||c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0'&&c<='9'; c=getchar()) r=r*10+c-'0'; return k*r; }
inline const int max(const int &a, const int &b) { return a>b?a:b; }
inline const int min(const int &a, const int &b) { return a<b?a:b; } const int N=1000;
int a[N], n; int main() {
while(~scanf("%d", &n) && n) {
rep(i, n) read(a[i]);
if(n&1) puts("1");
else {
bool flag=0;
sort(a, a+n);
for(int i=0; i<n; i+=2) if(a[i]!=a[i+1]) { flag=1; break; }
if(flag) puts("1");
else puts("0");
}
}
return 0;
}
Description
At each step of the game,the player choose a pile,remove at least
one stones,then freely move stones from this pile to any other pile that
still has stones.
For example:n=4 and the piles have (3,1,4,2) stones.If the player
chose the first pile and remove one.Then it can reach the follow states.
2 1 4 2
1 2 4 2(move one stone to Pile 2)
1 1 5 2(move one stone to Pile 3)
1 1 4 3(move one stone to Pile 4)
0 2 5 2(move one stone to Pile 2 and another one to Pile 3)
0 2 4 3(move one stone to Pile 2 and another one to Pile 4)
0 1 5 3(move one stone to Pile 3 and another one to Pile 4)
0 3 4 2(move two stones to Pile 2)
0 1 6 2(move two stones to Pile 3)
0 1 4 4(move two stones to Pile 4)
Alice always moves first. Suppose that both Alice and Bob do their best in the game.
You are to write a program to determine who will finally win the game.
Input
input contains several test cases. The first line of each test case
contains an integer number n, denoting the number of piles. The
following n integers describe the number of stones in each pile at the
beginning of the game, you may assume the number of stones in each pile
will not exceed 100.
The last test case is followed by one zero.
Output
Sample Input
3
2 1 3
2
1 1
0
Sample Output
1
0
Source
【POJ】A New Stone Game(博弈论)的更多相关文章
- POJ.1067 取石子游戏 (博弈论 威佐夫博弈)
POJ.1067 取石子游戏 (博弈论 威佐夫博弈) 题意分析 简单的威佐夫博弈 博弈论快速入门 代码总览 #include <cstdio> #include <cmath> ...
- [Codeforces 1191D] Tokitsukaze, CSL and Stone Game(博弈论)
[Codeforces 1191D] Tokitsukaze, CSL and Stone Game(博弈论) 题面 有n堆石子,两个人轮流取石子,一次只能从某堆里取一颗.如果某个人取的时候已经没有石 ...
- hdu 2486/2580 / poj 3922 A simple stone game 博弈论
思路: 这就是K倍动态减法游戏,可以参考曹钦翔从“k倍动态减法游戏”出发探究一类组合游戏问题的论文. 首先k=1的时候,必败态是2^i,因为我们把数二进制分解后,拿掉最后一个1,那么会导致对方永远也取 ...
- POJ 2348 Euclid's Game 博弈论
http://poj.org/problem?id=2348 顺便说,必应翻译真的好用,比谷歌翻译好用100倍. 很难判断这道题的具体博弈类型. 有两种写法,一种是找规律,一种是推理得到关系后循环(或 ...
- [poj 3537]Crosses and Crosses(博弈论)
题目:http://poj.org/problem?id=3537 题意:给你n个格子,两个人依次在n个格子的任意空位置画"X",谁如果画了一个后,3个X连在了一起,那么那个人就获 ...
- POJ 3922A Simple Stone Game
题目链接 A Sample Stone Game 题目大意:给定n,k,表示最初时有n个石头,两个人玩取石子游戏,第一个人第一次可以取1~n-1个石头,后面每个人最多可以拿走前面一个人拿走的个数的K倍 ...
- UVa 1378 A Funny Stone Game [博弈论 SG函数]
A Funny Stone Game 题意: $n \le 23$堆石子,每次选择$i < j \le k$,从$i$拿走1颗$j,k$各放入一颗,不能取就失败.求先手是否必胜以及第一次取的策略 ...
- POJ 3553 Light Switching Game 博弈论 nim积 sg函数
http://poj.org/problem?id=3533 变成三维的nim积..前面hdu那个算二维nim积的题的函数都不用改,多nim积一次就过了...longlong似乎不必要但是还是加上了 ...
- POJ 2425 A Chess Game 博弈论 sg函数
http://poj.org/problem?id=2425 典型的sg函数,建图搜sg函数预处理之后直接求每次游戏的异或和.仍然是因为看不懂题目卡了好久. 这道题大概有两个坑, 1.是搜索的时候vi ...
- POJ 2484 A Funny Game 博弈论 对称博弈
http://poj.org/problem?id=2484 1和2时Alice必胜,3时Bob必胜,其他情况下Bob只需要在Alice取过之后取一次将剩下的硬币链平均分为两份,然后Alice怎么取B ...
随机推荐
- SphinxSE的安装
SphinxSE 的使用 SphinxSE 的使用 :wiki SphinxSE是一个可以编译进MySQL 5.x版本的MySQL存储引擎,尽管被称作“存储引擎”,SphinxSE自身其实并不存储任何 ...
- dubbo作为消费者注册过程分析
请支持原创: http://www.cnblogs.com/donlianli/p/3847676.html 作者当前分析的版本为2.5.x.作者在分析的时候,都是带着疑问去查看代码,debug进 ...
- MyBatis3: Could not find SQL statement to include with refid ‘
错误: org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.builder.Incompl ...
- 搭建CAS单点登录服务器
最近公司的一个项目需要用到单点登录的功能,之前对单点登录了解得不多.于是网上找了下单点登录的解决方案,发现CAS是个不错的解决方案.于是搭个环境测试了一下.这里记录下测试的详细步骤. 官网:http: ...
- Java for LeetCode 062 Unique Paths
A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...
- Java for LeetCode 035 Search Insert Position
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
- elk+redis分布式分析nginx日志
一.elk套件介绍 ELK 由 ElasticSearch . Logstash 和 Kiabana 三个开源工具组成.官方网站: https://www.elastic.co/products El ...
- Could not create the view: An unexpected exception was thrown. 电脑突然断电,myeclipse非正常关闭,出现错误
电脑突然断电,myeclipse非正常关闭,“Package Explorer”非正常显示,出现错误“Could not create the view: An unexpected exceptio ...
- win7下IIS配置MVC项目
第一步:添加MVC程序映射 1.双击打开,如下图: 2. 点击界面右边操作中的:添加脚本映射 请求路径:* 可执行文件路径:C:\Windows\Microsoft.NET\Framework\v4. ...
- Xshell 中文乱码
Xshell对于嵌入式开发来说,是个非常不错的工具.但或许都有过被中文显示为乱码的问题感觉有点不爽.解决方法其实很简单的,即把xshell编码方式改成UTF-8即可. [文件]–>[打开]–&g ...