uva 10891 - Game of Sum

简介:

题意:

   A,B两人依次从数组两边拿数字,每次任选一边拿走1+个,A先手,问最后A比B大多少

代表i,j子序列先手可以取得的最大差值

转移方程为

个状态,个转移,总复杂度为结果为f(1,n)


也可设f(i,j)为子序列和,则结果为2f(1,n)-sum(n)

转移方程为 f(i,j)=sum(i,j)-min{d(i+1,j)................}

利用i.j差值递增做转移可以将复杂度降为n^2


/*
author:jxy
lang:C/C++
university:China,Xidian University
**If you need to reprint,please indicate the source**
*/
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
using namespace std;
#define inf 1000000000
bool vis[101][101];
int ans[101][101];
int n;
int sum[101];
int calc(int i,int j)
{
    if(i>j)return 0;
    if(vis[i][j])return ans[i][j];
    vis[i][j]=1;
    int t;
    int &tans=ans[i][j];
    tans=-inf;
    for(t=i+1;t<=j+1;t++)
    {
        tans=max(tans,sum[t-1]-sum[i-1]-calc(t,j));
    }
    for(t=j-1;t>=i;t--)
    {
        tans=max(tans,sum[j]-sum[t]-calc(i,t));
    }
    return tans;
}
int main()
{
    while(~scanf("%d",&n)&&n)
    {
        int i;
        sum[0]=0;
        for(i=1;i<=n;i++)
        {
            scanf("%d",&sum[i]);
            sum[i]+=sum[i-1];
        }
        memset(vis,0,sizeof(vis));
        printf("%d\n",calc(1,n));
    }
}


目录
相关文章
|
7月前
|
算法
uva 10891 game of sum
题目链接 详细请参考刘汝佳《算法竞赛入门经典训练指南》 p67
14 0
|
9月前
uva10783 Odd Sum
uva10783 Odd Sum
34 0
|
算法
[LeetCode]--55. Jump Game
Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length at that position. Dete
1239 0
|
人工智能 Java
|
人工智能 Java BI
[LeetCode] Dungeon Game
An interesting problem. The code is also short and clear. The basic idea is to use a 2d array dp[i][j] to denote the minimum hp that is required before entering dungeon[i][j].
719 0