lightoj 1031 被区间dp虐哭...看题解才学会

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
Output for Sample Input
2

4
4 -10 -20 7

4
1 2 3 4
Case 1: 7
Case 2: 10

这个题我只能想到最垃圾的那种转移,开三维….
前2是区间..第三是取的次数….
就很2…
而且因为多了1位没办法记忆化…
几乎就是裸的搜索…或者说是枚举……………………….
我他妈的好菜啊………………………………………………………………………
想想都炸一脸…
就没敢写…
也幸亏没写…….
dalao的思路是随便取一下,剩下的就可以进入递归….
想想确实是这样的….
毕竟a-(b-c)=a+c-b……、
这个虽然是小学学的东西
但是我完全没有想到可以用在这里…..
用在记忆化搜索里….
用在区间dp里……..
真的是要好好体会体会……………………
思路真是比我强的不知道哪里去了

#include
#include
#include
#include
using namespace std;
int tu[101],qz[101],dp[101][101];
int dfs(int z, int y)
{
    if (dp[z][y] > -1000000)return dp[z][y];
    dp[z][y] = qz[y] - qz[z - 1];
    for (int a = z;a <= y;a++)
    {
        dp[z][y] = max(dp[z][y], qz[a] - qz[z - 1] - dfs(a+1 , y));
        dp[z][y] = max(dp[z][y], qz[y] - qz[a - 1] - dfs(z, a-1));
    }
    return dp[z][y];
}
int main()
{
    int T;
    cin >> T;
    int u=0;
    while (T--)
    {
        memset(qz, 0, sizeof(qz));
        memset(dp, 0, sizeof(dp));
        int n;
        cin >> n;
        for (int a = 0;a <= n;a++)for (int b = a+1;b <= n;b++)dp[a][b] = -1000000;
        for (int a = 1;a <= n;a++)
        {
            cin >> tu[a];
            qz[a] =qz[a - 1] + tu[a];
            dp[a][a] = tu[a];
        }
        printf("Case %d: %d\n", ++u, dfs(1, n));
    }
    return 0;
}

你可能感兴趣的:(ACM练习,动态规划)