zoukankan      html  css  js  c++  java
  • Uva-10891-Game of Sum

    问题描述:

    This is a two player game. Initially there are n integer numbers in an array and players A and B get chance to take them alternatively. Each player can take one or more numbers from the left or right end of the array but cannot take from both ends at a time. He can take as many consecutive numbers as he 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. Each player 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?

    输入

    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.

    输出

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

    样例输入

    4
    4 -1 -20 7
     
    4
    1 2 3 4

    样例输出

    7
    10
     

    用d(i,j) 表示原 序列 [i...j] 在双方都采取最优的策略的情况下,先手得分的最大值(先手是相对的,指在当前局面下的第一个取的人)。

    对方肯定有一种可能是得0分,因为先手可以取完所有的。

    d(i,j) = sum(i,j) – min{ d(i+1,j), d(i+2,j), … , d(j,j) , d(i,j-1), d(i,j-2), … , d(i,i) , 0 }

    如何理解这个方程呢?

    在某一个局面下,总和是一定的,所以一个人的得分越高,另一个人的得分越低。

    那么我当前的最优决策是让对方在后面的所有步骤中取得的得分和最低,即min{ d(i+1,j), d(i+2,j), … , d(j,j) , d(i,j-1), d(i,j-2), … , d(i,i) , 0 }

    注意min里面的d()先手其实换人了,如d(i+1,j)就是我从左边取一个,把i+1~j的序列让给对手。其他的一个意思.

    那么我的最优得分即d(i,j)就是sum(i,j) – min{ d(i+1,j), d(i+2,j), … , d(j,j) , d(i,j-1), d(i,j-2), … , d(i,i) , 0 }

    最后的结果是d(i,j)-(sum(1,n)-d(i,j))即2*d(i,j)-sum(1,n)

    此时程序的复杂度为O(n^3),可以加一个优化,使复杂度降为O(n^2)

    设f(i,j)为min(d[i][j],d[i+1][j],d[i+2][j]+......+d[j][j])

    g(i,j)为min(d[i][j],d[i][j-1],d[i][j-2]+......+d[i][i])

    则d[i][j]=sum(i,j)-min(0,f(i+1,j),g(i,j-1))

    f和g可以O1推断出来:

    f[i][j]=min(d[i][j],f[i+1][j])

    g[i][j]=min(d[i][j],f[i][j-1])

    因此程序降为n^2

    优化后代码如下:

    #include<iostream>
    #define Size 1005
    using namespace std;
    
    int n;
    int a[Size];
    int s[Size];
    int d[Size][Size],f[Size][Size],g[Size][Size];
    
    inline int sum(int l,int r){ return s[r]-s[l-1]; }
    
    int main(){
        cin>>n;
        for(int i=1;i<=n;i++){
            cin>>a[i];
        }
        s[0]=0;
        for(int i=1;i<=n;i++){
            s[i]=s[i-1]+a[i];
        }
        for(int i=1;i<=n;i++){//边界 
            d[i][i]=f[i][i]=g[i][i]=a[i];
        }
        for(int L=1;L<=n;L++){
            for(int i=1;i+L<=n;i++){
                int j=i+L;
                
                int m=0;//m=min(0,f(i+1,j),g(i,j-1))
                m=min(m,f[i+1][j]);
                m=min(m,g[i][j-1]);
                
                d[i][j]=sum(i,j)-m;
                
                f[i][j]=min(d[i][j],f[i+1][j]);
                g[i][j]=min(d[i][j],g[i][j-1]);
            }
        } 
        cout<<2*d[1][n]-sum(1,n);
        
        return 0;
    } 
  • 相关阅读:
    如何查看存储过程中动态生成的sql
    [原] 高淇Java300集系列笔记 (待续)
    [原] 在HTML文档中添加标签名、ID、类名
    jquery学习笔记1-Ajax跨站请求资源
    博客搬家啦。请访问我的新底盘www.boyipark.com
    web端跨域调用webapi
    遮罩层
    去掉字符串后最后一个 ","
    对JAVA的static深刻理解(结合C语言的思考)
    java事件响应方法汇总(容器类监听、监听器类、AbstractAction、反射)
  • 原文地址:https://www.cnblogs.com/FuTaimeng/p/5414438.html
Copyright © 2011-2022 走看看