Foj 2296 Alice and Bob

题意

两个人博弈,规则如下:轮流取09中的数字,最后Alice所得的数字个数为1n中,数位在Alice所取集合中出现奇数次的。

双方想获得尽量多,问Alice能获得几个。

题解

观察一下,如果n是十的倍数,最后肯定是一人一半,这样一来,状态数应该会减少很多,我们就可以直接去搜。

半个月前我补这道题,一直觉得是个dp问题,现在想想,其实就是暴力去做极大极小过程,只是需要把状态数表示出来。

因为有了一开始的观察,所以可以把数字分成四类,再记一下前几位数一共出现了奇数次还是偶数次,如果奇数次最后答案是多少,偶数次是多少,这题就做完了。其实不难嘛!

扩展

game dfs 写法需考虑的状态表示问题需要多练习。

dp 需练习。

代码

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define rep(i, a, b) for(int i=(a); i<(b); i++)
#define sz(a) (int)a.size()
#define de(a) cout << #a << " = " << a << endl
#define dd(a) cout << #a << " = " << a << " "
#define all(a) a.begin(), a.end()
#define endl "\n"
typedef long long ll;
typedef pair<int, int> pii;
//--- int n;
int dp[11][11][11], cnt[10]; int dfs(int a, int b, int c, int d, int pre, int cnt1, int cnt2, int ty) {
if(a+b+c+d == 0) return pre==0 ? cnt1 : cnt2;
if(ty == 0) {
int ans = 0;
if(a) ans = max(ans, dfs(a-1, b, c, d, pre, cnt1+1, cnt2, 1));
if(b) ans = max(ans, dfs(a, b-1, c, d, pre^1, cnt1+1, cnt2, 1));
if(c) ans = max(ans, dfs(a, b, c-1, d, pre, cnt1, cnt2, 1));
if(d) ans = max(ans, dfs(a, b, c, d-1, pre^1, cnt1, cnt2, 1));
return ans;
} else {
int ans = 10;
if(a) ans = min(ans, dfs(a-1, b, c, d, pre, cnt1, cnt2+1, 0));
if(b) ans = min(ans, dfs(a, b-1, c, d, pre, cnt1, cnt2+1, 0));
if(c) ans = min(ans, dfs(a, b, c-1, d, pre, cnt1, cnt2, 0));
if(d) ans = min(ans, dfs(a, b, c, d-1, pre, cnt1, cnt2, 0));
return ans;
}
} void init() {
rep(i, 0, 11) rep(j, 0, 11-i) rep(k, 0, 11-i-j) dp[i][j][k] = dfs(i, j, k, 10-i-j-k, 0, 0, 0, 0);
} int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(0);
init();
int T;
cin >> T;
while(T--) {
///
cin >> n;
///init
memset(cnt, 0, sizeof(cnt));
///solve
int k = n%10;
int ans = n = n/10;
while(n) {
++cnt[n%10];
n/=10;
}
int a = 0, b = 0, c = 0;
rep(i, 0, k) {
if(cnt[i]%2==0) ++a;
else ++b;
}
rep(i, k, 10) c += cnt[i]%2==0;
cout << ans*5 + dp[a][b][c] << endl;
}
return 0;
}

