[Algorithm] Dynamic Programming

Question 1. 

Problem statement: Consider a row of n coins of values v1 . . . vn, where n is even. We play a game against an opponent by alternating turns. In each turn, a player selects either the first or last coin from the row, removes it from the row permanently, and receives the value of the coin. Determine the maximum possible amount of money we can definitely win if we move first.

Solution from: http://www.geeksforgeeks.org/dynamic-programming-set-31-optimal-strategy-for-a-game/ by Aashish Barnwal. Accessed on 18th June, 2016.

There are two choices:
1. The user chooses the ith coin with value Vi: The opponent either chooses (i+1)th coin or jth coin. The opponent intends to choose the coin which leaves the user with minimum value.
i.e. The user can collect the value Vi + min(F(i+2, j), F(i+1, j-1) )

2. The user chooses the jth coin with value Vj: The opponent either chooses ith coin or (j-1)th coin. The opponent intends to choose the coin which leaves the user with minimum value.
i.e. The user can collect the value Vj + min(F(i+1, j-1), F(i, j-2) )

Following is recursive solution that is based on above two choices. We take the maximum of two choices.

F(i, j)  represents the maximum value the user can collect from
         i‘th coin to j‘th coin.

    F(i, j)  = Max(Vi + min(F(i+2, j), F(i+1, j-1) ),
                   Vj + min(F(i+1, j-1), F(i, j-2) ))
Base Cases
    F(i, j)  = Vi           If j == i
    F(i, j)  = max(Vi, Vj)  If j == i+1

Notes: ‘minimisation‘ occurs because everyone wants to minmimise the opponent‘s total coin value. So you shall suppose that your opponent always takes a strategy that can minimise your total value of coins and then maximise his total value of coins, when it‘s your turn to choose the next coin form either the head or the tail of the list.

时间: 2024-10-02 21:50:44

[Algorithm] Dynamic Programming的相关文章

[Algorithm -- Dynamic programming] How Many Ways to Decode This Message?

For example we have 'a' -> 1 'b' -> 2 .. 'z' -> 26 By given "12", we can decode the string to give result "ab" or 'L', 2 ways to decode, your function should return 2 as an answer. Now asking by given "1246", what sh

以计算斐波那契数列为例说说动态规划算法(Dynamic Programming Algorithm Overlapping subproblems Optimal substructure Memoization Tabulation)

动态规划(Dynamic Programming)是求解决策过程(decision process)最优化的数学方法.它的名字和动态没有关系,是Richard Bellman为了唬人而取的. 动态规划主要用于解决包含重叠子问题的最优化问题,其基本策略是将原问题分解为相似的子问题,通过求解并保存重复子问题的解,然后逐步合并成为原问题的解.动态规划的关键是用记忆法储存重复问题的答案,避免重复求解,以空间换取时间. 用动态规划解决的经典问题有:最短路径(shortest path),0-1背包问题(K

Dynamic Programming

We began our study of algorithmic techniques with greedy algorithms, which in some sense form the most natural approach to algorithm design. Faced with a new computational problem, we've seen that it's not hard to propose multiple possible greedy alg

hdu 4972 A simple dynamic programming problem(高效)

题目链接:hdu 4972 A simple dynamic programming problem 题目大意:两支球队进行篮球比赛,每进一次球后更新比分牌,比分牌的计数方法是记录两队比分差的绝对值,每次进球的分可能是1,2,3分.给定比赛中的计分情况,问说最后比分有多少种情况. 解题思路:分类讨论: 相邻计分为1-2或者2-1的时候,会对应有两种的的分情况 相邻计分之差大于3或者说相等并且不等于1的话,为非法输入 其他情况下,不会造成新的比分情况产生 对于最后一次比分差为0的情况,就没有谁赢谁

hdu 4972 A simple dynamic programming problem (转化 乱搞 思维题) 2014多校10

题目链接 题意:给定一个数组记录两队之间分差,只记分差,不记谁高谁低,问最终有多少种比分的可能性 分析: 类似cf的题目,比赛的时候都没想出来,简直笨到极点..... 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cstdlib> 5 #include <cmath> 6 #include <vector> 7 #include &

2016 UESTC Training for Dynamic Programming

2016 UESTC Training for Dynamic Programming A - 柱爷与咸鱼神功 题意: 柱爷有n(<=5000)点心情去学m(<=5000)个招式,每个招式会得到一定的修炼值,但要消耗一定的心情,求最多的修炼值. 题解: 0.这是一个裸的背包问题,用每一个物品去更新每一种背包的状态. 1.状态定义:dp[i]表示用i点心情得到的最多修炼值. 2.状态转移:dp[i] = max{dp[i-v[j]]+w[j]} 代码: 1 2 3 4 5 6 7 8 9 10

Dynamic Programming Introduction

Optimal substructure In computer science, a problem is said to have optimal substructure if an optimal solution can be constructed efficiently from optimal solutions of its subproblems. This property is used to determine the usefulness of dynamic pro

About Dynamic Programming

Main Point: Dynamic Programming = Divide + Remember + Guess 1. Divide the key is to find the subproblem 2. Remember use a data structure to write down what has been done 3. Guess when don't know what to do, just guess what next step can be Problems:

noip Dynamic Programming

noip Dynamic Programming A - Jury Compromise POJ 1015 题意: 有n(<=200)个人作为陪审团的被选举成员,分别有正方和反方给予的评价,要求在这n个人中选出m(<=20)个人,在保证正方评价之和与反方评价之和差最小的情况下,使得评价分数总和最高. 题解: 0.定义状态fi][k]当前已经选取了i个人并且这i个人的差值为k时的评分总和的最大值,p[i][k]当前已经选取了i个人并且这i个人的差值为k时第i个人的编号. 1.如果令c[i]表示第