bzoj 3895 取石子——博弈论
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3895
看题解:https://blog.csdn.net/popoqqq/article/details/43989101
虽然大于1的堆的操作次数带来的必胜或必败在还有等于1的堆存在的情况下就不准了,但仍可以把它先记录下来,因为一旦没有了等于1的堆,就会按那个取,所以它被加入考虑。
注意两点:1.传参时注意如果没有大于1的堆,别传入-1;
2.注意当大于1的堆被取成一个等于1的堆时要判断。
dfs里写得美一点,就能快256ms。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=,M=;
int T,n,a[N],dp[N][N*M+N];
int rdn()
{
int ret=;char ch=getchar();
while(ch>''||ch<'')ch=getchar();
while(ch>=''&&ch<='')(ret*=)+=ch-'',ch=getchar();
return ret;
}
bool dfs(int x,int y)
{
if(y==)x++,y=;//!
if(dp[x][y]!=-)return dp[x][y];
if(!x) return dp[x][y]=(y&);
if(x&&!dfs(x-,y))return dp[x][y]=;
if(x&&y&&!dfs(x-,y+))return dp[x][y]=;//&&y!
if(x>&&!dfs(x-,y?y+:y+))return dp[x][y]=;
if(y&&!dfs(x,y-))return dp[x][y]=;
return dp[x][y]=;
}
int main()
{
T=rdn();
memset(dp,-,sizeof dp);
while(T--)
{
n=rdn();int cnt=,sum=;
for(int i=;i<=n;i++)
{
a[i]=rdn();
if(a[i]==)cnt++;else sum+=a[i];
}
puts(dfs(cnt,(n-cnt)?(n-cnt-+sum):)?"YES":"NO");//判n-cnt!
}
return ;
}
bzoj 3895 取石子——博弈论的更多相关文章
- bzoj 3895 取石子 —— 博弈论
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3895 看了博客:https://blog.csdn.net/popoqqq/article/ ...
- bzoj 3895: 取石子
$ \color{#0066ff}{ 题目描述 }$ Alice和Bob两个好朋含友又开始玩取石子了.游戏开始时,有N堆石子 排成一排,然后他们轮流操作(Alice先手),每次操作时从下面的规则中任选 ...
- BZOJ 3895: 取石子[SG函数 搜索]
有N堆石子 ·从某堆石子中取走一个 ·合并任意两堆石子 不能操作的人输. 100%的数据满足T<=100, N<=50. ai<=1000 容易发现基础操作数$d=\sum a ...
- bzoj 1874 取石子游戏 题解 & SG函数初探
[原题] 1874: [BeiJing2009 WinterCamp]取石子游戏 Time Limit: 5 Sec Memory Limit: 162 MB Submit: 334 Solved ...
- 【XSY2988】取石子 博弈论
题目描述 有 \(n\) 堆石子,每堆石子的个数是 \(c_i\). Alice 和 Bob 轮流取石子(先后手未定),Alice 每次从一堆中取 \(a\) 个,Bob每次从一堆中取 \(b\) 个 ...
- BZOJ 1874 取石子游戏 - SG函数
Description $N$堆石子, $M$种取石子的方式, 最后取石子的人赢, 问先手是否必胜 $A_i <= 1000$,$ B_i <= 10$ Solution 由于数据很小, ...
- 【ACM】取石子 - 博弈论
取石子(一) 时间限制:3000 ms | 内存限制:65535 KB 难度:2 描述 一天,TT在寝室闲着无聊,和同寝的人玩起了取石子游戏,而由于条件有限,他/她们是用旺仔小馒头当作石子.游 ...
- BZOJ 3895 3895: 取石子 / Luogu SP9934 ALICE - Alice and Bob (博弈 记忆化搜索)
转自PoPoQQQ大佬博客 题目大意:给定n堆石子,两人轮流操作,每个人可以合并两堆石子或拿走一个石子,不能操作者输,问是否先手必胜 直接想很难搞,我们不妨来考虑一个特殊情况 假设每堆石子的数量都&g ...
- 【BZOJ】3895: 取石子
[算法]博弈论+记忆化搜索 [题意]给定n堆石子,两人轮流操作,每个人可以合并两堆石子或拿走一个石子,不能操作者输,问是否先手必胜 [题解] 首先,若所有石子堆的石子数>1,显然总操作数为(石子 ...
随机推荐
- PAT甲级——A1071 Speech Patterns
People often have a preference among synonyms of the same word. For example, some may prefer "t ...
- 《DSP using MATLAB》Problem 8.31
代码: %% ------------------------------------------------------------------------ %% Output Info about ...
- JSON高亮格式化页面显示
高亮CSS定义: <style type="text/css"> pre {outline: 1px solid #ccc; padding: 5px; margin: ...
- sql语句怎么看效率?
1.数据库设计当面: 对查询进行优化,应该尽量避免全表扫描,首先应考虑在where及order by设计的列上加索引. d.索引并不是越多越好,索引可以提高查询效率,同时降低了insert和updat ...
- 通过media媒体查询设置ie7/8样式、使用media判断各机型、手淘flexible.js
@media all and (min-width:1280px){ /* 所有设备宽度大于1280干嘛干嘛嘞... */ body{ background:#f00; } } @media (min ...
- SQL server插入数据后,获取自增长字段的值
? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 insert into Tb_People(uname,er ...
- js 给链接 url或href或js、css、图片等解决浏览器缓存
一. 添加时间戳 情况一.链接是常量 var rand = new Date().getTime(); var aLen=document.getElementsByTagName("a&q ...
- PAT甲级——A1036 Boys vs Girls
This time you are asked to tell the difference between the lowest grade of all the male students and ...
- day1(老男孩-Python3.5-S14期全栈开发)
作者:赵俊 发布日期:2019/10/18 一.第一个python程序 1.在解释器下写hello world程序运行,与运行外部文件方法 运行外部文件,必须在相应位置创建一个p ...
- Docker(四)安装Redis
1.安装redis https://blog.csdn.net/qq_29917523/article/details/78019511 2.测试连接