Foj 2296 Alice and Bob(博弈、搜索)的更多相关文章

  1. ZOJ 3529 A Game Between Alice and Bob 博弈好题

    A Game Between Alice and Bob Time Limit: 5 Seconds      Memory Limit: 262144 KB Alice and Bob play t ...

  2. BZOJ 3895 3895: 取石子 / Luogu SP9934 ALICE - Alice and Bob (博弈 记忆化搜索)

    转自PoPoQQQ大佬博客 题目大意:给定n堆石子,两人轮流操作,每个人可以合并两堆石子或拿走一个石子,不能操作者输,问是否先手必胜 直接想很难搞,我们不妨来考虑一个特殊情况 假设每堆石子的数量都&g ...

  3. UVaLive 5760 Alice and Bob (博弈 + 记忆化搜索)

    题意:有 n 堆石子,有两种操作,一种是从一堆中拿走一个,另一种是把两堆合并起来,Alice 先拿,谁不能拿了谁输,问谁胜. 析:某些堆石子数量为 1 是特殊,石子数量大于 1 个的都合并起来,再拿, ...

  4. HDU 4111 Alice and Bob (博弈+记忆化搜索)

    题意:给定 n 堆石头,然后有两种操作,一种是把从任意一堆拿走一个,另一种是把一个石子放到另一堆上. 析:整体看,这个题真是不好做,dp[a][b] 表示有 a 堆1个石子,b个操作,操作是指把其他的 ...

  5. ACdream 1112 Alice and Bob (博弈&amp;&amp;素数筛选优化)

    题目链接:传送门 游戏规则: 没次能够将一堆分成两堆 x = a*b (a!=1&&b!=1)x为原来堆的个数,a,b为新堆的个数. 也能够将原来的堆的个数变成原来堆的约数y.y!=x ...

  6. Alice and Bob(博弈)

    Alice and Bob Time Limit: 1000ms, Special Time Limit:2500ms, Memory Limit:65536KB Total submit users ...

  7. hdu 4111 Alice and Bob 记忆化搜索 博弈论

    Alice and Bob Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pi ...

  8. 博弈 HDOJ 4371 Alice and Bob

    题目传送门 题意:Alice和 Bob轮流写数字,假设第 i 次的数字是S[i] ,那么第 i+1 次的数字 S[i+1] = S[i] + d[k] 或 S[i] - d[k],条件是 S[i+1] ...

  9. ACdream 1112 Alice and Bob(素筛+博弈SG函数)

    Alice and Bob Time Limit:3000MS     Memory Limit:128000KB     64bit IO Format:%lld & %llu Submit ...

随机推荐

  1. CNN理解与实现

    CNN理解与实现 组成部分 Convolution Layer Pool Layer: Max-pooling layer Average-pooling layer Full Connected(F ...

  2. CSS3 Media Queries_media queries, css3属性详解

    Media Queries直译过来就是"媒体查询",在我们平时的Web页面中head部分常看到这样的一段代码: <link href="css/reset.css& ...

  3. jQuery事件篇---过滤选择器 & 表单选择器

    内容提纲: 过滤选择器 1.基本过滤器 2.内容过滤器 3.可见性过滤器 4.子元素过滤器 5.其他方法 表单选择器 6.常规选择器 7.表单选择器 8.表单过滤器 发文不易,转载请注明出处! 过滤选 ...

  4. firefox并不支持selectSingleNode和selectNodes的解决方法

    转自:http://qsrock.iteye.com/blog/209585 function test(){ var perid = document.thisForm.PerID.value; v ...

  5. [编程] C语言的结构体

    结构体 struct 结构体名{} 变量名; 结构体变量: struct person{ char *name; int age; float score; } student; 成员的获取和赋值 / ...

  6. 【SSH网上商城项目实战12】添加和更新商品功能的实现

    转自: https://blog.csdn.net/eson_15/article/details/51366370 添加商品部分原理和添加商品类别是一样的,不过要比商品类别复杂,因为商品的属性有很多 ...

  7. SQL索引优化及实战文章总结(总结)

    1. MySQL索引原理以及查询优化 2.

  8. web 开发人员必须学习的 3 门语言

    web 开发人员必须学习的 3 门语言:html  css  js HTML 定义了网页的内容 CSS 描述了网页的布局 JavaScript 网页的行为

  9. js 控制选中文字

     //脚本获取网页中选中文字 var word = document.selection.createRange().text;  //获取选中文字所在的句子 var range =  documen ...

  10. js生成qq客服在线代码

    说到QQ客服在线代码,随便那么百度谷歌一下就会出来,一般都是 <a target="blank" href="http://wpa.qq.com/msgrd?V=1 ...