I Want That Cake

题目连接:

https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=4925

Description

There was an interesting game played on a popular Korean reality TV Show. 12 players in 3 teams

— 4 persons in each team — lined up in one row in random order. The game master approaches the

players one by one starting from the most front player, with a tray full of 31 cakes. Each player should

eat at least 1 and at most 5 cakes during his/her turn. The player who eats the last cake will lose

along with his/her group (this is a team game). It was more an entertainment show rather than a real

competition. The players were selected from “chubby” celebrities who like to eat, thus causing them

in dilemma whether they should win the game (which might require them to hold their urge to eat the

cakes), or just enjoy all 5 cakes ignoring their team member.

This problem is related to the game. There are 2 teams (A and B) each with N players lined up

in a row in random order. Initially there are M cakes in the tray, and each player (starting from the

most front) has to eat at least 1 and at most K cakes during his/her turn. The team whose player eat

the last cake win (note that this is different compared to the original game).

Your task is to determine which team will win the game, given both teams play optimally.

Input

The first line of input contains an integer T (T ≤ 100) denoting the number of cases. Each case begins

with three integers N, M, and K (1 ≤ N ≤ 1, 000; 1 ≤ K ≤ M ≤ 2 ∗ N) in a line denoting the number

of players in each team, the initial number of cakes, and the maximum number of cakes can be eaten

by each player in his/her turn respectively. The next line contains a string S representing the players

order from the front most to the last player. S consists of only character ‘A’ or ‘B’ representing which

team does the respective player belongs to. The length of S is exactly 2 ∗ N and the number of ‘A’ and

‘B’ will be equal.

Output

For each case, output ‘Case #X: Y ’, where X is the case number starts from 1 and Y is ‘A’ if the

game will be won by team A, otherwise ‘B’. Assume both teams will play the game optimally to win

the game.

Explanation for 1st sample case:

The first two players of team A each needs to eat 2 cakes, while the third A player eats the last

cake.

Explanation for 2nd sample case:

No matter what team A do, the last cake will be eaten by one of team B’s player.

Explanation for 3rd sample case:

To ensure their win, the first player (B) should eat 2 cakes, leaving only 3 cakes to the next player

(A). This player (A) should eat at least 1 and at most 2 cakes. No matter how many cakes this player

eats, the next player (B) will eat the last cake.

Sample Input

4

3 5 2

AAABBB

4 7 2

AAABBBBA

4 5 2

BABABABA

4 6 3

BAABBABA

Sample Output

Case #1: A

Case #2: B

Case #3: B

Case #4: A

Hint

题意

有两支队,每只队都有n个人,一共有m个蛋糕,每个人至少吃一个,最多吃k个。

都采取最优策略,谁吃到最后一个蛋糕,那么那只队就胜利。

按照给定的顺序去吃蛋糕,问你最后谁胜利。

题解:

先缩点,把相同的点都缩成一个点。

那么就变成了ABABABA这样交替的形式了,然后跑DP就好了。

但是这个DP好像是n^3的,其实你有break,所以跑起来还是蛮快的,当然,你吃蛋糕得倒着枚举。

代码

#include <bits/stdc++.h>
#define rep(a,b,c) for(int (a)=(b);(a)<=(c);++(a))
#define drep(a,b,c) for(int (a)=(b);(a)>=(c);--(a))
#define pb push_back
#define mp make_pair
#define sf scanf
#define pf printf
#define two(x) (1<<(x))
#define clr(x,y) memset((x),(y),sizeof((x)))
#define dbg(x) cout << #x << "=" << x << endl;
#define lowbit(x) ((x)&(-x))
const int mod = 1e9 + 7;
int mul(int x,int y){return 1LL*x*y%mod;}
int qpow(int x , int y){int res=1;while(y){if(y&1) res=mul(res,x) ; y>>=1 ; x=mul(x,x);} return res;}
inline int read(){int x=0,f=1;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}return x*f;}
using namespace std;
const int maxn = 1000 + 15;
int N , M , K , dp[2 * maxn][maxn * 2];
char str[maxn * 2];
vector < int > op; int dfs( int x , int y ){
if( y <= 0 ) return 0;
if( dp[x][y] != -1 ) return dp[x][y];
int & ans = dp[x][y] = 0;
if( x == op.size() ) return ans = 1;
for(int i = op[x] * K ; i >= op[x] ; -- i){
int rs = dfs( x + 1 , y - i );
if( rs == 0 ) return ans = 1;
}
return ans;
} int main(int argc,char *argv[]){
int T=read(),cas=0;
while(T--){
N=read(),M=read(),K=read();
sf("%s",str + 1);
op.clear();
for(int i = 1 ; i <= N * 2 ; ){
int j;
for(j = i ; j <= N * 2 && str[i] == str[j] ; ++ j );
op.pb( j - i );
i = j;
}
for(int i = 0 ; i <= op.size() ; ++ i) for(int j = 0 ; j <= M ; ++ j) dp[i][j] = -1;
pf("Case #%d: ", ++ cas);
if( dfs( 0 , M ) ) pf("%c\n" , str[1]);
else{
if( str[1] == 'A' ) pf("%c\n" , 'B');
else pf("%c\n" , 'A');
}
}
return 0;
}

