Anonymous_rush's blog

By Anonymous_rush, history, 8 years ago, In English

You are playing a two player game. Initially there are n integer numbers in an array and player 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 a time. He can take as many consecutive numbers as he wants during his time. The game ends when all numbers are taken from the array by the players. The point of each player is calculated by the summation of the numbers, which he has taken. Each player tries to achieve more points from other. If both players play optimally and player A starts the game then how much more point can player A get than player B?

Input

Input starts with an integer T (≤ 100), denoting the number of test cases.

Each case contains a blank line and an integer N (1 ≤ N ≤ 100) denoting the size of the array. The next line contains N space separated integers. You may assume that no number will contain more than 4 digits.

Output

For each test case, print the case number and the maximum difference that the first player obtained after playing this game optimally.

Sample Input

2

4

4 -10 -20 7

4

1 2 3 4

Output for Sample Input

Case 1: 7

Case 2: 10

  • Vote: I like it
  • +3
  • Vote: I do not like it

| Write comment?
»
8 years ago, # |
Rev. 5   Vote: I like it +3 Vote: I do not like it

let A be the given array of numbers

and let dp[i][j] be the maximum difference that the player whose turn is now can get from playing with

the sub range (A[i],A[i+1],A[i+2],....,A[j]), then the answer to the problem will be dp[0][n-1] where n

is the size the array A.

now to compute dp[i][j] we iterate over the range (i,j) by the iterator k and for each iteration we

try to give the player(whose turn is now) the prefix (A[i] + A[i+1] + .... + A[k]) once,

and we try to give him the suffix (A[k] + A[k+1] + ..... + A[j]) once and we always maximize

the dp[i][j].

this is a code to this idea :

for (int i=0;i<n;i++)

for (int j=0;j<n;j++)

    dp[i][j] = -1e14;

for (int i=0;i<n;i++) dp[i][i] = A[i];

for (int len=2;len<=n;len++){

for (int i=0;i<=n-len;i++){

     int j = i + len - 1;

     for (int k=i;k<=j;k++){

         dp[i][j] = max(dp[i][j] , f(i,k) - (k+1<=j ? dp[k+1][j] : 0));

         dp[i][j] = max(dp[i][j] , f(k,j) - (k-1>=i ? dp[i][k-1] : 0));

}
     }

}

cout<<dp[0][n-1]<<endl;

where dp[k+1][j] and dp[i][k-1] are the maximum differences that the other player(whose turn is

next) can get from the left sub ranges

and f(i,j) is a function that gives the sum (A[i] + A[i+1] + .... + A[j])

»
8 years ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

Let dp[l][r][t] be the best possible result for player t once l elements have been taken from the left and r elements have been taken from the right. The transition is quite straightforward, from [l][r][t] we can either go to [l + k][r][1 - t] or [l][r + k][1 - t], for any k in the range [1, N - l - r]. For the first player, we add the elements we take and we want to maximize the result, while for the second player we negate the elements we take and we want to minimize the result. I think the problem is clearer with recursive + memoization.

Here's the code: Easy Game