2 Player and N Coin
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的更多相关文章
- Android开发训练之第五章第五节——Resolving Cloud Save Conflicts
Resolving Cloud Save Conflicts IN THIS DOCUMENT Get Notified of Conflicts Handle the Simple Cases De ...
- Coins in a Line
Description There are n coins in a line. Two players take turns to take one or two coins from right ...
- Coins in a Line I
Description There are n coins with different value in a line. Two players take turns to take one or ...
- 洛谷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 ...
- [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 ...
- E - Coin Game
After hh has learned how to play Nim game, he begins to try another coin game which seems much easie ...
- HDU 3951 Coin Game (简单博弈)
Coin Game Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
- [USACO09NOV]硬币的游戏A Coin Game
https://daniu.luogu.org/problemnew/show/P2964 dp[i][j] 表示桌面上还剩i枚硬币时,上一次取走了j个的最大得分 枚举这一次要拿k个,转移到dp[i- ...
- Coin Game
Problem Description After hh has learned how to play Nim game, he begins to try another coin game wh ...
随机推荐
- windows上面链接使用linux上面的docker daemon
1. 修改linux 上面的 docker的 配置文件. vim /usr/lib/systemd/system/docker.service 注意 这个是centos的路径 发现ubuntu的路径不 ...
- Android Studio & HTTP Proxy
Android Studio & HTTP Proxy https://mirrors.neusoft.edu.cn/android https://mirrors.neusoft.edu.c ...
- Delphi的关键字
Constructor;构造器,定义构造函数使用Constructor关键字
- 【转】CNN卷积神经网络_ GoogLeNet 之 Inception(V1-V4)
http://blog.csdn.net/diamonjoy_zone/article/details/70576775 参考: 1. Inception[V1]: Going Deeper with ...
- Hadoop 入门
我看过的比较全的文章.赞一下 原文链接:http://www.aboutyun.com/thread-8329-1-1.html 问题导读: 1.hadoop编程需要哪些基础?2.hadoop编程需要 ...
- POJ1860(Currency Exchange)
题意: 给出一张各种货币交换的网络,问在网络中交换原有的货币,问货币能否增值? 解析: 判断是否存在正环即可 用spfa 负环和正环的判定方法一样 如果一个点的进队次数超过n次 则存在环 代码如 ...
- Maven项目读取resources下文件的路径问题(getClassLoader的作用)
读取resources下文件的方法 网上有问答如下:问: new FileInputStream("src/main/resources/all.properties") new ...
- python3网络爬虫(4):python3安装Scrapy
运行平台: Windows python版本: python3.5.2 IDE: pycharm 一.Scrapy简介 Scrapy是一个为了爬取网站数据提取结构性数据而编写的应用框架,可以应用于数 ...
- Rsync 客户端配置
安装rsync服务yum -y install rsync 创建密码文件rsync.passwordvi /etc/rsync.password只存储密码即可,无需用户名.密码为Rsync服务器端的/ ...
- python 和 scikit-learn 实现垃圾邮件过滤
文本挖掘(Text Mining,从文字中获取信息)是一个比较宽泛的概念,这一技术在如今每天都有海量文本数据生成的时代越来越受到关注.目前,在机器学习模型的帮助下,包括情绪分析,文件分类,话题分类,文 ...