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. leetcode简单题目两道(5)

    Problem Given an integer (signed bits), write a function to check whether it . Example: Given num = ...

  2. android studio不能预览

    错误:Failed to load the LayoutLib: com/android/layoutlib/bridge/Bridge : Unsupported major.minor versi ...

  3. iOS开源项目周报0309

    由OpenDigg 出品的iOS开源项目周报第十期来啦.我们的iOS开源周报集合了OpenDigg一周来新收录的优质的iOS开源项目,方便iOS开发人员便捷的找到自己需要的项目工具等.LazyScro ...

  4. 删除Panl控件中窗体的方法

    //删除窗体方法 private void CloseFrm() { foreach (Control item in panel1.Controls) { if (item is Form) //判 ...

  5. Vue 引入第三方js.css的方式

    转自:https://blog.csdn.net/csdn_yudong/article/details/78795743 我们以 jQuery 为例,来讲解 一.绝对路径直接引入,全局可用 主入口页 ...

  6. 七、spark核心数据集RDD

    简介 spark RDD操作具体参考官网:http://spark.apache.org/docs/latest/rdd-programming-guide.html#overview RDD全称叫做 ...

  7. yum卸载

    完全卸载依赖 -- 正常安装 yum install sl -- 列出操作 yum history list sl -- 根据显示install操作的id进行删除 yum history undo { ...

  8. POJ 1611(并查集+知识)

    并查集主要是两个过程,一个是并,一个是查 原理是用一个数组p[i]保存每个i的根节点,如果根节点一样则在同一个集合里,所以只有根节点p[i]=i; 查: int find(int x){return ...

  9. hdu 2049 考新郎

    假设一共有N对新婚夫妇,其中有M个新郎找错了新娘,求发生这种情况一共有多少种可能. 和之前那道题一样,是错排,但是要乘上排列数. 选对的人有C(N,M)个组合,将它们排除掉,剩下的人就是错排了 #in ...

  10. MyBatis缓存通俗易懂

    1.1     mybatis缓存介绍 如下图,是mybatis一级缓存和二级缓存的区别图解: Mybatis一级缓存的作用域是同一个SqlSession,在同一个sqlSession中两次执行相同的 ...