zoukankan      html  css  js  c++  java
  • luogu P3004 [USACO10DEC]宝箱Treasure Chest

    P3004 [USACO10DEC]宝箱Treasure Chest

    题目描述

    Bessie and Bonnie have found a treasure chest full of marvelous gold coins! Being cows, though, they can't just walk into a store and buy stuff, so instead they decide to have some fun with the coins.

    The N (1 <= N <= 5,000) coins, each with some value C_i (1 <= C_i <= 5,000) are placed in a straight line. Bessie and Bonnie take turns, and for each cow's turn, she takes exactly one coin off of either the left end or the right end of the line. The game ends when there are no coins left.

    Bessie and Bonnie are each trying to get as much wealth as possible for themselves. Bessie goes first. Help her figure out the maximum value she can win, assuming that both cows play optimally.

    Consider a game in which four coins are lined up with these values:

    30 25 10 35

    Consider this game sequence:

    Bessie Bonnie New Coin

    Player Side CoinValue Total Total Line

    Bessie Right 35 35 0 30 25 10

    Bonnie Left 30 35 30 25 10

    Bessie Left 25 60 30 10

    Bonnie Right 10 60 40 --

    This is the best game Bessie can play.

    贝西和伯尼找到了一个装满了金币的宝箱!但是,作为奶牛,他们不能随便进入一家商店去买东西。所以他们决定去用这些金币玩一个游戏。

    这里有N(1<=N<=5000)个硬币,每个都有一个价值C_i(1<=C_i<=5000)。这些硬币被摆成了一行。贝西和伯尼每人一回合。到了一只奶牛的回合时,他就要拿走最左边或者最右边的硬币。当没有硬币时,游戏结束。

    贝西和伯尼都想要使自己拿到的金币价值尽量高,贝西先拿。现在贝西想要你帮帮她,算出她最多可以拿多少钱(伯尼也会尽量取到最优)。

    输入输出格式

    输入格式:

    • Line 1: A single integer: N

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

    输出格式:

    • Line 1: A single integer, which is the greatest total value Bessie can win if both cows play optimally.

    输入输出样例

    输入样例#1:

    4

    30

    25

    10

    35

    输出样例#1:

    60

    思路:博弈DP。f[i][j]表示某位player在ij能取到的最大值。对于每个player,他们的更新方式是相同的,如果f[i][j]对应某一个player的最优值,那么相对应的另一个palyer的最优值,就是f[i+1][j]或f[i][j-1],如果要使f[i][j]最大,相应的就需要残局中对手的最优解最小。也就可以写出转移方程,f[i][j]=sum[j]-sum[i-1]-min(f[i+1][j],f[i][j-1]),用ij的区间和减去对手的最优值.

    说实话,貌似MLE的这一个点贼换做法。。

    所以我不要脸的下了一组数据

    下面的代码是90分的,至于那一个点,自己看吧。。没记错的话是n为5000>=……=<

    #include<bits/stdc++.h>//MLE一个点
    #define maxn 5001
    using namespace std;
    int n,a[maxn],f[maxn][maxn],sum[maxn];
    int dp(int i,int j)
    {
    	if(f[i][j])
    		return f[i][j];
    	return f[i][j]=sum[j]-sum[i-1]-min(dp(i+1,j),dp(i,j-1));
    }
    int main()
    {
    	scanf("%d",&n);
    	for(int i=1;i<=n;i++)
    	{
    		scanf("%d",&a[i]);
    		sum[i]=sum[i-1]+a[i];
    		f[i][i]=a[i]; 
    	}
    	cout<<dp(1,n);
    	return 0;
    }
    
  • 相关阅读:
    Python之路系列:面向对象初级:静态属性、静态方法、类方法
    对象和类
    Python的函数参数传递
    python参数传递:对象的引用
    Python的locals()函数
    Python 异常处理
    Python变量类型的强制转换
    日常问题总结
    高效能人士的七个习惯
    Dojo入门:DOM操作
  • 原文地址:https://www.cnblogs.com/gongcheng456/p/13548231.html
Copyright © 2011-2022 走看看