UVA10891 Game of Sum

Game of Sum

 给定n个数数列,两个人交替拿数,每次只能拿左边连续一段或右边连续一段,必须拿至少一个数。两人均采用最优策略拿和更大的数,问先手得分-后手得分值。

 

dp[i][j]表示i..j数列先手最大,sum[i] = num[1] + num[2] + ... + num[i - 1] + num[i]

dp[i][j] = sum[j] - sum[i - 1] - min(dp[i + 1][j], dp[i + 2][j],.....,dp[j][j], dp[i][j-1],dp[i][j-2],...,dp[i][i],0)

令mi1[i][j] = min(dp[i][j],dp[i+1][j],dp[i+2][j],...dp[j][j])
令mi2[i][j] = min(dp[i][j],dp[i][j -1],dp[i][j-2]...dp[i][i])

mi1[i][j] = min(mi1[i + 1][j], dp[i][j])
mi2[i][j] = min(mi2[i][j - 1], dp[i][j])
dp[i][j] = sum[j] - sum[i - 1 - min(mi1[i + 1][j], mi2[i][j - 1], 0)

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cstdlib>
 5 #include <algorithm>
 6 #include <queue>
 7 #include <vector>
 8 #define min(a, b) ((a) < (b) ? (a) : (b))
 9 #define max(a, b) ((a) > (b) ? (a) : (b))
10 #define abs(a) ((a) < 0 ? (-1 * (a)) : (a))
11 inline void swap(int &a, int &b)
12 {
13     int tmp = a;a = b;b = tmp;
14 }
15 inline void read(int &x)
16 {
17     x = 0;char ch = getchar(), c = ch;
18     while(ch < '0' || ch > '9') c = ch, ch = getchar();
19     while(ch <= '9' && ch >= '0') x = x * 10 + ch - '0', ch = getchar();
20     if(c == '-') x = -x;
21 }
22 
23 const int INF = 0x3f3f3f3f;
24 const int MAXN = 100 + 10;
25 
26 int num[MAXN], dp[MAXN][MAXN], mi1[MAXN][MAXN], mi2[MAXN][MAXN], sum[MAXN], n;
27 /*
28 dp[i][j] = sum[i][j] - min(mi1[i + 1][j], mi2[i][j - 1], 0)
29 mi1[i][j] = min(dp[i][j],dp[i+1][j],dp[i+2][j],...dp[j][j])
30 mi2[i][j] = min(dp[i][j],dp[i][j-1],dp[i][j-2]...dp[i][i])
31 
32 mi1[i][j] = min(mi1[i + 1][j], dp[i][j])
33 mi2[i][j] = min(mi2[i][j - 1], dp[i][j])
34 
35 */
36 int main()
37 {
38     while(scanf("%d", &n) != EOF && n)
39     {
40         for(register int i = 1;i <= n;++ i) read(num[i]), sum[i] = sum[i - 1] + num[i];
41         for(register int i = 1;i <= n;++ i) dp[i][i] = num[i], mi1[i][i] = mi2[i][i] = num[i];
42         for(register int k = 2;k <= n;++ k)
43             for(register int i = 1;i <= n;++ i)
44             {
45                 int j = i + k - 1;
46                 if(j > n) break;
47                 dp[i][j] = sum[j] - sum[i - 1] - min(mi1[i + 1][j], min(mi2[i][j - 1], 0));
48                 mi1[i][j] = min(mi1[i + 1][j], dp[i][j]);
49                 mi2[i][j] = min(mi2[i][j - 1], dp[i][j]);
50             }
51         printf("%d\n", dp[1][n] - (sum[n] - dp[1][n]));
52     }
53     return 0;
54 }
UVA10891
posted @ 2018-01-17 07:31  嘒彼小星  阅读(139)  评论(0编辑  收藏  举报