题目描述

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 C_i (1 <= C_i <= 100,000).

The first player starts the game by taking the top one or two coins (C_1 and maybe C_2) 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?

MEMORY LIMIT: 20 MB

农夫约翰的奶牛喜欢玩硬币游戏.

初始时,一个有N枚硬币的堆栈放在地上,从堆顶数起的第i枚硬币的币值 为Ci

开始玩游戏时,第一个玩家可以从堆顶拿走一枚或两枚硬币.如果第一个玩家只拿走堆顶的 一枚硬币,那么第二个玩家可以拿走随后的一枚或两枚硬币.如果第一个玩家拿走两枚硬币,则第二个玩家可以拿走1,2,3,或4枚硬币.在每一轮中,当前的玩家至少拿走一枚硬币,至多拿 走对手上一次所拿硬币数量的两倍.当没有硬币可拿时,游戏结束.

两个玩家都希望拿到最多钱数的硬币.请问,当游戏结束时,第一个玩家最多能拿多少钱 呢?

输入输出格式

输入格式:

  • Line 1: A single integer: N

  • Lines 2..N+1: Line i+1 contains a single integer: C_i

输出格式:

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

输入输出样例

输入样例#1: 复制

  1. 5
  2. 1
  3. 3
  4. 1
  5. 7
  6. 2

输出样例#1: 复制

  1. 9

说明

There are five coins with the values 1, 3, 1, 7, and 2.

The first player starts by taking a single coin (value 1). The opponent takes one coin as well (value 3). The first player takes two more coins (values 1 and 7 — total 9). The second player gets the leftover coin (value 2— total 5).

思路

用$f[i][j]$表示上一个人拿了j个,还剩1~i个时,能拿到的最大硬币数;

  • 枚举现在要拿k个
    转移方程$$f[i][j]=max(f[i][j],sum[i]-f[i-k][k])$$
    因为$f[i][j-1]$已经算了一大片了,所以k只要枚举$j*2-1$和$j*2$就可以
    答案就是$f[n][1]$

代码

  1. 1 #include<cmath>
  2. 2 #include<cstdio>
  3. 3 #include<string>
  4. 4 #include<cstring>
  5. 5 #include<iostream>
  6. 6 #include<algorithm>
  7. 7 #define re register int
  8. 8 using namespace std;
  9. 9 const int maxn=2e3+10;
  10. 10 inline int read(){
  11. 11 int x=0,w=1;
  12. 12 char ch=getchar();
  13. 13 while(ch!='-'&&(ch<'0'||ch>'9')) ch=getchar();
  14. 14 if(ch=='-') w=-1,ch=getchar();
  15. 15 while(ch>='0'&&ch<='9') x=(x<<1)+(x<<3)+ch-48,ch=getchar();
  16. 16 return x*w;
  17. 17 }
  18. 18 int n,sum[maxn],c[maxn],dp[maxn][maxn];
  19. 19 int main(){
  20. 20 freopen("p2964.in","r",stdin);
  21. 21 freopen("p2964.out","w",stdout);
  22. 22 n=read();
  23. 23 for(re i=n;i>=1;--i) c[i]=read();
  24. 24 for(re i=1;i<=n;++i) sum[i]+=sum[i-1]+c[i];
  25. 25 for(re i=1;i<=n;++i)
  26. 26 for(re j=1;j<=n;++j){
  27. 27 dp[i][j]=dp[i][j-1];
  28. 28 int k=2*j-1;
  29. 29 if(k<=i) dp[i][j]=max(dp[i][j],sum[i]-dp[i-k][k]);
  30. 30 k++;
  31. 31 if(k<=i) dp[i][j]=max(dp[i][j],sum[i]-dp[i-k][k]);
  32. 32 }
  33. 33 printf("%d\n",dp[n][1]);
  34. 34 return 0;
  35. 35 }

c++

  1. #include<cmath>
  2. #include<cstdio>
  3. #include<string>
  4. #include<cstring>
  5. #include<iostream>
  6. #include<algorithm>
  7. #define re register int
  8. using namespace std;
  9. const int maxn=2e3+10;
  10. inline int read(){
  11. int x=0,w=1;
  12. char ch=getchar();
  13. while(ch!='-'&&(ch<'0'||ch>'9')) ch=getchar();
  14. if(ch=='-') w=-1,ch=getchar();
  15. while(ch>='0'&&ch<='9') x=(x<<1)+(x<<3)+ch-48,ch=getchar();
  16. return x*w;
  17. }
  18. int n,sum[maxn],c[maxn],dp[maxn][maxn];
  19. int main(){
  20. freopen("p2964.in","r",stdin);
  21. freopen("p2964.out","w",stdout);
  22. n=read();
  23. for(re i=n;i>=1;--i) c[i]=read();
  24. for(re i=1;i<=n;++i) sum[i]+=sum[i-1]+c[i];
  25. for(re i=1;i<=n;++i)
  26. for(re j=1;j<=n;++j){
  27. dp[i][j]=dp[i][j-1];
  28. int k=2*j-1;
  29. if(k<=i) dp[i][j]=max(dp[i][j],sum[i]-dp[i-k][k]);
  30. k++;
  31. if(k<=i) dp[i][j]=max(dp[i][j],sum[i]-dp[i-k][k]);
  32. }
  33. printf("%d\n",dp[n][1]);
  34. return 0;
  35. }

