zoukankan      html  css  js  c++  java
  • 09_Sum游戏(UVa 10891 Game of Sum)

    问题来源:刘汝佳《算法竞赛入门经典--训练指南》 P67 例题28:

    问题描述:有一个长度为n的整数序列,两个游戏者A和B轮流取数,A先取,每次可以从左端或者右端取一个或多个数,但不能两端都取,所有数都被取完时游戏结束,然后统计每个人取走的所有数字之和作为得分,两人的策略都是使自己的得分尽可能高,并且都足够聪明,求A的得分减去B的得分的结果。

    问题分析:1.设dp[i][j]表示从第i第j的数的序列中,双方都采取最优策略的前提下,先手得分的最大值

           2.若求dp[i][j],我们可以枚举从左边(或者右边)取多少个数,并求枚举过程中dp[i][j]的最大值,则有状态转移方程

              dp[i][j] = sum[i][j] - Min{dp[i+1][j],dp[i+2][j],...,dp[j][j],  dp[i][i],dp[i][i+1],...,dp[i][j-1]}

          (其中sum[i][j] 表示从i到j的序列和)

    例题链接:...

    例题:UVa 10891

    10891 - Game of Sum

    Time limit: 3.000 seconds

     

      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?

    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                                   Output for Sample Input

    4

    4 -10 -20 7

    4

    1 2 3 4

    0

    7

    10

    O(n3)代码:

     1 #include "stdio.h"
     2 #include "string.h"
     3 #define N 105
     4 int a[N];
     5 int sum[N];
     6 int dp[N][N],mark[N][N];
     7 
     8 int inline Min(int a,int b) { return a<b?a:b; }
     9 
    10 int DP(int i,int j) //记忆化搜索
    11 {
    12     if(mark[i][j])
    13         return dp[i][j];
    14     mark[i][j] = 1;
    15     int m = 0;  //全部取光
    16     for(int k=i+1; k<=j; k++)
    17         m = Min(m,DP(k,j));
    18     for(int k=j-1; k>=i; k--)
    19         m = Min(m,DP(i,k));
    20     dp[i][j] = sum[j] - sum[i-1] - m;
    21     return dp[i][j];
    22 }
    23 
    24 int main()
    25 {
    26     int n;
    27     int i;
    28     while(scanf("%d",&n),n!=0)
    29     {
    30         for(i=1; i<=n; i++)
    31             scanf("%d",&a[i]);
    32         memset(sum,0,sizeof(sum));
    33         for(i=1; i<=n; i++)
    34             sum[i] = sum[i-1] + a[i];
    35         memset(dp,0,sizeof(dp));
    36         memset(mark,0,sizeof(mark));  //标记初始化
    37         printf("%d
    ",2*DP(1,n)-sum[n]);
    38     }
    39     return 0;
    40 }
  • 相关阅读:
    Spring MVC异常处理
    Spring MVC静态资源放行
    SpringMVC 接受前端传递的数据
    Eclipse+Maven构建SpringMVC+log4j2
    Eclipse+Maven构建SpringMVC项目
    log4j2
    Centos7 / RHEL 7 双网卡绑定
    CentOS7安装vncserver
    CentOS7修改ssh端口
    Linux配置Oracle 11g自动启动
  • 原文地址:https://www.cnblogs.com/ruo-yu/p/4388130.html
Copyright © 2011-2022 走看看