UVA 10891 Game of Sum (DP)

Source: Internet
Author: User

UVA 10891 Game of Sum (DP)


This is a and the player game. Initially there is n integer numbers in an array and playersA and B get chance to take them al Ternatively. 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 playerA get tha N player B?

Input

The input consists of a number of cases. Each case is starts with a line specifying the integern (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 wheren=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




The main idea : there are n Stones lined up in a row, and then there are two people to play games, each time you can from the two sides (left or right) at either end of a number of stones taken away (to obtain value for taking the sum of stones), But he took away the way must let him at the end of the game value as high as possible, two people are very smart, so each round will be in accordance with their most advantageous way to take the number, please calculate at the end of the game, first take the number of people value and after the value of the difference between the number of people . Solution idea: The DP function returns the I to J Player 1 to get the maximum value.
#include <stdio.h> #include <stdlib.h> #include <algorithm> #include <string.h>using namespace Std;int s[105], a[105], d[105][105], vis[105][105], n;int dp (int i, int j) {//DP function return I to J Player 1 can get maximum if (Vis[i][j]) return D[I][J];VIS[I][J] = 1;int m = 0;for (int k = i + 1; k <= J; k++) {m = min (M, DP (K, j));} for (int k = i; k < J; k++) {m = min (M, DP (i, k));} D[I][J] = s[j]-s[i-1]-M;return d[i][j];} int main () {while (scanf ("%d", &n), N) {memset (Vis, 0, sizeof (VIS)); S[0] = 0;for (int i = 1; I <= n; i++) {scanf ("%d", &a[i]); S[i] = S[i-1] + a[i];} printf ("%d\n", 2 * DP (1, N)-s[n]); DP (1, N)-(S[n]-DP (1, N))}return 0;}


UVA 10891 Game of Sum (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.