uva 10891

题目链接:
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1832
题目思路:
题目的目的是使A得更多分。设某序列为序列a,A取最优f(a),使其变成序列b,B取最优,一直取到序列为0.
那么怎么取呢,取的这一步必定包含在从左端取一个到全部或者从右端取一个到全部取完或者不取。
即满足关系式:
d[i][j] = max(d[i][j], Sum(i, j)-min(dp(i+k, j), dp(i, j-k)));(别人写的,我随便贴的)

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#pragma warning(disable : 4996)
using namespace std;
const int N = 105;
int dp[N];
int sum[N];
int vis[N][N], d[N][N];
int n;
int f(int l, int r)
{
    //第一次提交时间超限了,注意这里可以使用记忆化搜索
    if (vis[l][r]) return d[l][r];
    vis[l][r] = 1;
    int maxn = 0;
    for (int i = l+1; i <= r; i++)
    {
        //第一次全部取完的情况没有算上
        maxn = min(maxn, f(i, r));
    }
    for (int i = l; i < r;i++)
        maxn = min(maxn, f(l, i));
    d[l][r] =sum[r]-sum[l]+dp[l]- maxn;
    return d[l][r];
}
int main()
{
    //freopen("in.txt", "r", stdin);
    while (~scanf("%d", &n)&&n)
    {
        memset(sum, 0, sizeof(sum));
        memset(vis, 0, sizeof(vis));
        bool judge = false;
        for (int i = 0; i < n; i++)
        {
            scanf("%d", &dp[i]);
            if(i>0)
                sum[i] = sum[i - 1] + dp[i];
            else
                sum[i] = dp[i];
            if (dp[i] < 0)
                judge = true;
        }
        if(!judge)
            cout << sum[n - 1] << endl;
        else
            printf("%d\n", 2 * f(0, n - 1) - sum[n - 1]);
    }
    //system("pause");
    return 0;
}

你可能感兴趣的:(uva)