Uva 10891 Game of Sum (interval game dp)

Source: Internet
Author: User

10891-game of Sum

Time limit:3.000 seconds

This is a and the player game. Initially there is n integer numbers in an array and players a and B get chance to take them A Lternatively. 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 and he wants during his time. The game ends when all numbers is taken from the array by the players. The point of each player was calculated by the summation of the numbers, which he had taken. Each player tries to achieve more points from the other. If Both players play optimally and player a starts the game then what much more point can player A get th An player B?

Input

The input consists of a number of cases. Each case is starts with a line specifying the integer n (0 < n≤100), and the number of elements in the array. After that, n numbers is 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 T His game optimally.

Sample input Output for sample input

4

4-10-20 7

4

1 2 3 4

0

7

10


Test Instructions: Given n numbers, A and B can be arbitrarily selected from both ends of the string, and can only be selected from one end at a time In the collection.

and a B all try to make the result of their choice as the largest . If A is selected first, what is the maximum of a B difference.

idea: dp[I [j] represents the maximum number of stones the player can get in the interval [I, j], then

dp[I [j] = sum[j]-sum[i-1]-min (dp[I [K], dp[K + 1] [j]), where sum is prefixed and.


#include <iostream> #include <cstdio> #include <cstring>using namespace Std;const int inf=1<<30 ; Const int Maxn=110;int dp[maxn][maxn],a[maxn],sum[maxn],n;void Initial () {for    (int i=0;i<=n;i++) for        (int j=0;j<=n;j++)            Dp[i][j]=-inf;    sum[0]=0;} void input () {    for (int i=1;i<=n;i++)    {        scanf ("%d", &a[i]);        Sum[i]=sum[i-1]+a[i];}    } int DP (int l,int r) {    if (dp[l][r]!=-inf)  return dp[l][r];    if (l==r)  return dp[l][r]=a[l];    int ANS=SUM[R]-SUM[L-1];    for (int i=l;i<r;i++)        Ans=max (Ans,sum[r]-sum[l-1]-min (DP (L,I), DP (I+1,R)));    return Dp[l][r]=ans;} int main () {    while (scanf ("%d", &n)!=eof)    {        if (n==0) break  ;        Initial ();        Input ();        DP (1,n);        printf ("%d\n", dp[1][n]-(Sum[n]-dp[1][n]));    }    return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Uva 10891 Game of Sum (interval game dp)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.