hdu 6169 gems gems gems【DP】
Now there are n gems, each of which has its own value. Alice and Bob play a game with these n gems.
They place the gems in a row and decide to take turns to take gems from left to right.
Alice goes first and takes 1 or 2 gems from the left. After that, on
each turn a player can take k or k+1 gems if the other player takes k
gems in the previous turn. The game ends when there are no gems left or
the current player can’t take k or k+1 gems.
Your task is to determine the difference between the total value of gems
Alice took and Bob took. Assume both players play optimally. Alice
wants to maximize the difference while Bob wants to minimize it.
题目意思转化为第i个人希望最大化与第i+1个人的差值。
dp[i][j]表示当前人从第i个宝石开始取j个的最大差值。
1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 using namespace std; 5 const int N = 20001; 6 const int M = 201; 7 int a[N]; 8 int sum[N]; 9 int dp[N][M]; 10 int main() { 11 int t, n, i, j, ans; 12 scanf("%d", &t); 13 sum[0] = 0; 14 while(t--) { 15 scanf("%d", &n); 16 for(i = 1; i <= n; ++i) { 17 scanf("%d", &a[i]); 18 sum[i] = sum[i-1] + a[i]; 19 } 20 for(i = n; i >= 1; --i) { 21 for(j = min(200, n-i+1); j >= 1; --j) { 22 dp[i][j] = sum[i+j-1] - sum[i-1]; 23 if(i+j+j <= n)dp[i][j] -= max(dp[i+j][j], dp[i+j][j+1]); 24 else if(i+j+j-1 <= n) dp[i][j] -= dp[i+j][j]; 25 } 26 } 27 ans = dp[1][1]; 28 if(n>1) ans = max(dp[1][1], dp[1][2]); 29 printf("%d\n", ans); 30 } 31 return 0; 32 }