hdu 6169 gems gems gems【DP】

本文介绍了一个关于两人轮流取宝石的游戏问题,并通过动态规划的方法求解Alice和Bob取走宝石价值之差的最大值。游戏规则为:两人依次从一排宝石中取走一定数量的宝石,数量取决于上一人取走的数量。最终目标是计算最优策略下Alice比Bob多取走的价值。

题目链接:hdu 6169 gems gems gems

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 }
249ms

 

转载于:https://www.cnblogs.com/GraceSkyer/p/7543960.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值