zoukankan      html  css  js  c++  java
  • UVA 10891 Game of Sum(DP)

    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, nnumbers 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.

    题目大意:给n个数,两个人轮流取数,可以从左往右或从右往左取任意多个。两个人都希望自己的取得的数的总和尽量大,都采取最优策略,问第一个人能比第二个人取得的数多多少。

    思路:很容易可以想到一个$O(n^3)$的DP,用dp[i][j]代表只剩下a[i..j]的数,先手可以取得的最大值,此时后手取得的最大值为sum[i..j] - dp[i][j]。

    那么状态转移方程为:dp[i][j] = max(sum[i..j], sum[i..j] - min(dp[i+1][j], dp[i+2][j]……), sum[i..j] - min(dp[i][j - 1], dp[i, j - 2])。

    输出结果为2 * dp[1][n] - sum[1..n]。

    代码(0.026S):

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <algorithm>
     5 using namespace std;
     6 
     7 const int MAXN = 110;
     8 
     9 int dp[MAXN][MAXN];
    10 int a[MAXN], sum[MAXN];
    11 int n;
    12 
    13 int main() {
    14     while(scanf("%d", &n) != EOF && n) {
    15         for(int i = 1; i <= n; ++i) scanf("%d", a + i);
    16         for(int i = 1; i <= n; ++i) sum[i] = sum[i - 1] + a[i];
    17         for(int k = 0; k < n; ++k) {
    18             for(int i = 1; i + k <= n; ++i) {
    19                 int j = i + k;
    20                 dp[i][j] = sum[j] - sum[i - 1];
    21                 for(int p = i + 1; p <= j; ++p) dp[i][j] = max(dp[i][j], sum[j] - sum[i - 1] - dp[p][j]);
    22                 for(int p = j - 1; p >= i; --p) dp[i][j] = max(dp[i][j], sum[j] - sum[i - 1] - dp[i][p]);
    23             }
    24         }
    25         printf("%d
    ", 2 * dp[1][n] - sum[n]);
    26     }
    27 }
    View Code

    这个DP还有优化的余地,观察状态转移方程可以发现,dp[i][j]使用了min(dp[i+1][j], dp[i+2][j]……),而dp[i+1][j]=min(dp[i+2][j], dp[i+3][j]……),有重复的部分。

    于是我们可以用l[i][j]记录max(dp[i][j], dp[i+1][j], dp[i+2][j]……),即从左往右取的后手最小值,则sum[i..j] - min(dp[i+1][j], dp[i+2][j]……)可以写成sum[i..j]-l[i+1][j]。每次更新l[i][j] = min(dp[i][j], l[i+1][j])。

    同理用r[i][j]记录从右往左取的后手最小值。

    至此DP优化至$O(n^2)$。

    代码(0.015S):

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <algorithm>
     5 using namespace std;
     6 
     7 const int MAXN = 110;
     8 
     9 int dp[MAXN][MAXN];
    10 int l[MAXN][MAXN], r[MAXN][MAXN];
    11 int a[MAXN], sum[MAXN];
    12 int n;
    13 
    14 int main() {
    15     while(scanf("%d", &n) != EOF && n) {
    16         for(int i = 1; i <= n; ++i) scanf("%d", a + i);
    17         for(int i = 1; i <= n; ++i) sum[i] = sum[i - 1] + a[i];
    18         for(int k = 0; k < n; ++k) {
    19             for(int i = 1; i + k <= n; ++i) {
    20                 int j = i + k;
    21                 l[i][j] = r[i][j] = dp[i][j] = sum[j] - sum[i - 1];
    22                 if(i != j) {
    23                     dp[i][j] = max(dp[i][j], sum[j] - sum[i - 1] - l[i + 1][j]);
    24                     dp[i][j] = max(dp[i][j], sum[j] - sum[i - 1] - r[i][j - 1]);
    25                     l[i][j] = min(dp[i][j], l[i + 1][j]);
    26                     r[i][j] = min(dp[i][j], r[i][j - 1]);
    27                 }
    28             }
    29         }
    30         printf("%d
    ", 2 * dp[1][n] - sum[n]);
    31     }
    32 }
    View Code
  • 相关阅读:
    分享下有趣的注释头
    android studio 的自动更新问题
    docker 搭建kafka集群(入门版)
    brew换源
    golang web框架 kratos中的日志框架
    golang 日志框架(zap)完整配置和使用
    python 日志模块
    mysqldump备份恢复数据
    寻找二叉树上从根结点到给定结点的路径
    linux 磁盘IO速度测试
  • 原文地址:https://www.cnblogs.com/oyking/p/3699017.html
Copyright © 2011-2022 走看看