【bzoj1982】【Spoj2021】【Moving Pebbles】【博弈论】
2016-03-17 19:40
411 查看
Description
2021. Moving Pebbles Two players play the following game. At the beginning of the game they start with n (1<=n<=100000) piles of stones. At each step of the game, the player chooses a pile and remove at least one stone from this pile and move zero or more stonesfrom this pile to any other pile that still has stones. A player loses if he has no more possible moves. Given the initial piles, determine who wins: the first player, or the second player, if both play perfectly. 给你N堆Stone,两个人玩游戏. 每次任选一堆,首先拿掉至少一个石头,然后移动任意个石子到任意堆中.
谁不能移动了,谁就输了...
Input
Each line of input has integers 0 < n <= 100000, followed by n positive integers denoting the initial piles.Output
For each line of input, output "first player" if first player can force a win, or "second player", if the second player can force a win.Sample Input
3 2 1 3Sample Output
first player题解:博弈论的题都好神奇。
考虑如果这些石子能够两两配对配成n/2组(n为偶数),则是先手必败的。
因为这种情况下无论先手如何操作,后手都可以让局面回到等价的状态。
其余情况下先手必胜,因为其余情况下先手都有办法将局面变成先手必败。
代码:
#include<iostream> #include<cstdio> #include<algorithm> using namespace std; int n,a[100010]; int main(){ cin>>n; if (n&1){cout<<"first player"<<endl;return 0;} for (int i=1;i<=n;i++)cin>>a[i]; sort(a+1,a+n+1); for (int i=1;i<=n;i+=2) if (a[i]!=a[i+1]){ cout<<"first player"<<endl; return 0; } cout<<"second player"<<endl; }
相关文章推荐
- 蓝桥杯 历届试题 敢死队
- 一个简单的Servlet程序
- 修改IP的脚本
- Android SVN服务器的搭建及在AndroidStudio中使用SVN
- ASP.NET网站开发中的配置文件
- 第二周总结
- 怎样学好Python
- C++的运算符重载
- java eclipse如何显示行数
- 《分布式系统原理与范型》第二版笔记
- Java集合框架:TreeMap
- Maximum GCD
- 第一次做二级联动,没有用到对象与二维数组(笨方法,思路简单)
- 关于堆栈、内存分配
- Java集合框架:TreeMap
- ios 脚本打包demo
- 【操作系统】实验一 命令解释程序的编写
- 复利计算完成时间统计
- DFS回溯-函数递归-xiaoz triangles
- 第一章 引论 第二章 算法分析