UVa 10891 - Game of Sum 动态规划,博弈 难度: 0

题目

https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1832


题意

n个数据,A,B两个玩家轮流从两端取1到多个数字,最终数字加和为分数,分数最大的获胜,A为先手,二者都很聪明,求A分数-B分数。

 

思路

如刘书,

区间DP,明显可以用a[i][j]记录区间[i,j)的先手最大分数。

 

感想

1. 要像刘一样,在能达到要求之后进一步思考如何减少时间

 

代码

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#define LOCAL_DEBUG
using namespace std;
typedef pair<int, int> MyPair;
const int MAXN = 101;
int sum[MAXN][MAXN];
int a[MAXN][MAXN];
int mnleftA[MAXN][MAXN];
int mnrightA[MAXN][MAXN];
int g[MAXN];

int main() {
#ifdef LOCAL_DEBUG
    freopen("C:\\Users\\Iris\\source\\repos\\ACM\\ACM\\input.txt", "r", stdin);
    //freopen("C:\\Users\\Iris\\source\\repos\\ACM\\ACM\\output.txt", "w", stdout);
#endif // LOCAL_DEBUG
    int n;
    for (int ti = 1; scanf("%d", &n) == 1 && n; ti++) {
        for (int i = 0; i < n; i++)scanf("%d", g + i);
        for (int i = 0; i < n; i++) {
            sum[i][i] = 0;
            for (int j = i + 1; j <= n; j++) {
                sum[i][j] = sum[i][j - 1] + g[j - 1];
            }
        }
        for (int len = 1; len <= n; len++) {
            for (int i = 0, j = i + len; j <= n; i++, j++) {
                a[i][j] = sum[i][j] - min(mnleftA[i][j - 1], mnrightA[i + 1][j]);
                mnleftA[i][j] = min(mnleftA[i][j - 1], a[i][j]);
                mnrightA[i][j] = min(mnrightA[i + 1][j], a[i][j]);

            }
        }
        int ans = 2 * a[0][n] - sum[0][n];
        printf("%d\n", ans);
    }

    return 0;
}
View Code

 

posted @ 2019-02-24 19:27  雪溯  阅读(90)  评论(0编辑  收藏  举报