[USACO09NOV]硬幣的遊戲A Coin Game(DP)

https://daniu.luogu.org/problemnew/show/P2964ios

 

dp[i][j] 表示桌面上還剩i枚硬幣時,上一次取走了j個的最大得分git

枚舉這一次要拿k個,轉移到dp[i-k][k]  ide

dp[i][j]=max(sum[i]-dp[i-k][k])spa

由於 上一次取走j個和取走j-1個 k的取值範圍 只相差 2*j-1 和 2*jthree

因此 直接 dp[i][j]=dp[i][j-1] 遊戲

而後k分別等於  2*j-1 和 2*j,轉移get

最後輸出dp[n][1],由於先手能夠拿1個或2個,至關於上次取走1個博客

 

#include<cstdio>
#include<iostream>
#include<algorithm>

using namespace std;

#define N 2001

int dp[N][N];

int a[N],sum[N];

void read(int &x)
{
    x=0; char c=getchar();
    while(!isdigit(c))  c=getchar(); 
    while(isdigit(c)) { x=x*10+c-'0'; c=getchar();  }
}

int main()
{
    int n;
    read(n);
    for(int i=1;i<=n;++i) read(a[n-i+1]);
    for(int i=1;i<=n;++i) sum[i]=sum[i-1]+a[i];
    int k;
    for(int i=1;i<=n;++i)
        for(int j=1;j<=n;++j)
        {
            dp[i][j]=dp[i][j-1];
            k=(j<<1)-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];
}

 

題目描述

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

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).io

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).

做者: xxy
 
本文版權歸做者和博客園共有,轉載請用連接,請勿原文轉載,Thanks♪(・ω・)ノ。
相關文章
相關標籤/搜索