码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2017-09-14 16:36:47      阅读:212      评论:0      收藏:0      [点我收藏+]

标签:暴力   多少   isp   状态   i+1   for   gets   show   开始   

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 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:
5 
1 
3 
1 
7 
2 
输出样例#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).

技术分享
#include<iostream>
#include<cstdio>
using namespace std;
#define maxn 2010
int n,st[maxn],ans,sum[maxn];
void dfs(int pre,int who,int from,int mx){
    ans=max(ans,mx);
    if(from==n+1)return;
    for(int i=1;i<=min(pre*2,n-from+1);i++){
        if(who==0)dfs(i,1,from+i,mx);
        if(who==1)dfs(i,0,from+i,max(mx,sum[from+i-1]-sum[from-1]));
    }
}
int main(){
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        scanf("%d",&st[i]);
        sum[i]=sum[i-1]+st[i];
    }
    ans=max(sum[1],sum[2]);
    dfs(1,0,2,sum[1]);
    dfs(2,0,3,sum[2]);
    cout<<ans;
}
10分 暴力
技术分享
/*
    1.状态:建立一个二维的状态(i,j)说明拿硬币的权力到达其中一名玩家时,桌面上还剩下编号为1~i(倒序,1为最底下的) 的硬币,
    上一名玩家拿走了j枚硬币。 
    2.下一步的状态:那么这一个玩家在这一轮可以选择拿走1,2,3,4…2*j枚硬币,而他所能获得的最大硬币面值就是1~i所有的硬币面
    值之和减去他完成此次操作后(设他取走了k枚硬币)到达状态(i-k,k)的另一名玩家所能获得的最大硬币数。 
    3.状态的转移:可是因为k的取值范围很大,所以不能直接枚举,不难发现(i,j-1)和(i,j)的状态只相差两种选择的可能,即下一步取走
    2*j-1或2*j个硬币的这两种,只需要再比较一次这两种状态即可。 
    **状态转移方程:dp[i][j]=max(dp[i][j],sum[i]-dp[i-k][k],sum[i]-dp[i-k-1][k+1])(k==2*j-1);
    4.答案:答案则是在剩下1~n枚硬币时(初始状态)的dp[n][1](下一步可以选择1枚或两枚硬币)了。 
*/
#include<bits/stdc++.h>
using namespace std;
#define maxn 2010
int n,sum[maxn],c[maxn],dp[maxn][maxn]; //数组要开大一点,不然会WA; 
int main(){
    cin>>n;
    for(int i=n;i>=1;i--)cin>>c[i];//为了处理方便,我们直接逆序输入(编号自底向上) 
    for(int i=1;i<=n;i++)sum[i]+=sum[i-1]+c[i];//获取前缀和 
    for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++){
            dp[i][j]=dp[i][j-1];//重合部分 
            int k=2*j-1;
            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]);//新增状态 
        }
    cout<<dp[n][1]<<endl;
    return 0;
}
100分 dp

 

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

标签:暴力   多少   isp   状态   i+1   for   gets   show   开始   

原文地址:http://www.cnblogs.com/thmyl/p/7520625.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!