UVALive 6913 I Want That Cake 博弈dp的更多相关文章

  1. UVALive 6913 I Want That Cake 博弈+dp

    题目链接: http://acm.hust.edu.cn/vjudge/problem/96343 I Want That Cake Time Limit: 3000MS 64bit IO Forma ...

  2. HDU 5623 KK's Number (博弈DP)

    KK's Number 题目链接: http://acm.hust.edu.cn/vjudge/contest/121332#problem/K Description Our lovely KK h ...

  3. 博弈dp 以I Love this Game! POJ - 1678 为例

    写在前面的话 知识基础:一些基础的博弈论的方法,动态规划的一些知识 前言:博弈论就是一些关于策略或者游戏之间的最优解,动态规划就是对于一些状态之间转移的一些递推式(or 递归),dp分为很多很多种,比 ...

  4. 博弈dp入门 POJ - 1678 HDU - 4597

    本来博弈还没怎么搞懂,又和dp搞上了,哇,这真是冰火两重天,爽哉妙哉. 我自己的理解就是,博弈dp有点像对抗搜索的意思,但并不是对抗搜索,因为它是像博弈一样,大多数以当前的操作者来dp,光想是想不通的 ...

  5. zoj 3537 Cake 区间DP (好题)

    题意:切一个凸边行,如果不是凸包直接输出.然后输出最小代价的切割费用,把凸包都切割成三角形. 先判断是否是凸包,然后用三角形优化. dp[i][j]=min(dp[i][j],dp[i][k]+dp[ ...

  6. tyvj P1075 - 硬币游戏 博弈DP

    P1075 - 硬币游戏 From price    Normal (OI)总时限:10s    内存限制:128MB    代码长度限制:64KB 背景 Background 农民John的牛喜欢玩 ...

  7. ZOJ 3905 Cake(贪心+dp)

    动态规划题:dp[i][j]表示有i个Cake,给了Alice j个,先按照b排序,这样的话,能保证每次都能成功给Alice Cake,因为b从大到小排序,所以Alice选了j个之后,Bob最少选了j ...

  8. UVA 1558 - Number Game(博弈dp)

    UVA 1558 - Number Game 题目链接 题意:20之内的数字,每次能够选一个数字,然后它的倍数,还有其它已选数的倍数组合的数都不能再选,谁先不能选数谁就输了,问赢的方法 思路:利用dp ...

  9. 计蒜客 取数游戏 博弈+dp

    题目链接 取数游戏 思路:dp(x, y)表示先手在区间[x, y]能取得的最大分数.当先手取完,就轮到后手去,后手一定会选择当前能令他得到最大分数的策略,其实当先手在[x, y]区间两端取走一个数, ...

随机推荐

  1. bzoj千题计划213:bzoj2660: [Beijing wc2012]最多的方案

    http://www.lydsy.com/JudgeOnline/problem.php?id=2660 很容易想到是先把n表示成最大的两个斐波那契数相加,然后再拆分这两个斐波那契数 把数表示成斐波那 ...

  2. Windows一个文件夹下面最多可以放多少文件

    一个文件夹下面最多可以放多少文件 这个问题其实我也不知道,不过我们可以来进行个测试,看看文件夹下面最多能放多少个文件. 那么怎么来测试这样一个问题呢,很显然我们一个个的去建立文件是不现实的,没那么多时 ...

  3. ASP.NET MVC学习笔记-----ControllerFactory

    上面这张图是asp.net mvc的工作流程图,我们可以看到当一个http请求来临时,首先需要经过路由系统,路由系统从中获取一些路由信息,然后ControllerFactory根据所得到的路由信息生成 ...

  4. 第5月第16天 php crud CodeIgniter CI_DB_active_record

    1.C.R.U.D. Generator for CodeIgniter https://github.com/antonioyee/crud-generator/tree/9e5e48e773a52 ...

  5. Sortable.js参数

    所有的事件回调函数都有两个参数:event和ui,浏览器自有event对象,和经过封装的ui对象ui.helper - 表示sortable元素的JQuery对象,通常是当前元素的克隆对象ui.pos ...

  6. Spark笔记之使用UDF(User Define Function)

    一.UDF介绍 UDF(User Define Function),即用户自定义函数,Spark的官方文档中没有对UDF做过多介绍,猜想可能是认为比较简单吧. 几乎所有sql数据库的实现都为用户提供了 ...

  7. Python基础教程-第3版(文字版) 超清文字-非扫描版 [免积分、免登录]

    此处免费下载,无需账号,无需登录,无需积分.收集自互联网,侵权通知删除. 点击下载:Python基础教程-第3版 备用下载:Python基础教程-第3版

  8. 重温CSS之基础

    在HTML中插入样式表: 内联式:直接在HTML标签中插入样式 <p style="color:red"></p> 2. 嵌入式: <style ty ...

  9. Smarty 模板引擎简介

    前言 Smarty是一个使用PHP写出来的模板引擎,是目前业界最著名的PHP模板引擎之一.它分离了逻辑代码和外在的内容,提供了一种易于管理和使用的方法,用来将原本与HTML代码混杂在一起PHP代码逻辑 ...

  10. 移动端console.log()调试

    在微信或app进行开发的时候,没法直接查看console.log的输出内容,调试起来简直太痛苦了. 1.笨笨的方法 fiddler抓请求:追加dom节点,显示调试信息. var div =docume ...