UVA 10891 Game of Sum(DP)
This is a two player game. Initially there are n integer numbers in an array and players 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
The input consists of a number of cases. Each case starts with a line specifying the integer n (0 < n ≤100), the number of elements in the array. After that, nnumbers are given for the game. Input is terminated by a line where n=0.
Output
For each test case, print a number, which represents the maximum difference that the first player obtained after playing this game optimally.
题目大意:给n个数,两个人轮流取数,可以从左往右或从右往左取任意多个。两个人都希望自己的取得的数的总和尽量大,都采取最优策略,问第一个人能比第二个人取得的数多多少。
思路:很容易可以想到一个$O(n^3)$的DP,用dp[i][j]代表只剩下a[i..j]的数,先手可以取得的最大值,此时后手取得的最大值为sum[i..j] - dp[i][j]。
那么状态转移方程为:dp[i][j] = max(sum[i..j], sum[i..j] - min(dp[i+1][j], dp[i+2][j]……), sum[i..j] - min(dp[i][j - 1], dp[i, j - 2])。
输出结果为2 * dp[1][n] - sum[1..n]。
代码(0.026S):
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 using namespace std; 6 7 const int MAXN = 110; 8 9 int dp[MAXN][MAXN]; 10 int a[MAXN], sum[MAXN]; 11 int n; 12 13 int main() { 14 while(scanf("%d", &n) != EOF && n) { 15 for(int i = 1; i <= n; ++i) scanf("%d", a + i); 16 for(int i = 1; i <= n; ++i) sum[i] = sum[i - 1] + a[i]; 17 for(int k = 0; k < n; ++k) { 18 for(int i = 1; i + k <= n; ++i) { 19 int j = i + k; 20 dp[i][j] = sum[j] - sum[i - 1]; 21 for(int p = i + 1; p <= j; ++p) dp[i][j] = max(dp[i][j], sum[j] - sum[i - 1] - dp[p][j]); 22 for(int p = j - 1; p >= i; --p) dp[i][j] = max(dp[i][j], sum[j] - sum[i - 1] - dp[i][p]); 23 } 24 } 25 printf("%d\n", 2 * dp[1][n] - sum[n]); 26 } 27 }
这个DP还有优化的余地,观察状态转移方程可以发现,dp[i][j]使用了min(dp[i+1][j], dp[i+2][j]……),而dp[i+1][j]=min(dp[i+2][j], dp[i+3][j]……),有重复的部分。
于是我们可以用l[i][j]记录max(dp[i][j], dp[i+1][j], dp[i+2][j]……),即从左往右取的后手最小值,则sum[i..j] - min(dp[i+1][j], dp[i+2][j]……)可以写成sum[i..j]-l[i+1][j]。每次更新l[i][j] = min(dp[i][j], l[i+1][j])。
同理用r[i][j]记录从右往左取的后手最小值。
至此DP优化至$O(n^2)$。
代码(0.015S):
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 using namespace std; 6 7 const int MAXN = 110; 8 9 int dp[MAXN][MAXN]; 10 int l[MAXN][MAXN], r[MAXN][MAXN]; 11 int a[MAXN], sum[MAXN]; 12 int n; 13 14 int main() { 15 while(scanf("%d", &n) != EOF && n) { 16 for(int i = 1; i <= n; ++i) scanf("%d", a + i); 17 for(int i = 1; i <= n; ++i) sum[i] = sum[i - 1] + a[i]; 18 for(int k = 0; k < n; ++k) { 19 for(int i = 1; i + k <= n; ++i) { 20 int j = i + k; 21 l[i][j] = r[i][j] = dp[i][j] = sum[j] - sum[i - 1]; 22 if(i != j) { 23 dp[i][j] = max(dp[i][j], sum[j] - sum[i - 1] - l[i + 1][j]); 24 dp[i][j] = max(dp[i][j], sum[j] - sum[i - 1] - r[i][j - 1]); 25 l[i][j] = min(dp[i][j], l[i + 1][j]); 26 r[i][j] = min(dp[i][j], r[i][j - 1]); 27 } 28 } 29 } 30 printf("%d\n", 2 * dp[1][n] - sum[n]); 31 } 32 }