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));
    }
}
时间: 2024-11-19 03:03:54

uva 10891 - Game of Sum的相关文章

UVa 108:Maximum Sum

链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=44 原题: Background A problem that is simple to solve in one dimension is often much more difficult to solve in more than one dim

UVa 10827:Maximum sum on a torus

题目链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=1768 原题: A grid that wraps both horizontally and vertically is called a torus. Given a torus where each cell contains an inte

算法题之UVA 10891

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

UVa 10656 Maximum Sum (II) (water ver.)

10656 - Maximum Sum (II) Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=1597 In a given a sequence of non-negative integers you will have to find such a

UVa 10783 Odd Sum (water ver.)

10783 - Odd Sum Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=19&page=show_problem&problem=1724 Given a range [a, b], you are to find the summation of all the odd integers in this r

UVa 12640 Largest Sum Game (water ver.)

12640 - Largest Sum Game Time limit: 1.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=602&page=show_problem&problem=4388 题目:http://uva.onlinejudge.org/external/126/12640.pdf 主要是读取数据怎么处理. 完整代码: /*0

UVa 11028 Sum of Product:A007773

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=1969 算了前几个发现是A007773 还给出了个公式:当n>=7, a(n) = (n^3-16n+27)/6 (n是奇数); (n^3-16n+30)/6 (n是偶数).(这谁看的出来!) 完整代码: 01./*0.012s*/ 02. 03.#inc

UVa 10791 Minimum Sum LCM (数论&amp;amp;素因子分解)

10791 - Minimum Sum LCM Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php? option=com_onlinejudge&Itemid=8&category=467&page=show_problem&problem=17 32 LCM (Least Common Multiple) of a set of integers is defined as the minimum

算法题之UVA 763

Fibinary Numbers The standard interpretation of the binary number 1010 is 8 + 2 = 10. An alternate way to view the sequence ``1010'' is to use Fibonacci numbers as bases instead of powers of two. For this problem, the terms of the Fibonacci sequence