描述

Farmer John's cows like to play coin games so FJ has invented with a new two-player coin game called Xoinc for them.

Initially a stack of N (5 ≤ N ≤ 2,000) coins sits on the ground; coin i from the top has integer value Ci (1 ≤ Ci ≤ 100,000).

The first player starts the game by taking the top one or two coins (C1 and maybe C2) from the stack. If the first player takes just the top coin, the second player may take the following one or two coins in the next turn. If the first player takes two coins then the second player may take the top one, two, three or four coins from the stack. In each turn, the current player must take at least one coin and at most two times the amount of coins last taken by the opposing player. The game is over when there are no more coins to take.

Afterwards, they can use the value of the coins they have taken from the stack to buy treats from FJ, so naturally, their purpose in the game is to maximize the total value of the coins they take. Assuming the second player plays optimally to maximize his own winnings, what is the highest total value that the first player can have when the game is over?

输入

* Line 1: A single integer: N

* Lines 2..N+1: Line i+1 contains a single integer: Ci

输出

* Line 1: A single integer representing the maximum value that can be made by the first player.

样例输入

5
1
3
1
7
2

样例输出

9

题意

有两个人n枚硬币,A先手可以取1-2个,B最多可以取A*2个,问A的最大总价值。

题解

dp[i][j]表示剩下1-i,上个人取了j枚的最大总价值。

那么答案显然是dp[n][1],表示剩下1-n,上个人取了1枚(虚的),那么先手就可以取1枚或者2枚。

O(n^3)的转移dp[i][j]=max(sum[i]-dp[i-k][k])(1<=k<=2*j)。

易得dp[i][j-1]=max(sum[i]-dp[i-k][k])(1<=k<=2*j-2)。

两个相差sum[i]-dp[i-2*j][2*j]和sum[i]-dp[i-(2*j-1)][2*j-1]。

所以O(n^3)的转移可以优化一层变成O(n^2)。

dp[i][j]=(dp[i][j-1]或者max(sum[i]-dp[i-k][k])(2*j-1<=k<=2*j))。

代码

 #include<bits/stdc++.h>
using namespace std; int n,sum[],c[],dp[][];
int main()
{
scanf("%d",&n);
for(int i=n;i>=;i--)scanf("%d",&c[i]);
for(int i=;i<=n;i++)sum[i]=sum[i-]+c[i];
for(int i=;i<=n;i++)
{
for(int j=;j<=n;j++)
{
dp[i][j]=dp[i][j-];
int k=*j-;
if(k<=i)dp[i][j]=max(dp[i][j],sum[i]-dp[i-k][k]);
k++;
if(k<=i)dp[i][j]=max(dp[i][j],sum[i]-dp[i-k][k]);
}
for(int j=;j<=n;j++)
printf("%d ",dp[i][j]);
printf("\n");
} printf("%d\n",dp[n][]);
return ;
}

