补一道模板题(取数博弈)J. Boxes Game

题目:http://codeforces.com/gym/101502/problem/J

J. Boxes Game
time limit per test
3.0 s
memory limit per test
256 MB
input
standard input
output
standard output

Ibraheem and Husam are playing a game with group of boxes, lined next to each other on a straight line, such that 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 card inside this box to his score, then remove this box from the line.

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

Input

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

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

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

Output

For each test case, 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


题意:5组样咧
4个数组
。。。。。


取数方法:
只能从第一个或者最后一个数去走并且在数组中删除这个数
两个人分别取数 Ibraheem 和扎马洛特 Ibraheem 先开始 知道两个玩家都发挥最佳, 你的任务是计算值 x - y, 这样 x 是 Ibraheem 在游戏结束时的得分, 而 y 是扎马洛特的分数。游戏结束。


思路:

取数博弈模板,,哈哈哈哈哈哈,,别问为什么,,因为我特么也看不懂

代码:

#include<iostream>
#include<algorithm>
using namespace std;

int nums[200];
int dp[200][200]={0};
int main()
{
    int n;
    int t;
    cin>>t;
    while(t--){
    cin>>n;
    int sum=0;
    for(int i=0;i<n;++i)
    {
        cin>>nums[i];
        sum+=nums[i];
    }
    int len=0;
    //分n为奇偶分类讨论下初始情况,len是区间[st,en]的长度-1
    if(n%2==1)
    {
        for(int i=0;i<n;++i)
            dp[i][i]=nums[i];
        len = 2; //倒数下一轮的区间长度
    }
    else
    {
        for(int i=0;i<n-1;++i)
        {
            dp[i][i+1] = max(nums[i],nums[i+1])-min(nums[i],nums[i+1]);
        }
        len =3;
    }
    while(len<n)
    {
        for(int i=0;i+len<n;++i)
        {
            dp[i][i+len]=max(min(nums[i]-nums[i+1]+dp[i+2][i+len],nums[i]-nums[i+len]+dp[i+1][i+len-1]),
                min(nums[i+len]-nums[i+len-1]+dp[i][i+len-2],nums[i+len]-nums[i]+dp[i+1][i+len-1]));
        }
        len+=2;
    }
    cout<<(sum+dp[0][n-1])/2-(sum-dp[0][n-1])/2<<endl;
    }
    return 0;
}
原文地址:https://www.cnblogs.com/huangzzz/p/8342595.html