dynamica programming coins in line

转载 2015年07月06日 23:16:33

There are n coins in a line. (Assume n is even). Two players take turns to take a coin from one of the ends of the line until there are no more coins left. The player with the larger amount of money wins.

  1. Would you rather go first or second? Does it matter?
  2. Assume that you go first, describe an algorithm to compute the maximum amount of money you can win.

Let us look one extra step ahead this time by considering the two coins the opponent will possibly take, Ai+1 and Aj. If the opponent takes Ai+1, the remaining coins are { Ai+2 … Aj }, which our maximum is denoted by P(i+2, j). On the other hand, if the opponent takes Aj, our maximum is P(i+1, j-1). Since the opponent is as smart as you, he would have chosen the choice that yields the minimum amount to you.

Therefore, the maximum amount you can get when you choose Ai is:

P1 = Ai + min { P(i+2, j), P(i+1, j-1) }

Similarly, the maximum amount you can get when you choose Aj is:

P2 = Aj + min { P(i+1, j-1), P(i, j-2) }

Therefore,

P(i, j) = max { P1, P2 }
        = max { Ai + min { P(i+2, j),   P(i+1, j-1) },
                Aj + min { P(i+1, j-1), P(i,   j-2) } }

Although the above recurrence relation could be implemented in few lines of code, its complexity is exponential. The reason is that each recursive call branches into a total of four separate recursive calls, and it could be n levels deep from the very first call). Memoization provides an efficient way by avoiding re-computations using intermediate results stored in a table. Below is the code which runs in O(n2) time and takes O(n2) space.

Edit:
Updated code with a new function printMoves which prints out all the moves you and the opponent make (assuming both of you are taking the coins in an optimal way).


395.Coins in a Line II-硬币排成线 II(中等题)

硬币排成线 II 题目有 n 个不同价值的硬币排成一条线。两个参赛者轮流从左边依次拿走 1 或 2 个硬币,直到没有硬币为止。计算两个人分别拿到的硬币总价值,价值高的人获胜。请判定 第一个玩家 是输...

lintcode Coins in Line II 硬币排成线 II

硬币排成线 II  描述 笔记  数据  评测 有 n 个不同价值的硬币排成一条线。两个参赛者轮流从左边依次拿走 1 或 2 个硬币,直到没有硬币为...

ALSA --- amixer控制声卡驱动实现Line-in功能

开发环境:Ubuntu12.04    开发板:OK6410,Linux3.0 alsamixer是Linux 音频架构ALSA工具的其中一个,用于配置音频的各个参数。 alsamixer是基于文...

uva 216 Getting in Line 最短路,全排列暴力做法

题目给出离散的点,要求求出一笔把所有点都连上的最短路径。 最多才8个点,果断用暴力求。 用next_permutation举出全排列,计算出路程,记录最短路径。 这题也可以用dfs回溯暴力,但是...
  • hcbbt
  • hcbbt
  • 2013年07月23日 11:59
  • 1809

【九度】题目1326:Waiting in Line

题目描述: Suppose a bank has N windows open for service. There is a yellow line in front of the windows...

PAT 1014 Waiting in Line(模拟+队列操作)

题目https://www.patest.cn/contests/pat-a-practise/1014 银行前台处理客户的过程如下,有n个窗口,每个窗口前的队伍可以排m个人,其余人在黄线外等待;...

freemarker.core.InvalidReferenceException: Expression parameters.parseContent is undefined on line 45, column 28 in template/aja

严重: Expression parameters.parseContent is undefined on line 45, column 28 in template/ajax/head.ftl....

UVA216 - Getting in Line(哈密顿图)

题目: http://acm.hust.edu.cn/vjudge/contest/view.action?cid=90847#problem/C 思路: 经过每条边一次。 递归回溯。 AC...

use addr2line in android

Let's say that logcat show you the following crash log (this is from one of my projects):   Lo...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:dynamica programming coins in line
举报原因:
原因补充:

(最多只允许输入30个字)