• 洛谷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]表示在i,j这段区间内先手能获得的最大分数;
        那么后手在先手最优方案走法下,按最优方案走的最大分数就是
        i,j这个区间总分数减去f[i][j].
    */
    #include<iostream>
    #include<cstdio>
    using namespace std;
    #define maxn 5010
    int w[maxn],sum[maxn],f[maxn][maxn],n;
    int main(){
        scanf("%d",&n);
        for(int i=1;i<=n;i++){
            scanf("%d",&w[i]);
            sum[i]=sum[i-1]+w[i];
            f[i][i]=w[i];
        }
        for(int len=2;len<=n;len++){
            for(int l=1;l+len-1<=n;l++){
                int r=l+len-1;
                f[l][r]=sum[r]-sum[l-1]-min(f[l][r-1],f[l+1][r]);
            }
        }
        printf("%d",f[1][n]);
    }
    二维dp
    #include<iostream>
    #include<cstdio>
    #define maxn 5010
    using namespace std;
    int n,sum[maxn],f[maxn];
    int main(){
        scanf("%d",&n);
        int x;
        for(int i=1;i<=n;i++){
            scanf("%d",&x);
            sum[i]=sum[i-1]+x;
            f[i]=x;
        }
        for(int len=2;len<=n;len++)
            for(int l=1;l+len-1<=n;l++){
                int r=l+len-1;
                f[l]=sum[r]-sum[l-1]-min(f[l+1],f[l]);
            }
        printf("%d",f[1]);
    }
    一维dp
  • 相关阅读:
    AQS共享锁应用之Semaphore原理
    AQS与重入锁ReetrantLock原理
    2018第23周总结
    阅读思考——被误用的敏捷和阻碍程序员成长的坏习惯
    百度云盘下载工具
    线程中断总结
    JUC包中的锁框架
    Java并发实现线程阻塞原语LockSupport
    react的类型检查PropTypes自React v15.5起已弃用,请使用prop-types
    python性能优化、内存优化、内存泄露;与其他语音比较效率如何?
  • 原文地址:https://www.cnblogs.com/thmyl/p/7577133.html
Copyright © 2020-2023  润新知