Easy Game (区间DP)

Posted 1013star

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Easy Game (区间DP)相关的知识,希望对你有一定的参考价值。

Easy Game

 LightOJ - 1031 

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 Nspace 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

2

4

4 -10 -20 7

4

1 2 3 4

Sample Output

Case 1: 7

Case 2: 10

题意:有一个长度为N的整数序列,Alice和Bob轮流取数,Alice先取。每次玩家只能从左端或者右端取一个或多个数,但不能两端都取。所有数都被取走后游戏结束,然后统计每个人取走的所有数之和,作为各自的得分。两个人采取的策略都是让自己的得分尽量高,并且两个人都足够聪明。求最后结束游戏后先手和后手的得分最大差值。

题解:区间dp dp[i][j]表示的是在区间 i-j 中先手得分和后手得分的最大差值。从 i - j 遍历区间断点 k,表示在区间 [i,j] 中先手取走前 k 个,或者先手取走后 k 个所能得到的最大值。如果取走前 k 个,即先手得分为sum[k]-sum[l-1],因为先手后手都会采取最优策略,所以后手一定从剩下的区间中取走尽可能多的得分,使得差值尽可能小,那么后手的得分就是dp[k+1][r] ,同理,如果先手取走后 K 个,先手得分为sum[r]-sum[k],后手得分为dp[l][k].故dp[l][r]=max(dp[l][r],max(sum[k]-sum[l-1]-dp[k+1][r],sum[r]-sum[k]-dp[l][k]))

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 #define inf 0x3f3f3f3f
 6 using namespace std;
 7 int n;
 8 int a[110];
 9 int sum[110];
10 int dp[110][110];
11 int main()
12 {
13     int casen;
14     int ca=1;
15     int val;
16     cin>>casen;
17     while(casen--)
18     {
19         scanf("%d",&n);
20         memset(dp,-inf,sizeof(dp));
21         memset(sum,0,sizeof(sum));
22         for(int i=1;i<=n;i++)
23         {
24             scanf("%d",&val);
25             sum[i]=sum[i-1]+val;
26             dp[i][i]=val;
27         }
28         for(int len=2;len<=n;len++)
29         {
30             for(int l=1;l+len-1<=n;l++)
31             {
32                 int r=l+len-1;
33                 dp[l][r]=sum[r]-sum[l-1];
34                 for(int k=l;k<r;k++)
35                 {
36                     dp[l][r]=max(dp[l][r],max(sum[k]-sum[l-1]-dp[k+1][r],sum[r]-sum[k]-dp[l][k]));
37                 }
38             }
39         }
40         printf("Case %d: %d
",ca++,dp[1][n]);
41     }
42     return 0;
43 }

emmm....莫名失眠,也算是人生第一次通宵吧,到现在竟然一点不困,神奇~希望今天明天后天大后天大大后天的所有训练对我都友好一些~体侧跑800希望不要吐出来~~

以上是关于Easy Game (区间DP)的主要内容,如果未能解决你的问题,请参考以下文章

ZOJ3791 An Easy Game(DP)

ZOJ-3791 An Easy Game DP

UVA10891 Game of Sum(区间dp)

A Game(区间DP)

Gym - 101196:F Removal Game(区间DP)

UVA - 10891 Game of Sum (区间dp)