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;
    }
  • 相关阅读:
    增加samba用户提示Failed to add entry for user
    二叉树
    excel技巧
    mongodb导入json文件
    mongodb导出数据csv格式
    mongoexport导出csv中文乱码
    左连接,结果大于左面的表验证 解释
    plsql 用法和技巧
    对javaNI和NIO理解
    TinyMCE4.x整合教程-Xproer.WordPaster
  • 原文地址:https://www.cnblogs.com/zxhl/p/5600934.html
Copyright © 2011-2022 走看看