TZOJ 2965 A Coin Game(DP)的更多相关文章

  1. UVA.674 Coin Change (DP 完全背包)

    UVA.674 Coin Change (DP) 题意分析 有5种硬币, 面值分别为1.5.10.25.50,现在给出金额,问可以用多少种方式组成该面值. 每种硬币的数量是无限的.典型完全背包. 状态 ...

  2. 【题解】284E. Coin Troubles(dp+图论建模)

    [题解]284E. Coin Troubles(dp+图论建模) 题意就是要你跑一个完全背包,但是要求背包的方案中有个数相对大小的限制 考虑一个\(c_i<c_j\)的限制,就是一个\(c_i\ ...

  3. [HDOJ]Coin Change(DP)

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=2069 题意 有面值1,5,10,25,50的硬币数枚,对于输入的面值n,输出可凑成面值n(且限制总硬笔 ...

  4. UVA 10328 - Coin Toss dp+大数

    题目链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_proble ...

  5. LeetCode OJ 322. Coin Change DP求解

    题目链接:https://leetcode.com/problems/coin-change/ 322. Coin Change My Submissions Question Total Accep ...

  6. [HRBUST1472]Coin(dp,计数)

    题目链接:http://acm-software.hrbust.edu.cn/problem.php?id=1472 题意:给n个硬币,面值随意.问恰好凑成m元的种类数(去掉重复). dp(i,j,k ...

  7. TZOJ 5101 A Game(区间DP)

    描述 Consider the following two-player game played with a sequence of N positive integers (2 <= N & ...

  8. BZOJ2017[USACO 2009 Nov Silver 1.A Coin Game]——DP+博弈论

    题目描述 农夫约翰的奶牛喜欢玩硬币游戏,因此他发明了一种称为“Xoinc”的两人硬币游戏. 初始时,一个有N(5 <= N <= 2,000)枚硬币的堆栈放在地上,从堆顶数起的第I枚硬币的 ...

  9. TZOJ 3295 括号序列(区间DP)

    描述 给定一串字符串,只由 “[”.“]” .“(”.“)”四个字符构成.现在让你尽量少的添加括号,得到一个规则的序列. 例如:“()”.“[]”.“(())”.“([])”.“()[]”.“()[( ...

随机推荐

  1. MyBatis - 常用标签与动态Sql

    MyBatis常用标签 ● 定义sql语句:select.insert.delete.update ● 配置JAVA对象属性与查询结构及中列明对应的关系:resultMap ● 控制动态sql拼接:i ...

  2. hadoop 环境下不知道yarn端口可以通过此命令查找

    yarn jar hadoop-examples-2.6.0-mr1-cdh5.10.0.jar pi 1 30 hadoop-examples-2.6.0-mr1-cdh5.10.0.jar 此JA ...

  3. Oracle如何用单字段或多字段进行查重

    最近在整理数据形成信用报告,发现重复的数据真的多,梳理都好久.我就做个笔记把去掉重复数据的方法整理下来.方便我后期查阅. 我将我目前已知的两种去重方法分为:视图去重和表去重.原理就是有无rowid这个 ...

  4. Linux定时重启

      1.安装crontabyum install cixie-cron  yum install crontabs    2.编辑cron第一步,登陆账号第二步,输入crontab -e第三步,输入i ...

  5. [JZOJ4763] 【NOIP2016提高A组模拟9.7】旷野大计算

    题目 题目大意 给你一个数列,有很多个询问,询问一段区间内,某个数乘它的出现次数的最大值,也就是带权众数. 思考历程 第一次看到这道题,立马想到了树套树之类的二位数据结构,发现不行.(就算可以也很难打 ...

  6. Apache Flink 进阶(一):Runtime 核心机制剖析

    1. 综述 本文主要介绍 Flink Runtime 的作业执行的核心机制.首先介绍 Flink Runtime 的整体架构以及 Job 的基本执行流程,然后介绍在这个过程,Flink 是怎么进行资源 ...

  7. pytorch基础(1)

    基本数据类型和tensor import torch import numpy as np #array 和 tensor的转换 array = np.array([,]) tensorArray = ...

  8. SQLServer中使用索引视图

    在SQL Server中,视图是一个保存的T-SQL查询.视图定义由SQL Server保存,以便它能够用作一个虚拟表来简化查询,并给基表增加另一层安全.但是,它并不占用数据库的任何空间.实际上,在你 ...

  9. Composer的简介说明与安装

    https://mp.weixin.qq.com/s/aSZRhoa2-JjKOTRVhPdxdQ Composer 是 PHP 的一个依赖管理工具.它允许你申明项目所依赖的代码库,它会在你的项目中为 ...

  10. 基于SpringBoot的开源微信开发平台,Jeewx-Boot 1.0 版本发布

    项目介绍 JeewxBoot 是一款基于SpringBoot的免费微信开发平台.支持微信公众号.小程序官网.微信抽奖活动. Jeewx-Boot实现了微信公众号管理.小程序CMS.微信抽奖活动等基础功 ...