zoukankan      html  css  js  c++  java
  • UVA

    题目连接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=19461

      Game of sum


    Description

    This is a two player game. Initially there are n integer numbers in an array and players A and B getchance to take them alternatively. Each player can take one or more numbers from the left or right endof the array but cannot take from both ends at a time. He can take as many consecutive numbers ashe wants during his time. The game ends when all numbers are taken from the array by the players.The point of each player is calculated by the summation of the numbers, which he has taken. Eachplayer tries to achieve more points from other. If both players play optimally and player A starts the

    game then how much more point can player A get than player B?


    Input

    The input consists of a number of cases. Each case starts with a line specifying the integer n (0 <
    n ≤ 100), the number of elements in the array. After that, n numbers are given for the game. Input is
    terminated by a line where n = 0.


    Output

    For each test case, print a number, which represents the maximum difference that the first player
    obtained after playing this game optimally


    Sample Input

     4

    4 -10 -20 7
    4
    1 2 3 4
    0

    Sample Output

    7

    10


    Hint

    题意:

       给你一个长度n的数组,AB两人轮流开始选一段数,至少一个,但是只能从两端开始,问你最后每个人选的权值和A最多比B多多少,A,B一样聪明

    题解:

      区间DP,dp[i][j]表示从i,j内最佳答案

    #include <iostream>
    #include <cstdio>
    #include <cmath>
    #include <cstring>
    #include <algorithm>
    using namespace std;
    const int N = 2e2+10,inf = 2e9, mod = 1e9+7;
    typedef long long ll;
    
    const ll INF = 1e18;
    
    ll dp[N][N],a[N],sum[N],n;
    
    ll dfs(int l,int r)
    {
        if(dp[l][r]!=INF)
        {
            return dp[l][r];
        }
        ll &ret=dp[l][r];
        ret=-INF;
        for(int i=l;i<r;i++)
        {
            ret = max(ret,sum[i]-sum[l-1] - dfs(i+1,r));
        }
        for(int i=r;i>l;i--)
        {
            ret = max(ret,sum[r]-sum[i-1] - dfs(l,i-1));
        }
        ret = max(ret,sum[r]-sum[l-1]);
        return ret;
    }
    int main()
    {
        while(scanf("%lld",&n)!=EOF)
        {
            if(n==0)break;
            for(int i=0;i<=n+100;i++)
                for(int j=0;j<=n+100;j++) dp[i][j]=INF;
            sum[0]=0;
            for(int i=1;i<=n;i++)
            {
                scanf("%lld",&a[i]);
                sum[i]=(sum[i-1]+a[i]);
            }
            printf("%lld
    ",dfs(1,n));
        }
        return 0;
    }
  • 相关阅读:
    go语言教程零基础入门到精通
    php探针文件内容
    一篇文章揭穿创业公司的套路
    Google资深工程师深度讲解Go语言面向接口(五)
    完全解析<atlalloc.h>
    巧妙的Section — — 剖析ATL OBJECT_MAP的自动建立
    ATL中的各种CriticalSection
    C++中的INL
    如何剖析一个类
    ATL线程模型解析
  • 原文地址:https://www.cnblogs.com/zxhl/p/5600934.html
Copyright © 2011-2022 走看看