class Solution {

     public void printChoices(int[] A, int[][] C) {
System.out.println("硬币列表如下:");
for(int i=0,l=A.length; i<l; i++) {
System.out.print(A[i] + "\t");
}
System.out.println("");
final int N = C.length;
String[] names = {"本人", "对手"};
int L = 0, R = N-1, WHO = 0;
while(L<=R) {
int c = C[L][R];
System.out.println(names[WHO] + "选择第 " + c + " 个元素" + A[c]);
WHO = (WHO+1)%2;
if(L==c) {
L = L+1;
}
if(R==c) {
R = R-1;
}
if(L == R) {
break;
}
}
} public int maxMoney(int[] A) {
final int N = A.length;
int[][] C = new int[N][N];
int[][] M = new int[N][N]; // 每个子问题最优解(赚的最多钱) // 设M(i,j) 为从第i到第j个硬币中能获得的最大收入
// j-i>=2 时
// M(i,j) = max(
// A[i] + min(M(i+2,j), M(i+1,j-1)),
// A[j] + min(M(i+1,j-1), M(i,j-2)),
// ); // 初始化坐标 [i,i] 和 [i,i+1] 处的决策和最大收入
for(int i=0; i<N; i++) {
M[i][i] = A[i];
C[i][i] = i;
if(i<N - 1) {
M[i][i+1] = Integer.max(A[i], A[i+1]);
C[i][i+1] = (A[i] < A[i+1] ? i+1 : i);
}
} // 计算所有 [i,i+2] [i,i+3]... 处的决策和最大收入
for(int i=N-2; i>=0; i--) {
for(int j=i+2; j<N; j++) {
int I = A[i] + Integer.min(M[i+2][j], M[i+1][j-1]);
int J = A[j] + Integer.min(M[i+1][j-1], M[i][j-2]);
M[i][j] = Integer.max(I, J);
C[i][j] = I>J ? i : j;
}
} printChoices(A, C);
return M[0][N-1];
} public static void main(String[] args) {
Solution s = new Solution();
int[] a = {1,20,10,2};
int r = s.maxMoney(a);
System.out.println(r);
}
}

2 Player and N Coin的更多相关文章

  1. Android开发训练之第五章第五节——Resolving Cloud Save Conflicts

    Resolving Cloud Save Conflicts IN THIS DOCUMENT Get Notified of Conflicts Handle the Simple Cases De ...

  2. Coins in a Line

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

  3. Coins in a Line I

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

  4. 洛谷P2964 [USACO09NOV]硬币的游戏A Coin Game

    题目描述 Farmer John's cows like to play coin games so FJ has invented with a new two-player coin game c ...

  5. [luogu2964][USACO09NOV][硬币的游戏A Coin Game] (博弈+动态规划)

    题目描述 Farmer John's cows like to play coin games so FJ has invented with a new two-player coin game c ...

  6. E - Coin Game

    After hh has learned how to play Nim game, he begins to try another coin game which seems much easie ...

  7. HDU 3951 Coin Game (简单博弈)

    Coin Game Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

  8. [USACO09NOV]硬币的游戏A Coin Game

    https://daniu.luogu.org/problemnew/show/P2964 dp[i][j] 表示桌面上还剩i枚硬币时,上一次取走了j个的最大得分 枚举这一次要拿k个,转移到dp[i- ...

  9. Coin Game

    Problem Description After hh has learned how to play Nim game, he begins to try another coin game wh ...

随机推荐

  1. Jquery 组 tbale表格隔行变色

    <!DOCTYPE html><html lang="zh-cn"><head> <meta charset="utf-8&qu ...

  2. SAP字体调节大小

    登陆SAP 之后,菜单下面一行,最右边的那个彩色按钮(SAP GUI),点击“选项”-可视设计-字体设计-固定狂赌字体设计,点击:选择字体 即可.

  3. mysql 由decimal 引起的 Warning: Data truncated for column

    今天在使用python 库mysqldb的rawsql的时候遇到一个问题(其实并不是mysqlbean引起的) cls.raw_sql('update {table} set available_am ...

  4. Lodop设置打印维护返回打印语句代码

    打印设计关闭时,可以返回设计的打印代码,打印维护则返回成功打印的次数.不过打印维护也是返回打印程序代码的,方法就是加个语句,这个语句是: LODOP.SET_PRINT_MODE("PRIN ...

  5. codeforces439B

    Devu, the Dumb Guy CodeForces - 439B Devu is a dumb guy, his learning curve is very slow. You are su ...

  6. luogu1856

    P1856 [USACO5.5]矩形周长Picture 题目背景 墙上贴着许多形状相同的海报.照片.它们的边都是水平和垂直的.每个矩形图片可能部分或全部的覆盖了其他图片.所有矩形合并后的边长称为周长. ...

  7. Twitter数据挖掘:如何使用Python分析大数据 (3)

    让我们来拉取Twitter账号@NyTimes的最近20条微博. 我们可以创建变量来存放待拉取的微博数量(即count),以及待拉取的用户(即name).然后用这两个参数调用user_timeline ...

  8. Python的单向链表实现

    思路 链表由节点组成,先规定节点(Node),包含data和指向下个节点的next 初始化 data当然就是传入的data了,next指向None 添加 分两种情况: 链表为空,那么头节点和尾节点都指 ...

  9. 自学Linux Shell11.1-shell概述

    点击返回 自学Linux命令行与Shell脚本之路 11.1-shell概述 Shell 是一个用 C 语言编写的程序,它是用户使用 Linux 的桥梁.Shell 既是一种命令语言,又是一种程序设计 ...

  10. 【BZOJ3668】【NOI2014】起床困难综合症(贪心)

    [NOI2014]起床困难综合症(贪心) 题面 Description 21 世纪,许多人得了一种奇怪的病:起床困难综合症,其临床表现为:起床难,起床后精神不佳.作为一名青春阳光好少年,atm 一直坚 ...