【题解】Luogu p2964 BZOJ 2017[Usaco2009 Nov]硬币游戏的更多相关文章

  1. bzoj 2017 [Usaco2009 Nov]硬币游戏 动态规划

    [Usaco2009 Nov]硬币游戏 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 431  Solved: 240[Submit][Status] ...

  2. [bzoj 2017] [Usaco2009 Nov]硬币游戏

    一个多月没更博客了..(期间明白了自己有多傻逼. 这种问题大概就倒着做... f[i][j]:表示考虑剩下的硬币i..n,且之前的人取了j个时,先手最多拿到的钱数.aft[i]:表示硬币i..n的总钱 ...

  3. bzoj 2017: [Usaco2009 Nov]硬币游戏【dp】

    废了废了,一个小dp都想不出来 把c数组倒序一下,变成1在最下,设f[i][j]为某一人取完j个之后还剩1~i的硬币,转移的话应该是f[i][j]=max(s[i]-f[i-k][k]),就是1~n的 ...

  4. 【BZOJ】2017: [Usaco2009 Nov]硬币游戏(dp+神题+博弈论)

    http://www.lydsy.com/JudgeOnline/problem.php?id=2017 这题太神了,我想了一个中午啊 原来是看错题一直没理解题解说的,一直以为题解是错的QAQ “开始 ...

  5. BZOJ_2017_[Usaco2009 Nov]硬币游戏_博弈论+DP

    BZOJ_2017_[Usaco2009 Nov]硬币游戏_博弈论+DP Description 农夫约翰的奶牛喜欢玩硬币游戏,因此他发明了一种称为“Xoinc”的两人硬币游戏. 初始时,一个有N(5 ...

  6. [BZOJ2017][Usaco2009 Nov]硬币游戏

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

  7. [BZOJ2017][Usaco2009 Nov]硬币游戏(要复习系列)

    又是DP? 好吧,或者说是博弈论,但是我不会啊. 先搞个O(n^3)的记忆化搜索,然后瞎搞好像发现两个状态几乎一样? 竟然过了样例,然后竟然A了... #include<iostream> ...

  8. BZOJ 1770: [Usaco2009 Nov]lights 燈( 高斯消元 )

    高斯消元解xor方程组...暴搜自由元+最优性剪枝 -------------------------------------------------------------------------- ...

  9. BZOJ 2019 [Usaco2009 Nov]找工作:spfa【最长路】【判正环】

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2019 题意: 奶牛们没钱了,正在找工作.农夫约翰知道后,希望奶牛们四处转转,碰碰运气. 而 ...

随机推荐

  1. 接口测试原理及Postman详解

    接口测试定义 接口是前后端沟通的桥梁,是数据传输的通道,包括外部接口.内部接口.内部接口又包括:上层服务与下层服务接口,同级接口 生活中常见接口:电脑上的键盘.USB接口,电梯按钮,KFC下单 接口测 ...

  2. java使用户EasyExcel导入导出excel

    使用alibab的EasyExce完成导入导出excel 一.准备工作 1.导包 <!-- poi 相关--> <dependency> <groupId>org. ...

  3. 面试题:ArrayList、LinkedList、Vector三者的异同?

    面试题:ArrayList.LinkedList.Vector三者的异同? 同:三个类都是实现了List接口(Collection的子接口之一),存储数据的特点相同:存储有序的.可重复的数据不同: * ...

  4. jQuery的入口和jQurey的对象切换

    jQuery jQuery的导入 通过script标签的src属性,link标签是导入层叠样式表 jQuery和原生JS的入口函数 1.jQ在页面结构加载完毕就会执行 原生JS的入口函数是等图片.层叠 ...

  5. top,它们的意思分别是1分钟、5分钟、15分钟内系统的平均负荷。

    理解Linux系统负荷   作者: 阮一峰 日期: 2011年7月31日   一.查看系统负荷 如果你的电脑很慢,你或许想查看一下,它的工作量是否太大了. 在Linux系统中,我们一般使用uptime ...

  6. 更换介质:请把标有Debian ... 的盘片插入驱动器

    Debian安装软件报错如下: 更换介质:请把标有 "Debian GNU/Linux 10.8.0 _Buster_ - Official amd64 DVD Binary-1 20210 ...

  7. Linux_配置主DNS服务(基础)

    [RHEL8]-DNSserver:[Centos7.4]-DNSclient !!!测试环境我们首关闭防火墙和selinux(DNSserver和DNSclient都需要) [root@localh ...

  8. 拉勾、Boss直聘、内推、100offer

    BOSS直聘 拉勾.Boss直聘.内推.100offer  

  9. ScreenToGif: 屏幕录制神器

    ScreenToGif:一款小众但很好用的屏幕录制神器  牛人干货 2020-01-07 00:23:08 今天干货君给大家介绍一款电脑屏幕录制神器-ScreenToGif . ScreenToGif ...

  10. IDEA 安装 zookeeper 可视化管理插件

    1. 安装 zookeeper 插件 打开 IDEA->Settings->Plugins,然后在 Marketplace 输入 "zookeeper" 如下: 插件安 ...