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. pandas重新索引

    #重新索引会更改DataFrame的行标签和列标签.重新索引意味着符合数据以匹配特定轴上的一组给定的标签. #可以通过索引来实现多个操作 - #重新排序现有数据以匹配一组新的标签. #在没有标签数据的 ...

  2. spring学习总结(一)_Ioc基础(上)

    最近经历了许许多多的事情,学习荒废了很久.自己的目标成了摆设.现在要奋起直追了.最近发现了张果的博客.应该是一个教师.看了他写的spring系列的博客,写的不错.于是本文的内容参考自他的博客,当然都是 ...

  3. Delphi导出数据的多种方法

    //Dxdbgrid,则直接用SaveToexcel即可//使用 ExcelWithOdbc 控件function TDataModule1.GetDataToFile(DsData: TObject ...

  4. html 头部 head

    head里面包含标签有: title:html名称,每个html文档都必须有 形式:<title>名字</title>,文档区不显示,浏览器可以识别: 浏览器工具栏显示的页面标 ...

  5. codeforces625C

    K-special Tables CodeForces - 625C 人们经常做一些疯狂的事来凸显自己.有的人跳舞,有的人撩妹,有的人立志成为顶级程序猿(例如某peng),还有的人喜欢收集有趣的数学对 ...

  6. 部署AWStats分析系统

    介绍 AWStats是使用Prel语言开发的一款开源日志分析系统,它不仅可以用来分析Apache网站服务器的访问日志,也可以用来分析Samba.Vsftpd.IIS等服务的日志信息. AWStats软 ...

  7. BZOJ5252 八省联考2018林克卡特树(动态规划+wqs二分)

    假设已经linkcut完了树,答案显然是树的直径.那么考虑这条直径在原树中是怎样的.容易想到其是由原树中恰好k+1条点不相交的链(包括单个点)拼接而成的.因为这样的链显然可以通过linkcut拼接起来 ...

  8. M - Help Hanzo LightOJ - 1197 (大区间求素数)

    题意: 求[a,b]之间的素数的个数 数很大...数组开不起 所以要想到转化 因为小于等于b的合数的最小质因子 一定小于等于sqrt(b),所以只需要求出来[0,sqrt(b)]的素数  然后取倍数删 ...

  9. 自学Linux Shell12.7-控制循环break、continue命令

    点击返回 自学Linux命令行与Shell脚本之路 12.7-控制循环break.continue命令 break命令.break命令用于跳出循环,使用break可以跳出任何类型的循环:for.whi ...

  10. 【BZOJ2000】[HNOI2000]取石头游戏(贪心,博弈论)

    [BZOJ2000][HNOI2000]取石头游戏(贪心,博弈论) 题面 BZOJ 洛谷 题解 这题好神仙啊,窝不会QaQ. 假装一下只有三个元素\(a_{i-1},a_i,a_{i+1}\),并且满 ...