Nice one to learn: DP + Game Theory
https://lefttree.gitbooks.io/leetcode/content/dynamicProgramming2/coinsInLine2.html

In game theory, we assume the other player always take optimal step too. Combining with DP, we put this assumption together with each DP selection.

class Solution {
public:
/**
* @param values: a vector of integers
* @return: a boolean which equals to true if the first player will win
*/
bool firstWillWin(vector<int> &values) {
int n = values.size();
if(n<) return true; // Backward DP: since dp[a] -> dp[b] where a > b
vector<long long> dp(n, );
// init
dp[n-] = values[n-]; // pick 1
dp[n-] = values[n-] + values[n-]; // pick 2 for(int i = n - ; i >= ; i --)
{
int dp2 = ,dp3 = , dp4 = ;
if(i < n - ) dp4 = dp[i + ];
if(i < n - ) dp3 = dp[i + ];
if(i < n - ) dp2 = dp[i + ]; // we pick min because the other player is smart too
int v1 = values[i] + min(dp2, dp3); // pick 1
int v2 = values[i] + values[i + ] + min(dp3, dp4); // pick 2
dp[i] = max(v1, v2);
} long long suma = accumulate(values.begin(), values.end(), );
return dp[] > (suma - dp[]);
}
};

LintCode "Coins in a Line II" !的更多相关文章

  1. [LintCode] Coins in a Line II 一条线上的硬币之二

    There are n coins with different value in a line. Two players take turns to take one or two coins fr ...

  2. LintCode: coins in a line I

    有 n 个硬币排成一条线.两个参赛者轮流从右边依次拿走 1 或 2 个硬币,直到没有硬币为止.拿到最后一枚硬币的人获胜. 请判定 第一个玩家 是输还是赢? n = 1, 返回 true.n = 2, ...

  3. lintcode 394. Coins in a Line 、leetcode 292. Nim Game 、lintcode 395. Coins in a Line II

    变型:如果是最后拿走所有石子那个人输,则f[0] = true 394. Coins in a Line dp[n]表示n个石子,先手的人,是必胜还是必输.拿1个石子,2个石子之后都是必胜,则当前必败 ...

  4. [LintCode] Coins in a Line 一条线上的硬币

    There are n coins in a line. Two players take turns to take one or two coins from right side until t ...

  5. Lintcode395 Coins in a Line II solution 题解

    [题目描述] There are n coins with different value in a line. Two players take turns to take one or two c ...

  6. Coins in a Line II

    There are n coins with different value in a line. Two players take turns to take one or two coins fr ...

  7. 395. Coins in a Line II

    最后更新 这个题做得也不好,dp[n]尝试写了几下,不太对. 应该是类似于gem theory的题. 当只有1个硬币剩下的时候直接拿走,不BB. 剩俩的时候也都拿了.. dp[n]表示剩下多少个硬币. ...

  8. LintCode "Coins in a Line III" !!

    https://codesolutiony.wordpress.com/2015/05/24/lintcode-coins-in-a-line-iii/ A very juicy one! Deser ...

  9. LintCode "Coins in a Line"

    Recursion + Memorized Search(DP). And apparently, the code below can be iterative with only 3 vars - ...

随机推荐

  1. Linux Shell 命令

    (1) 操作一个文件并对文件查询行进行切分处理 (或者1-) (2) 操作文件进行去重并显示重复次数 | sort | uniq -c (3) 查看总的汇总行数 | sort | uniq -c | ...

  2. SecureCRT相关

    -------------------------------------------------- 如何解决SecureCRT汉字乱码的问题? 选择工具栏上的“选项”菜单在打开的下拉菜单中选择“会话 ...

  3. C# 入门篇之listview用法

    在用listview,经常对各种属性都不了解,用完了,下次又忘:写个随笔记记. 看到别人已经有总结很详细的,就直接转过来了,可能中间会有一些自己加的. ------------------------ ...

  4. iOS学习笔记---c语言学习第七天

      结构体 结构体是一种自定义的数据类型 struct 结构体名 { 类型说明符  成员名: … 类型说明符  成员名: }: #import <Foundation/Foundation.h& ...

  5. Java-->一个只能运行十次的程序

    --> 感觉没什么营养的样子啊... package com.dragon.java.tensoftware; import java.io.BufferedReader; import jav ...

  6. Zuoye for guo

      A  B  C  D  E  A  -  3  6  10  13  B  -  -  5  9  12  C  -  -  -  10  13  D  -  -  -  -  9  E  -  ...

  7. User_Agent_List 浏览器信息列表

    User_Agent_List = ['Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_8; en-us) AppleWebKit/534.50 (KHT ...

  8. Linux文件系统目录标准

    FHS(Filesystem Hierarchy Standard):文件层次标准 操作系统自身运行使用的 /bin: 存放可执行的二进制程序,管理员和普通用户都可以使用 /sbin:管理员才能执行的 ...

  9. URAL 1227 Rally Championship(树的直径)(无向图判环)

    1227. Rally Championship Time limit: 1.0 secondMemory limit: 64 MB A high-level international rally ...

  10. poj1420 拓扑序

    题意:给出一个表格,一部分单元格是给定的数字,而另一部分单元格则是一个式子,表示是其他一些单元格的和,让你输出最后计算出的所有格子的数. 因为有些格子需要其他格子先计算出来,所以计算顺序是按照拓扑序的 ...