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. Comet事件分析

    简介[ Introduction ]  使用APR或者NIO API作为连接器的基础,Tomcat能够提供一些在阻塞IO之上的有效扩展,用于支持Servlet API. [ With usage of ...

  2. 微电子中的die-to-die和within-die

    工艺制造中lot指按某种方式生成的硅柱状体,将这些lot切成薄片就称为wafer,wafer是进行集成电路制造的基板,一般以直径来区分,8寸.10寸,12寸等,或者以毫米来区分.直径越大材料的利用率越 ...

  3. Oracle执行计划的查看

    前言 一个系统在刚开始的时候,由于数据库中数据量不大,开发人员的主要精力都在业务与功能实现上.系统完成部署上线后随着时间的累积,每个表中的数据都在不断增长,我们往往会发现系统越来越慢,这可能是程序设计 ...

  4. IOS折线图二

    上周把项目的折线图给做了下,今天想着把它完善完善,自己设置了不同的数据源来测试,哈哈,还真遇到问题了, 就是给图表设置折点数为0时,视图显示的还是原来的,由于数据为空,应该将其设置为空,所以想着怎么把 ...

  5. iOS开源项目周报0309

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

  6. 十二:video 视频

    属性名 类型 默认值 说明 src String   要播放视频的资源地址 controls Boolean true 是否显示默认播放控件(播放/暂停按钮.播放进度.时间) danmu-list O ...

  7. ASP.NET 解决在点击Button执行服务器事件之前验证用户输入并阻塞

    在网站项目开发时,为了减少用户的错误性的操作,很多时候我们都需要做一些必要的JS验证来提醒用户,比如:“输入的值不符合规则,请重新输入”.“提交后无法修改,您确定要继续吗?”友好性的提示. 这时候我们 ...

  8. Java基础教程(6)--数组

    1.基本概念   数组中的每一项称为元素,每个元素都通过数字索引(也可以称为下标)访问,编号从0开始.例如,第4个元素的索引为3.下面的程序创建了一个int类型的数组,把一些值放入数组中并将每个值打印 ...

  9. linux ssh免密登陆

    大致流程: 两台linux系统A B 如果A要登陆到B 1.生成A的密钥对 2.将A的公钥拷贝到B的authorized_keys中即可 可以使用命令:ssh-copy-id -i ~/.ssh/id ...

  10. Redis-SDS

    Redis 的简单动态字符串 (simple dynamic string,SDS) SDS的结构: struct sdshdr { int len;  //保存的字符串长度. int free;  ...