• 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;
    } 
  • 相关阅读:
    docker实例之mysql的使用
    使用Dockerfile创建ssh服务的镜像02
    添加ssh服务构建新镜像-docker commit 方式01
    Keepalived
    ubuntu网卡配置
    升级openssl
    源码安装nginx env
    dockerfile
    shell字符截取
    MYSQL导入/迁移后事件不执行
  • 原文地址:https://www.cnblogs.com/FuTaimeng/p/5414438.html
Copyright © 2020-2023  润新知