GYM-101502J (take number game +DP)

Source: Internet
Author: User

title:Http://codeforces.com/gym/101502/problem/J

J. Boxes Game
Time limit per test
3.0 S
Memory limit per test
+ MB
Input
Standard input
Output
Standard output
Ibraheem and Husam is playing a game with group of boxes, lined next to all other on a straight line, such this each box Contains a card with some value written on it. Ibraheem and Husam already know the values of all cards before the game starts.

Ibraheem and Husam take turns to play the game, Ibraheem starts first. In each turn, a player can take one box from either ends of the "line", add the value of the the card inside this box to his SCO Re, then remove this box from the line.

Knowing that both players play optimally, your task was to calculate the value x-y, such that X was Ibraheem ' s score at th E End of the game, and Y is Husam's score at the end of the game.

Input
The first line contains a integer T (1≤t≤500), where T is the number of test cases.

The first line of all test case contains a integer n (1≤n≤103), where n is the number of boxes in the line.

The second line of all test case contains n integers a1, a2, ..., an (-103≤ai≤103), giving the values of the cards in Side the boxes.

Output
For each test case, the print a single line containing its answer.

Example
Input
5
4
3 1 100 5
1
1000
4
-1 100 4-5
1
-4
1
0
Output
97
1000
92
-4
0

Test Instructions:
Can only be taken from the first or last number and delete this number in the array, delete this number is his score, two people to take the number, Ibraheem and Husam Zomlot, Ibraheem first know that both players play the best, your task is calculated value x-y, X is Ibraheem in the game The score at the end, and Y is the Husam zomlot. Game over.

* * Idea: **DP (x, y) indicates the maximum score that the initiator can achieve in the interval [x, y]. When the initiator is finished, on the turn to go to the next, the back will choose the current can make him to get the maximum score of the strategy, in fact, when the initiator in the [x, Y] interval at both ends take a number, then the back face two states [X+1, y] and [X, Y-1], the initiator want to get the maximum value, will want to let the , the number of the [x, Y] interval is set to sum, and the transfer equation is DP (x, y) = MAX{SUM-DP (x+1, y), SUM-DP (x, Y-1)}. The boundary is the time when there is only one number, namely x==y.

About the game thought: When the initiator took, the back of the time, the back is actually seen as an initiator treatment, just face different state, so it is very easy to understand.

Code:

#include <cstdio> #include <cmath> #include <cctype> #include <algorithm> #include <cst ring> #include <utility> #include <string> #include <iostream> #include <map> #includ  
E <set> #include <vector> #include <queue> #include <stack> using namespace std; #pragma COMMENT (linker, "/stack:1024000000,1024000000") #define EPS 1e-10 #define INF 0x3f3f3f3f #define PI PAIR&LT  
; int, int> typedef long Long LL;  
const int MAXN = 100+5;  
int A[MAXN], DP[MAXN][MAXN], SUM[MAXN];  
    int dfs (int x, int y) {if (Dp[x][y]! =-1) return dp[x][y];  
    if (x = = y) return dp[x][y] = a[x];  
    int left = DFS (x+1, y);  
    int right = DFS (x, y-1); int tol = Sum[y]-sum[x-1];  
    [x, Y] and dp[x][y] = max (Tol-left, tol-right);  
return dp[x][y];  
    } int main () {int n;  
        while (scanf ("%d", &n) = = 1) {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));  
        int ans = DFS (1, n); printf ("%d%d\n", ans, sum[n]-ans);  
The final answer is 2*ans-sum[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.