UVA 10891 Game of Sum (game theory + interval DP)

Source: Internet
Author: User
Tags integer numbers time limit

Game of Sum
Time limit:3000ms Memory Limit:unknown 64bit IO Format:%lld &%llu
Submit Status

Description

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 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 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 other. If Both players play optimally and player A starts the
Game then what much more point can player A get than player B?
Input
The input consists of a number of cases. Each case starts with a line specifying the integer n (0 <
n), 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 RST player
Obtained after playing this game optimally.
Sample Input
4
4-10-20 7
4
1 2 3 4
0
Sample Output
7
10




Parsing: Classic Interval DP

Same HDU 4597 Play game (game + interval DP)

DP[L][R] Indicates the maximum value that can be reached when the interval is left L~r

Sum[i] represents all numbers from the range start point to the current point and

There are four cases of each fetch:

Take from the leftmost end of the sequence

Take from the right end of the sequence

Therefore, in the case of selecting the number K (where 0<=k<=r-i+1) is given, Dp[l][r] is

DP[L+K][R]

DP[L][R-K]
Transferred.
For the current is a take, then the last is a B take, because the total number of l~r interval sum[r]-SUM[L-1] is fixed, want to make a maximum, that is, a to select the last B's minimum selection for state transfer, that is
Dp[l][r] = (Sum[r]-sum[l-1])-min{dp[l+k][r], Dp[l][r-k]}

Because there are a lot of repeating states, add a memory




AC Code:

#include <bits/stdc++.h>
using namespace std;

int dp[102][102];
int sum[102];
int a[102];
int n;

int solve (int l, int r) {
    if (dp[l][r]! =-1) return dp[l][r];
    if (L > R) return dp[l][r] = 0;
    int Sum = Sum[r]-SUM[L-1];
    int ans = -123456789;
    for (int k=1; k<=r-l+1; k++)    //Enumeration Select the number of
        ans = max (ans, sum-min (Solve (L+k, R), solve (L, r-k)));
    return dp[l][r] = ans;
}

int main () {
    #ifdef sxk
        freopen ("In.txt", "R", stdin);
    #endif//Sxk
    while (scanf ("%d", &n) = = 1 && N) {
        sum[0] = 0;
        for (int i=1; i<=n; i++) {
            scanf ("%d", &a[i]);
            Sum[i] = Sum[i-1] + a[i];
        }
        Memset (DP,-1, sizeof (DP));
        printf ("%d\n", Solve (1, N)-(Sum[n]-sum[0]-Solve (1, n)));
    }
    return 0;
}


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.